diff --git a/app/src/main/java/org/linphone/ui/main/chat/model/MessageModel.kt b/app/src/main/java/org/linphone/ui/main/chat/model/MessageModel.kt index 5b4400ed3..a17b480e5 100644 --- a/app/src/main/java/org/linphone/ui/main/chat/model/MessageModel.kt +++ b/app/src/main/java/org/linphone/ui/main/chat/model/MessageModel.kt @@ -19,6 +19,7 @@ */ package org.linphone.ui.main.chat.model +import android.os.CountDownTimer import android.text.Spannable import android.text.SpannableStringBuilder import android.text.Spanned @@ -108,6 +109,10 @@ class MessageModel @WorkerThread constructor( val statusIcon = MutableLiveData() + val isEphemeral = MutableLiveData() + + val ephemeralLifetime = MutableLiveData() + val text = MutableLiveData() val reactions = MutableLiveData() @@ -169,6 +174,8 @@ class MessageModel @WorkerThread constructor( } // End of voice record related fields + private lateinit var countDownTimer: CountDownTimer + private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) private var transferringFileModel: FileModel? = null @@ -234,12 +241,20 @@ class MessageModel @WorkerThread constructor( } model?.transferProgress?.postValue(percent) } + + @WorkerThread + override fun onEphemeralMessageTimerStarted(message: ChatMessage) { + Log.d("$TAG Ephemeral timer started") + updateEphemeralTimer() + } } init { groupedWithNextMessage.postValue(isGroupedWithNextOne) groupedWithPreviousMessage.postValue(isGroupedWithPreviousOne) isPlayingVoiceRecord.postValue(false) + isEphemeral.postValue(chatMessage.isEphemeral) + updateEphemeralTimer() chatMessage.addListener(chatMessageListener) statusIcon.postValue(LinphoneUtils.getChatIconResId(chatMessage.state)) @@ -813,4 +828,33 @@ class MessageModel @WorkerThread constructor( return "" } + + @WorkerThread + private fun updateEphemeralTimer() { + if (chatMessage.isEphemeral) { + if (chatMessage.ephemeralExpireTime == 0L) { + // This means the message hasn't been read by all participants yet, so the countdown hasn't started + // In this case we simply display the configured value for lifetime + ephemeralLifetime.postValue( + TimestampUtils.formatLifetime(chatMessage.ephemeralLifetime) + ) + } else { + // Countdown has started, display remaining time + val remaining = chatMessage.ephemeralExpireTime - (System.currentTimeMillis() / 1000) + ephemeralLifetime.postValue(TimestampUtils.formatLifetime(remaining)) + if (!::countDownTimer.isInitialized) { + countDownTimer = object : CountDownTimer(remaining * 1000, 1000) { + override fun onFinish() {} + + override fun onTick(millisUntilFinished: Long) { + ephemeralLifetime.postValue( + TimestampUtils.formatLifetime(millisUntilFinished / 1000) + ) + } + } + countDownTimer.start() + } + } + } + } } diff --git a/app/src/main/java/org/linphone/utils/TimestampUtils.kt b/app/src/main/java/org/linphone/utils/TimestampUtils.kt index 44c1e93db..0a49e5182 100644 --- a/app/src/main/java/org/linphone/utils/TimestampUtils.kt +++ b/app/src/main/java/org/linphone/utils/TimestampUtils.kt @@ -27,6 +27,7 @@ import java.util.Calendar import java.util.Date import java.util.Locale import org.linphone.LinphoneApplication.Companion.coreContext +import org.linphone.R class TimestampUtils { companion object { @@ -217,6 +218,32 @@ class TimestampUtils { .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } } + @AnyThread + fun formatLifetime(seconds: Long): String { + val days = seconds / 86400 + val hours = seconds / 3600 + return when { + days >= 1L -> AppUtils.getStringWithPlural( + R.plurals.days, + days.toInt(), + "$days" + ) + hours >= 1L -> { + String.format( + "%02d:%02d:%02d", + seconds / 3600, + (seconds % 3600) / 60, + (seconds % 60) + ) + } + else -> String.format( + "%02d:%02d", + (seconds % 3600) / 60, + (seconds % 60) + ) + } + } + @AnyThread private fun isSameDay( cal1: Calendar, diff --git a/app/src/main/res/layout/chat_bubble_incoming.xml b/app/src/main/res/layout/chat_bubble_incoming.xml index 022cd3514..43adf4171 100644 --- a/app/src/main/res/layout/chat_bubble_incoming.xml +++ b/app/src/main/res/layout/chat_bubble_incoming.xml @@ -247,6 +247,29 @@ android:visibility="@{model.isFromGroup ? View.VISIBLE : View.GONE}" app:tint="?attr/color_main1_500" /> + + + + diff --git a/app/src/main/res/layout/chat_bubble_outgoing.xml b/app/src/main/res/layout/chat_bubble_outgoing.xml index b0e1e01f4..351249a33 100644 --- a/app/src/main/res/layout/chat_bubble_outgoing.xml +++ b/app/src/main/res/layout/chat_bubble_outgoing.xml @@ -198,6 +198,28 @@ android:gravity="center_vertical" android:orientation="horizontal"> + + + + diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 1a3ca6f9f..615026323 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -18,6 +18,10 @@ Commencer Aujourd\'hui Hier + + %s jour + %s jours + %s selectionné diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ea3d490c7..642f84f0d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -53,6 +53,10 @@ Start Today Yesterday + + %s day + %s days + %s selected