forked from upstream-mirrors/quik
Improved date formatting
This commit is contained in:
@@ -57,7 +57,7 @@ class ConversationAdapter(data: OrderedRealmCollection<Message>?) : RealmRecycle
|
|||||||
viewHolder.avatar.contacts = conversation?.contacts ?: RealmList()
|
viewHolder.avatar.contacts = conversation?.contacts ?: RealmList()
|
||||||
viewHolder.title.text = conversation?.getTitle()
|
viewHolder.title.text = conversation?.getTitle()
|
||||||
|
|
||||||
viewHolder.date.text = dateFormatter.getMessageTimestamp(message.date)
|
viewHolder.date.text = dateFormatter.getConversationTimestamp(message.date)
|
||||||
viewHolder.date.setTextColor(dateColor)
|
viewHolder.date.setTextColor(dateColor)
|
||||||
|
|
||||||
viewHolder.snippet.text = message.body
|
viewHolder.snippet.text = message.body
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ package com.moez.QKSMS.util
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.text.format.DateFormat
|
import android.text.format.DateFormat
|
||||||
import android.text.format.DateUtils
|
|
||||||
import com.moez.QKSMS.R
|
|
||||||
import com.moez.QKSMS.util.extensions.isDayAfter
|
|
||||||
import com.moez.QKSMS.util.extensions.isSameDay
|
import com.moez.QKSMS.util.extensions.isSameDay
|
||||||
import com.moez.QKSMS.util.extensions.isSameWeek
|
import com.moez.QKSMS.util.extensions.isSameWeek
|
||||||
import com.moez.QKSMS.util.extensions.isSameYear
|
import com.moez.QKSMS.util.extensions.isSameYear
|
||||||
@@ -16,27 +13,39 @@ class DateFormatter(val context: Context) {
|
|||||||
/**
|
/**
|
||||||
* Replace 12 hour format with 24 hour format if necessary
|
* Replace 12 hour format with 24 hour format if necessary
|
||||||
*/
|
*/
|
||||||
private fun accountFor24HourTime(context: Context, input: SimpleDateFormat): SimpleDateFormat {
|
private fun getFormatter(pattern: String): SimpleDateFormat {
|
||||||
val isUsing24HourTime = DateFormat.is24HourFormat(context)
|
val isUsing24HourTime = DateFormat.is24HourFormat(context)
|
||||||
|
|
||||||
return if (isUsing24HourTime) SimpleDateFormat(input.toPattern().replace('h', 'H').replace(" a".toRegex(), ""))
|
return if (isUsing24HourTime) SimpleDateFormat(pattern.replace('h', 'H').replace(" a".toRegex(), ""), Locale.US)
|
||||||
else input
|
else SimpleDateFormat(pattern, Locale.US)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessageTimestamp(date: Long): String {
|
fun getMessageTimestamp(date: Long): String {
|
||||||
|
|
||||||
|
val now = Calendar.getInstance()
|
||||||
val then = Calendar.getInstance()
|
val then = Calendar.getInstance()
|
||||||
then.timeInMillis = date
|
then.timeInMillis = date
|
||||||
|
|
||||||
val now = Calendar.getInstance()
|
|
||||||
|
|
||||||
val time = ", " + accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
|
||||||
return when {
|
return when {
|
||||||
now.isSameDay(then) -> accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
now.isSameDay(then) -> getFormatter("h:mm a")
|
||||||
now.isDayAfter(then) -> context.getString(R.string.date_yesterday) + time
|
now.isSameWeek(then) -> getFormatter("E h:mm a")
|
||||||
now.isSameWeek(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY) + time
|
now.isSameYear(then) -> getFormatter("MMM d, h:mm a")
|
||||||
now.isSameYear(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH) + time
|
else -> getFormatter("d/MM/yy h:mm a")
|
||||||
else -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH) + time
|
}.format(date)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getConversationTimestamp(date: Long): String {
|
||||||
|
|
||||||
|
val now = Calendar.getInstance()
|
||||||
|
val then = Calendar.getInstance()
|
||||||
|
then.timeInMillis = date
|
||||||
|
|
||||||
|
return when {
|
||||||
|
now.isSameDay(then) -> getFormatter("h:mm a")
|
||||||
|
now.isSameWeek(then) -> getFormatter("E")
|
||||||
|
now.isSameYear(then) -> getFormatter("MMM d")
|
||||||
|
else -> getFormatter("d/MM/yy")
|
||||||
|
}.format(date)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,4 @@ fun Calendar.isSameWeek(other: Calendar): Boolean {
|
|||||||
|
|
||||||
fun Calendar.isSameYear(other: Calendar): Boolean {
|
fun Calendar.isSameYear(other: Calendar): Boolean {
|
||||||
return get(Calendar.YEAR) == other.get(Calendar.YEAR)
|
return get(Calendar.YEAR) == other.get(Calendar.YEAR)
|
||||||
}
|
|
||||||
|
|
||||||
fun Calendar.isDayAfter(other: Calendar): Boolean {
|
|
||||||
other.add(Calendar.DAY_OF_YEAR, 1)
|
|
||||||
return isSameDay(other)
|
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,4 @@
|
|||||||
|
|
||||||
<string name="message_hint">Write a message</string>
|
<string name="message_hint">Write a message</string>
|
||||||
|
|
||||||
<string name="date_yesterday">Yesterday</string>
|
|
||||||
<string name="date_just_now">Just now</string>
|
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user