From a412b2128d583f7ec375268f7d598c89583656eb Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 5 Apr 2022 15:13:01 +0200 Subject: [PATCH] Rendering method --- .../im/vector/app/core/utils/TextUtils.kt | 55 ++++++++++++++++++- .../live/LocationLiveMessageBannerView.kt | 20 ++++++- vector/src/main/res/values/strings.xml | 9 +++ 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/core/utils/TextUtils.kt b/vector/src/main/java/im/vector/app/core/utils/TextUtils.kt index 992a85679c..db2009a620 100644 --- a/vector/src/main/java/im/vector/app/core/utils/TextUtils.kt +++ b/vector/src/main/java/im/vector/app/core/utils/TextUtils.kt @@ -19,11 +19,15 @@ package im.vector.app.core.utils import android.content.Context import android.os.Build import android.text.format.Formatter +import im.vector.app.R import org.threeten.bp.Duration import java.util.TreeMap object TextUtils { + private const val MINUTES_PER_HOUR = 60 + private const val SECONDS_PER_MINUTE = 60 + private val suffixes = TreeMap().also { it[1000] = "k" it[1000000] = "M" @@ -71,13 +75,58 @@ object TextUtils { } fun formatDuration(duration: Duration): String { - val hours = duration.seconds / 3600 - val minutes = (duration.seconds % 3600) / 60 - val seconds = duration.seconds % 60 + val hours = getHours(duration) + val minutes = getMinutes(duration) + val seconds = getSeconds(duration) return if (hours > 0) { String.format("%d:%02d:%02d", hours, minutes, seconds) } else { String.format("%02d:%02d", minutes, seconds) } } + + fun formatDurationWithUnits(context: Context, duration: Duration): String { + val hours = getHours(duration) + val minutes = getMinutes(duration) + val seconds = getSeconds(duration) + val builder = StringBuilder() + // TODO do we need Locale ? test with different language setting + when { + hours > 0 -> { + appendHours(context, builder, hours) + builder.append(" ") + appendMinutes(context, builder, minutes) + builder.append(" ") + appendSeconds(context, builder, seconds) + } + minutes > 0 -> { + appendMinutes(context, builder, minutes) + builder.append(" ") + appendSeconds(context, builder, seconds) + } + else -> { + appendSeconds(context, builder, seconds) + } + } + + return builder.toString() + } + + private fun appendHours(context: Context, builder: StringBuilder, hours: Int) { + builder.append(context.resources.getQuantityString(R.plurals.time_unit_hour_short, hours)) + } + + private fun appendMinutes(context: Context, builder: StringBuilder, minutes: Int) { + builder.append(minutes) + builder.append(context.getString(R.string.time_unit_minute_short)) + } + + private fun appendSeconds(context: Context, builder: StringBuilder, seconds: Int) { + builder.append(seconds) + builder.append(context.getString(R.string.time_unit_second_short)) + } + + private fun getHours(duration: Duration): Int = duration.toHours().toInt() + private fun getMinutes(duration: Duration): Int = duration.toMinutes().toInt() % MINUTES_PER_HOUR + private fun getSeconds(duration: Duration): Int = (duration.seconds % SECONDS_PER_MINUTE).toInt() } diff --git a/vector/src/main/java/im/vector/app/features/location/live/LocationLiveMessageBannerView.kt b/vector/src/main/java/im/vector/app/features/location/live/LocationLiveMessageBannerView.kt index 95805d5b7d..12fb0db4ba 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/LocationLiveMessageBannerView.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/LocationLiveMessageBannerView.kt @@ -20,9 +20,18 @@ import android.content.Context import android.util.AttributeSet import android.view.LayoutInflater import android.widget.Button +import android.widget.TextView import androidx.constraintlayout.widget.ConstraintLayout +import androidx.core.view.isVisible +import im.vector.app.R +import im.vector.app.core.utils.TextUtils import im.vector.app.databinding.ViewLocationLiveMessageBannerBinding -import im.vector.app.databinding.ViewLocationLiveStatusBinding +import org.threeten.bp.Duration + +data class LocationLiveMessageBannerViewState( + val isStopButtonVisible: Boolean, + val remainingTimeInMillis: Long +) class LocationLiveMessageBannerView @JvmOverloads constructor( context: Context, @@ -37,4 +46,13 @@ class LocationLiveMessageBannerView @JvmOverloads constructor( val stopButton: Button get() = binding.locationLiveMessageBannerStop + + private val subTitle: TextView + get() = binding.locationLiveMessageBannerSubTitle + + fun render(viewState: LocationLiveMessageBannerViewState) { + stopButton.isVisible = viewState.isStopButtonVisible + val duration = Duration.ofMillis(viewState.remainingTimeInMillis.coerceAtLeast(0L)) + subTitle.text = context.getString(R.string.location_share_live_remaining_time, TextUtils.formatDurationWithUnits(context, duration)) + } } diff --git a/vector/src/main/res/values/strings.xml b/vector/src/main/res/values/strings.xml index f137592c60..b73bb47690 100644 --- a/vector/src/main/res/values/strings.xml +++ b/vector/src/main/res/values/strings.xml @@ -322,6 +322,14 @@ Start Chatting Spaces + + + hour + hours + + min + sec + Some permissions are missing to perform this action, please grant the permissions from the system settings. To perform this action, please grant the Camera permission from the system settings. @@ -3012,6 +3020,7 @@ Loading live location… Stop Stop sharing + %1$s left ${app_name} Live Location Location sharing is in progress