Skip to content

Commit 420b3ef

Browse files
committed
chore: sort tasks by IDs
1 parent 74b71fb commit 420b3ef

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

examples/src/main/java/com/ably/example/screen/TaskManagementScreen.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import com.ably.example.setCoroutine
2323
import io.ably.lib.objects.type.map.LiveMapValue
2424
import io.ably.lib.realtime.AblyRealtime
2525
import kotlinx.coroutines.launch
26+
import kotlin.uuid.ExperimentalUuidApi
27+
import kotlin.uuid.Uuid
2628

27-
@OptIn(ExperimentalMaterial3Api::class)
29+
@OptIn(ExperimentalMaterial3Api::class, ExperimentalUuidApi::class)
2830
@Composable
2931
fun TaskManagementScreen(realtimeClient: AblyRealtime) {
3032
var taskText by remember { mutableStateOf("") }
@@ -38,6 +40,10 @@ fun TaskManagementScreen(realtimeClient: AblyRealtime) {
3840

3941
val (taskIdToTask, liveTasks) = observeMap(channel, root, "tasks")
4042

43+
val taskEntries = remember(taskIdToTask) {
44+
taskIdToTask.entries.sortedBy { it.key }
45+
}
46+
4147
Column(
4248
modifier = Modifier
4349
.fillMaxSize()
@@ -77,7 +83,7 @@ fun TaskManagementScreen(realtimeClient: AblyRealtime) {
7783
onClick = {
7884
if (taskText.isNotBlank()) {
7985
scope.launch {
80-
val taskId = "task_${System.currentTimeMillis()}"
86+
val taskId = "${System.currentTimeMillis()}_${Uuid.random().toHexString()}"
8187
liveTasks?.setCoroutine(taskId, LiveMapValue.of(taskText.trim()))
8288
taskText = ""
8389
}
@@ -139,8 +145,8 @@ fun TaskManagementScreen(realtimeClient: AblyRealtime) {
139145
LazyColumn(
140146
verticalArrangement = Arrangement.spacedBy(8.dp)
141147
) {
142-
items(taskIdToTask.entries.size) { index ->
143-
val task = taskIdToTask.entries.elementAt(index)
148+
items(taskIdToTask.size) { index ->
149+
val task = taskEntries.elementAt(index)
144150
TaskItemCard(
145151
task = task,
146152
isEditing = editingTaskId == task.key,

0 commit comments

Comments
 (0)