Rendering method

This commit is contained in:
Maxime NATUREL 2022-04-05 15:13:01 +02:00
parent df1ba8ec88
commit a412b2128d
3 changed files with 80 additions and 4 deletions

View File

@ -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<Int, String>().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()
}

View File

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

View File

@ -322,6 +322,14 @@
<string name="start_chatting">Start Chatting</string>
<string name="spaces">Spaces</string>
<!-- Time units -->
<plurals name="time_unit_hour_short">
<item quantity="one">hour</item>
<item quantity="other">hours</item>
</plurals>
<string name="time_unit_minute_short">min</string>
<string name="time_unit_second_short">sec</string>
<!-- Permissions denied forever -->
<string name="denied_permission_generic">Some permissions are missing to perform this action, please grant the permissions from the system settings.</string>
<string name="denied_permission_camera">To perform this action, please grant the Camera permission from the system settings.</string>
@ -3012,6 +3020,7 @@
<string name="location_share_live_started">Loading live location…</string>
<string name="location_share_live_stop">Stop</string>
<string name="location_share_live_stop_long_version">Stop sharing</string>
<string name="location_share_live_remaining_time">%1$s left</string>
<string name="live_location_sharing_notification_title">${app_name} Live Location</string>
<string name="live_location_sharing_notification_description">Location sharing is in progress</string>