Localize time formatting for chat messages

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-12-09 17:27:40 +01:00
parent b93845cebd
commit b3836c007f
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
12 changed files with 75 additions and 0 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)

View file

@ -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) {

View file

@ -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()