From 2341834c6e8f06f9c721c018570409c3f0e6c9f5 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 26 Sep 2017 16:58:43 -0400 Subject: [PATCH] Improved date formatting --- .../ui/conversations/ConversationAdapter.kt | 2 +- .../java/com/moez/QKSMS/util/DateFormatter.kt | 39 ++++++++++++------- .../util/extensions/CalendarExtensions.kt | 5 --- qksms/src/main/res/values/strings.xml | 3 -- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/qksms/src/main/java/com/moez/QKSMS/ui/conversations/ConversationAdapter.kt b/qksms/src/main/java/com/moez/QKSMS/ui/conversations/ConversationAdapter.kt index bc3fd9b6..f43b1f03 100644 --- a/qksms/src/main/java/com/moez/QKSMS/ui/conversations/ConversationAdapter.kt +++ b/qksms/src/main/java/com/moez/QKSMS/ui/conversations/ConversationAdapter.kt @@ -57,7 +57,7 @@ class ConversationAdapter(data: OrderedRealmCollection?) : RealmRecycle viewHolder.avatar.contacts = conversation?.contacts ?: RealmList() viewHolder.title.text = conversation?.getTitle() - viewHolder.date.text = dateFormatter.getMessageTimestamp(message.date) + viewHolder.date.text = dateFormatter.getConversationTimestamp(message.date) viewHolder.date.setTextColor(dateColor) viewHolder.snippet.text = message.body diff --git a/qksms/src/main/java/com/moez/QKSMS/util/DateFormatter.kt b/qksms/src/main/java/com/moez/QKSMS/util/DateFormatter.kt index f99f135d..aa1d29c0 100644 --- a/qksms/src/main/java/com/moez/QKSMS/util/DateFormatter.kt +++ b/qksms/src/main/java/com/moez/QKSMS/util/DateFormatter.kt @@ -2,9 +2,6 @@ package com.moez.QKSMS.util import android.content.Context 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.isSameWeek 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 */ - private fun accountFor24HourTime(context: Context, input: SimpleDateFormat): SimpleDateFormat { + private fun getFormatter(pattern: String): SimpleDateFormat { val isUsing24HourTime = DateFormat.is24HourFormat(context) - return if (isUsing24HourTime) SimpleDateFormat(input.toPattern().replace('h', 'H').replace(" a".toRegex(), "")) - else input + return if (isUsing24HourTime) SimpleDateFormat(pattern.replace('h', 'H').replace(" a".toRegex(), ""), Locale.US) + else SimpleDateFormat(pattern, Locale.US) } fun getMessageTimestamp(date: Long): String { + + val now = Calendar.getInstance() val then = Calendar.getInstance() then.timeInMillis = date - val now = Calendar.getInstance() - - val time = ", " + accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date) return when { - now.isSameDay(then) -> accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date) - now.isDayAfter(then) -> context.getString(R.string.date_yesterday) + time - now.isSameWeek(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY) + time - now.isSameYear(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH) + time - else -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH) + time - } + now.isSameDay(then) -> getFormatter("h:mm a") + now.isSameWeek(then) -> getFormatter("E h:mm a") + now.isSameYear(then) -> getFormatter("MMM d, h:mm a") + else -> getFormatter("d/MM/yy h:mm a") + }.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) } } \ No newline at end of file diff --git a/qksms/src/main/java/com/moez/QKSMS/util/extensions/CalendarExtensions.kt b/qksms/src/main/java/com/moez/QKSMS/util/extensions/CalendarExtensions.kt index f19fc20d..8f3ddd28 100644 --- a/qksms/src/main/java/com/moez/QKSMS/util/extensions/CalendarExtensions.kt +++ b/qksms/src/main/java/com/moez/QKSMS/util/extensions/CalendarExtensions.kt @@ -12,9 +12,4 @@ fun Calendar.isSameWeek(other: Calendar): Boolean { fun Calendar.isSameYear(other: Calendar): Boolean { return get(Calendar.YEAR) == other.get(Calendar.YEAR) -} - -fun Calendar.isDayAfter(other: Calendar): Boolean { - other.add(Calendar.DAY_OF_YEAR, 1) - return isSameDay(other) } \ No newline at end of file diff --git a/qksms/src/main/res/values/strings.xml b/qksms/src/main/res/values/strings.xml index 3407e1e1..d4eb222a 100644 --- a/qksms/src/main/res/values/strings.xml +++ b/qksms/src/main/res/values/strings.xml @@ -4,7 +4,4 @@ Write a message - Yesterday - Just now -