From b3836c007f672b44f26f2fb0a7a30142e5076a41 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Fri, 9 Dec 2022 17:27:40 +0100 Subject: [PATCH] Localize time formatting for chat messages Signed-off-by: Andy Scherzinger --- .../IncomingLinkPreviewMessageViewHolder.kt | 5 +++++ .../IncomingLocationMessageViewHolder.kt | 6 ++++++ .../messages/IncomingPollMessageViewHolder.kt | 5 +++++ .../messages/IncomingTextMessageViewHolder.kt | 6 ++++++ .../messages/IncomingVoiceMessageViewHolder.kt | 6 ++++++ .../OutcomingLinkPreviewMessageViewHolder.kt | 5 +++++ .../OutcomingLocationMessageViewHolder.kt | 5 +++++ .../messages/OutcomingPollMessageViewHolder.kt | 5 +++++ .../messages/OutcomingTextMessageViewHolder.kt | 6 ++++++ .../messages/OutcomingVoiceMessageViewHolder.kt | 5 +++++ .../messages/PreviewMessageViewHolder.kt | 5 +++++ .../java/com/nextcloud/talk/utils/DateUtils.kt | 16 ++++++++++++++++ 12 files changed, 75 insertions(+) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt index b6037a9d4..2da908446 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLinkPreviewMessageViewHolder.kt @@ -40,6 +40,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import javax.inject.Inject @@ -60,6 +61,9 @@ class IncomingLinkPreviewMessageViewHolder(incomingView: View, payload: Any) : M @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @Inject lateinit var ncApi: NcApi @@ -72,6 +76,7 @@ class IncomingLinkPreviewMessageViewHolder(incomingView: View, payload: Any) : M super.onBind(message) this.message = message sharedApplication!!.componentApplication.inject(this) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) setAvatarAndAuthorOnMessageItem(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt index 65a4bc8f6..5c9051ef0 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingLocationMessageViewHolder.kt @@ -49,6 +49,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.UriUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders @@ -75,6 +76,9 @@ class IncomingLocationMessageViewHolder(incomingView: View, payload: Any) : Mess @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + lateinit var commonMessageInterface: CommonMessageInterface @SuppressLint("SetTextI18n") @@ -92,6 +96,8 @@ class IncomingLocationMessageViewHolder(incomingView: View, payload: Any) : Mess binding.messageText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize) binding.messageText.text = message.text + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) + // parent message handling setParentMessageDataOnMessageItem(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt index c1b9a00ff..732e75663 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingPollMessageViewHolder.kt @@ -39,6 +39,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.polls.ui.PollMainDialogFragment import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import javax.inject.Inject @@ -59,6 +60,9 @@ class IncomingPollMessageViewHolder(incomingView: View, payload: Any) : MessageH @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @Inject lateinit var ncApi: NcApi @@ -71,6 +75,7 @@ class IncomingPollMessageViewHolder(incomingView: View, payload: Any) : MessageH super.onBind(message) this.message = message sharedApplication!!.componentApplication.inject(this) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) setAvatarAndAuthorOnMessageItem(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt index 800375c6a..43edceb5d 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingTextMessageViewHolder.kt @@ -45,6 +45,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.DisplayUtils import com.nextcloud.talk.utils.TextMatchers import com.nextcloud.talk.utils.preferences.AppPreferences @@ -66,6 +67,9 @@ class IncomingTextMessageViewHolder(itemView: View, payload: Any) : MessageHolde @Inject lateinit var appPreferences: AppPreferences + @Inject + lateinit var dateUtils: DateUtils + lateinit var commonMessageInterface: CommonMessageInterface override fun onBind(message: ChatMessage) { @@ -104,6 +108,8 @@ class IncomingTextMessageViewHolder(itemView: View, payload: Any) : MessageHolde binding.messageText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize) binding.messageText.text = messageString + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) + // parent message handling if (!message.isDeleted && message.parentMessage != null) { processParentMessage(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt index ab67e99d6..8b281a7ca 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/IncomingVoiceMessageViewHolder.kt @@ -47,6 +47,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import java.util.concurrent.ExecutionException @@ -66,6 +67,9 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) : Message @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @JvmField @Inject var appPreferences: AppPreferences? = null @@ -81,6 +85,8 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) : Message this.message = message sharedApplication!!.componentApplication.inject(this) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) + setAvatarAndAuthorOnMessageItem(message) colorizeMessageBubble(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLinkPreviewMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLinkPreviewMessageViewHolder.kt index f88b90648..d47cbfd0c 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLinkPreviewMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLinkPreviewMessageViewHolder.kt @@ -39,6 +39,7 @@ import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import javax.inject.Inject @@ -56,6 +57,9 @@ class OutcomingLinkPreviewMessageViewHolder(outcomingView: View, payload: Any) : @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @Inject lateinit var appPreferences: AppPreferences @@ -73,6 +77,7 @@ class OutcomingLinkPreviewMessageViewHolder(outcomingView: View, payload: Any) : sharedApplication!!.componentApplication.inject(this) val textColor = viewThemeUtils.getScheme(binding.messageTime.context).onSurfaceVariant binding.messageTime.setTextColor(textColor) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) colorizeMessageBubble(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt index 3802e54dd..603a1a8fe 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingLocationMessageViewHolder.kt @@ -46,6 +46,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.UriUtils import com.stfalcon.chatkit.messages.MessageHolders import java.net.URLEncoder @@ -70,6 +71,9 @@ class OutcomingLocationMessageViewHolder(incomingView: View) : MessageHolders @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + lateinit var commonMessageInterface: CommonMessageInterface @SuppressLint("SetTextI18n") @@ -78,6 +82,7 @@ class OutcomingLocationMessageViewHolder(incomingView: View) : MessageHolders sharedApplication!!.componentApplication.inject(this) val textColor = viewThemeUtils.getScheme(binding.messageTime.context).onSurfaceVariant binding.messageTime.setTextColor(textColor) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) realView.isSelected = false val layoutParams = binding.messageTime.layoutParams as FlexboxLayout.LayoutParams diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingPollMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingPollMessageViewHolder.kt index 4788ba2f5..828896999 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingPollMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingPollMessageViewHolder.kt @@ -39,6 +39,7 @@ import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.polls.ui.PollMainDialogFragment import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import javax.inject.Inject @@ -56,6 +57,9 @@ class OutcomingPollMessageViewHolder(outcomingView: View, payload: Any) : Messag @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @Inject lateinit var appPreferences: AppPreferences @@ -73,6 +77,7 @@ class OutcomingPollMessageViewHolder(outcomingView: View, payload: Any) : Messag sharedApplication!!.componentApplication.inject(this) val textColor = viewThemeUtils.getScheme(binding.messageTime.context).onSurfaceVariant binding.messageTime.setTextColor(textColor) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) colorizeMessageBubble(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt index 1b48f6e3f..58111c687 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingTextMessageViewHolder.kt @@ -44,6 +44,7 @@ import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.DisplayUtils.searchAndReplaceWithMentionSpan import com.nextcloud.talk.utils.TextMatchers import com.stfalcon.chatkit.messages.MessageHolders.OutcomingTextMessageViewHolder @@ -60,6 +61,9 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + lateinit var commonMessageInterface: CommonMessageInterface override fun onBind(message: ChatMessage) { @@ -88,6 +92,8 @@ class OutcomingTextMessageViewHolder(itemView: View) : OutcomingTextMessageViewH binding.messageText.setTextColor(textColor) binding.messageText.text = messageString + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) + // parent message handling if (!message.isDeleted && message.parentMessage != null) { processParentMessage(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingVoiceMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingVoiceMessageViewHolder.kt index 76ece745e..611acd723 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingVoiceMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/OutcomingVoiceMessageViewHolder.kt @@ -43,6 +43,7 @@ import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.models.json.chat.ReadStatus import com.nextcloud.talk.ui.theme.ViewThemeUtils import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.preferences.AppPreferences import com.stfalcon.chatkit.messages.MessageHolders import java.util.concurrent.ExecutionException @@ -62,6 +63,9 @@ class OutcomingVoiceMessageViewHolder(outcomingView: View) : MessageHolders @Inject lateinit var viewThemeUtils: ViewThemeUtils + @Inject + lateinit var dateUtils: DateUtils + @JvmField @Inject var appPreferences: AppPreferences? = null @@ -80,6 +84,7 @@ class OutcomingVoiceMessageViewHolder(outcomingView: View) : MessageHolders sharedApplication!!.componentApplication.inject(this) val textColor = viewThemeUtils.getScheme(binding.messageTime.context).onSurfaceVariant binding.messageTime.setTextColor(textColor) + binding.messageTime.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) colorizeMessageBubble(message) diff --git a/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt b/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt index 2759f0bbc..d2a41f1c1 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt +++ b/app/src/main/java/com/nextcloud/talk/adapters/messages/PreviewMessageViewHolder.kt @@ -56,6 +56,7 @@ import com.nextcloud.talk.extensions.loadChangelogBotAvatar import com.nextcloud.talk.models.json.chat.ChatMessage import com.nextcloud.talk.ui.recyclerview.MessageSwipeCallback import com.nextcloud.talk.ui.theme.ViewThemeUtils +import com.nextcloud.talk.utils.DateUtils import com.nextcloud.talk.utils.DisplayUtils import com.nextcloud.talk.utils.DrawableUtils.getDrawableResourceIdForMimeType import com.nextcloud.talk.utils.FileViewerUtils @@ -81,6 +82,9 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) : @Inject var viewThemeUtils: ViewThemeUtils? = null + @Inject + lateinit var dateUtils: DateUtils + @JvmField @Inject var okHttpClient: OkHttpClient? = null @@ -102,6 +106,7 @@ abstract class PreviewMessageViewHolder(itemView: View?, payload: Any?) : @Suppress("NestedBlockDepth", "ComplexMethod", "LongMethod") override fun onBind(message: ChatMessage) { super.onBind(message) + time.text = dateUtils.getLocalTimeStringFromTimestamp(message.timestamp) if (userAvatar != null) { if (message.isGrouped || message.isOneToOneConversation) { if (message.isOneToOneConversation) { diff --git a/app/src/main/java/com/nextcloud/talk/utils/DateUtils.kt b/app/src/main/java/com/nextcloud/talk/utils/DateUtils.kt index 6f32260f7..2f8b3836f 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/DateUtils.kt +++ b/app/src/main/java/com/nextcloud/talk/utils/DateUtils.kt @@ -48,14 +48,30 @@ class DateUtils(val context: Context) { }, ) + /* date formatter in local timezone and locale */ + private var formatTime: DateFormat = DateFormat.getTimeInstance( + DateFormat.SHORT, // timeStyle + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + context.resources.configuration.locales[0] + } else { + @Suppress("DEPRECATION") + context.resources.configuration.locale + }, + ) + init { format.timeZone = tz + formatTime.timeZone = tz } fun getLocalDateTimeStringFromTimestamp(timestampMilliseconds: Long): String { return format.format(Date(timestampMilliseconds)) } + fun getLocalTimeStringFromTimestamp(timestampSeconds: Long): String { + return formatTime.format(Date(timestampSeconds * DateConstants.SECOND_DIVIDER)) + } + fun relativeStartTimeForLobby(timestampMilliseconds: Long, resources: Resources): String { return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { val fmt = RelativeDateTimeFormatter.getInstance()