android: Duration formatter (#1419)

This commit is contained in:
Stanislav Dmitrenko
2022-11-25 15:23:30 +03:00
committed by GitHub
parent eb099c526a
commit 2eec81c35e
6 changed files with 16 additions and 17 deletions
@@ -1284,7 +1284,13 @@ class CIQuote (
val content: MsgContent,
val formattedText: List<FormattedText>? = null
): ItemContent {
override val text: String get() = content.text
override val text: String by lazy {
if (content is MsgContent.MCVoice && content.text.isEmpty())
content.toTextWithDuration(true)
else
content.text
}
fun sender(membership: GroupMember?): String? = when (chatDir) {
is CIDirection.DirectSnd -> generalGetString(R.string.sender_you_pronoun)
@@ -1370,7 +1376,7 @@ sealed class MsgContent {
}
fun MsgContent.MCVoice.toTextWithDuration(short: Boolean): String {
val time = String.format("%02d:%02d", duration / 60, duration % 60)
val time = durationToString(duration)
return if (short) time else generalGetString(R.string.voice_message) + " ($time)"
}
@@ -1607,11 +1613,9 @@ enum class CICallStatus {
Accepted -> generalGetString(R.string.callstatus_accepted)
Negotiated -> generalGetString(R.string.callstatus_connecting)
Progress -> generalGetString(R.string.callstatus_in_progress)
Ended -> String.format(generalGetString(R.string.callstatus_ended), duration(sec))
Ended -> String.format(generalGetString(R.string.callstatus_ended), durationToString(sec))
Error -> generalGetString(R.string.callstatus_error)
}
fun duration(sec: Int): String = "%02d:%02d".format(sec / 60, sec % 60)
}
@Serializable
@@ -84,9 +84,8 @@ fun ComposeVoiceView(filePath: String, durationMs: Int, finished: Boolean, cance
val numberInText = remember(durationMs, audioInfo.value) {
derivedStateOf { if (audioPlaying.value) audioInfo.value.progressMs / 1000 else durationMs / 1000 }
}
val text = "%02d:%02d".format(numberInText.value / 60, numberInText.value % 60)
Text(
text,
durationToString(numberInText.value),
fontSize = 18.sp,
color = HighOrLowlight,
)
@@ -16,6 +16,7 @@ import androidx.compose.ui.unit.sp
import chat.simplex.app.R
import chat.simplex.app.model.*
import chat.simplex.app.ui.theme.*
import chat.simplex.app.views.helpers.durationToString
@Composable
fun CICallItemView(cInfo: ChatInfo, cItem: ChatItem, status: CICallStatus, duration: Int, acceptCall: (Contact) -> Unit) {
@@ -38,7 +39,7 @@ fun CICallItemView(cInfo: ChatInfo, cItem: ChatItem, status: CICallStatus, durat
CICallStatus.Progress -> Icon(Icons.Filled.PhoneInTalk, stringResource(R.string.icon_descr_call_progress), tint = SimplexGreen)
CICallStatus.Ended -> Row {
Icon(Icons.Outlined.CallEnd, stringResource(R.string.icon_descr_call_ended), tint = HighOrLowlight, modifier = Modifier.padding(end = 4.dp))
Text(status.duration(duration), color = HighOrLowlight)
Text(durationToString(duration), color = HighOrLowlight)
}
CICallStatus.Error -> {}
}
@@ -65,7 +65,7 @@ fun CIVoiceView(
val time = if (audioPlaying.value) audioInfo.value.progressMs else audioInfo.value.durationMs
val minWidth = with(LocalDensity.current) { 45.sp.toDp() }
val text = String.format("%02d:%02d", time / 1000 / 60, time / 1000 % 60)
val text = durationToString(time / 1000)
if (hasText) {
VoiceMsgIndicator(file, audioPlaying.value, sent, hasText, audioInfo, brokenAudio, play, pause)
Text(
@@ -58,12 +58,8 @@ fun FramedItemView(
Modifier.padding(vertical = 6.dp, horizontal = 12.dp),
contentAlignment = Alignment.TopStart
) {
val text = if (qi.content is MsgContent.MCVoice && qi.text.isEmpty())
qi.content.toTextWithDuration(true)
else
qi.text
MarkdownText(
text, qi.formattedText, sender = qi.sender(membership()), senderBold = true, maxLines = 3,
qi.text, qi.formattedText, sender = qi.sender(membership()), senderBold = true, maxLines = 3,
style = TextStyle(fontSize = 15.sp, color = MaterialTheme.colors.onSurface),
linkMode = linkMode
)
@@ -1,7 +1,5 @@
package chat.simplex.app.views.helpers
import android.R.attr.factor
import android.R.color
import android.content.Context
import android.content.res.Resources
import android.graphics.*
@@ -28,7 +26,6 @@ import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.*
import androidx.core.content.FileProvider
import androidx.core.net.toFile
import androidx.core.text.HtmlCompat
import chat.simplex.app.*
import chat.simplex.app.model.CIFile
@@ -450,6 +447,8 @@ fun directoryFileCountAndSize(dir: String): Pair<Int, Long> { // count, size in
return fileCount to bytes
}
fun durationToString(sec: Int): String = "%02d:%02d".format(sec / 60, sec % 60)
fun Color.darker(factor: Float = 0.1f): Color =
Color(max(red * (1 - factor), 0f), max(green * (1 - factor), 0f), max(blue * (1 - factor), 0f), alpha)