Skip to content

Commit 4f08c01

Browse files
committed
Add time formatting for average response time in user profile
1 parent 178790e commit 4f08c01

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ui/profile/UserProfileActivity.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.getstream.chat.android.compose.sample.ui.profile
1818

19+
import android.content.res.Resources
1920
import android.os.Bundle
2021
import androidx.activity.ComponentActivity
2122
import androidx.activity.compose.setContent
@@ -36,12 +37,13 @@ import androidx.compose.runtime.collectAsState
3637
import androidx.compose.runtime.getValue
3738
import androidx.compose.ui.Alignment
3839
import androidx.compose.ui.Modifier
40+
import androidx.compose.ui.platform.LocalContext
3941
import androidx.compose.ui.platform.LocalLayoutDirection
4042
import androidx.compose.ui.res.painterResource
4143
import androidx.compose.ui.text.style.TextOverflow
4244
import androidx.compose.ui.unit.dp
4345
import androidx.lifecycle.viewmodel.compose.viewModel
44-
import io.getstream.chat.android.compose.R
46+
import io.getstream.chat.android.compose.sample.R
4547
import io.getstream.chat.android.compose.ui.components.BackButton
4648
import io.getstream.chat.android.compose.ui.components.LoadingIndicator
4749
import io.getstream.chat.android.compose.ui.components.avatar.UserAvatar
@@ -88,6 +90,7 @@ class UserProfileActivity : ComponentActivity() {
8890
}
8991
}
9092

93+
@Suppress("LongMethod")
9194
@Composable
9295
private fun UserProfileScreenContent(
9396
user: User?,
@@ -141,7 +144,10 @@ private fun UserProfileScreenContent(
141144
color = ChatTheme.colors.textHighEmphasis,
142145
)
143146
Text(
144-
text = "$avgResponseTimeInSeconds seconds",
147+
text = formatTime(
148+
resources = LocalContext.current.resources,
149+
seconds = avgResponseTimeInSeconds,
150+
),
145151
style = ChatTheme.typography.body,
146152
color = ChatTheme.colors.textHighEmphasis,
147153
maxLines = 1,
@@ -157,3 +163,21 @@ private fun UserProfileScreenContent(
157163
private fun Divider() {
158164
HorizontalDivider(modifier = Modifier.padding(vertical = 4.dp))
159165
}
166+
167+
@Suppress("MagicNumber")
168+
private fun formatTime(
169+
resources: Resources,
170+
seconds: Long,
171+
): String {
172+
val minutes = (seconds / 60).toInt()
173+
val remainingSeconds = (seconds % 60).toInt()
174+
return buildString {
175+
if (minutes > 0) {
176+
append(resources.getQuantityString(R.plurals.time_minutes, minutes, minutes))
177+
}
178+
if (remainingSeconds > 0) {
179+
if (isNotEmpty()) append(" ")
180+
append(resources.getQuantityString(R.plurals.time_seconds, remainingSeconds, remainingSeconds))
181+
}
182+
}
183+
}

stream-chat-android-compose-sample/src/main/res/values/strings.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,14 @@
111111
<string name="reminders_query_failed">Failed to load reminders</string>
112112
<string name="reminders_update_failed">Failed to update the reminder</string>
113113
<string name="reminders_delete_failed">Failed to delete the reminder</string>
114+
115+
<plurals name="time_minutes">
116+
<item quantity="one">1 minute</item>
117+
<item quantity="other">%d minutes</item>
118+
</plurals>
119+
<plurals name="time_seconds">
120+
<item quantity="one">1 second</item>
121+
<item quantity="other">%d seconds</item>
122+
</plurals>
123+
114124
</resources>

0 commit comments

Comments
 (0)