From 51ef704cf7765513eac7ac1d5f557756a8a99871 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 23 Oct 2022 20:01:24 +0200 Subject: [PATCH] Improve dialog screen by displaying more info --- .../lonami/talaria/data/DialogRepository.kt | 44 ++++++++++- .../java/dev/lonami/talaria/models/Dialog.kt | 6 +- .../dev/lonami/talaria/models/MessageAck.kt | 7 ++ .../lonami/talaria/models/MessagePreview.kt | 10 +++ .../lonami/talaria/ui/screens/DialogScreen.kt | 75 ++++++++++++++++--- .../java/dev/lonami/talaria/ui/theme/Theme.kt | 2 +- app/src/main/res/drawable/seen.xml | 12 +++ app/src/main/res/drawable/sent.xml | 9 +++ app/src/main/res/drawable/tack.xml | 9 +++ app/src/main/res/values/strings.xml | 4 + 10 files changed, 161 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/dev/lonami/talaria/models/MessageAck.kt create mode 100644 app/src/main/java/dev/lonami/talaria/models/MessagePreview.kt create mode 100644 app/src/main/res/drawable/seen.xml create mode 100644 app/src/main/res/drawable/sent.xml create mode 100644 app/src/main/res/drawable/tack.xml diff --git a/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt b/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt index 89e3ef7..c2d8377 100644 --- a/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt +++ b/app/src/main/java/dev/lonami/talaria/data/DialogRepository.kt @@ -2,6 +2,9 @@ package dev.lonami.talaria.data import dev.lonami.talaria.bindings.Native import dev.lonami.talaria.models.Dialog +import dev.lonami.talaria.models.MessageAck +import dev.lonami.talaria.models.MessagePreview +import java.time.LocalDateTime interface DialogRepository { fun loadDialogs(): List; @@ -16,9 +19,14 @@ class NativeDialogRepository : DialogRepository { for (i in 0 until dialogCount) { dialogs.add( Dialog( - Native.dialogTitle(dialogPtr, i), - Native.dialogPacked(dialogPtr, i), - false + title = Native.dialogTitle(dialogPtr, i), + lastMessage = MessagePreview( + sender = "", + text = Native.dialogPacked(dialogPtr, i), + date = LocalDateTime.now(), + ack = MessageAck.RECEIVED + ), + pinned = false ) ) } @@ -32,7 +40,35 @@ class MockDialogRepository : DialogRepository { override fun loadDialogs(): List { val dialogs = mutableListOf() for (i in 0 until 10) { - dialogs.add(Dialog("Sample Dialog $i", "Sample Message $i", i == 0)) + dialogs.add( + Dialog( + title = "Sample Dialog $i", + lastMessage = if (i % 4 == 3) { + null + } else { + MessagePreview( + sender = if (i % 2 == 0) { + "Sender A" + } else { + "Sender B" + }, + text = if (i % 3 == 2) { + "Very Long Sample Message $i, with a Lot of Text, which makes it hard to Preview" + } else { + "Sample Message $i" + }, + date = LocalDateTime.now(), + ack = when (i % 3) { + 0 -> MessageAck.RECEIVED + 1 -> MessageAck.SENT + 2 -> MessageAck.SEEN + else -> throw RuntimeException() + } + ) + }, + pinned = i < 4 + ) + ) } return dialogs } diff --git a/app/src/main/java/dev/lonami/talaria/models/Dialog.kt b/app/src/main/java/dev/lonami/talaria/models/Dialog.kt index a75b67c..220b762 100644 --- a/app/src/main/java/dev/lonami/talaria/models/Dialog.kt +++ b/app/src/main/java/dev/lonami/talaria/models/Dialog.kt @@ -1,3 +1,7 @@ package dev.lonami.talaria.models -data class Dialog(val title: String, val lastMessage: String, val pinned: Boolean) +data class Dialog( + val title: String, + val lastMessage: MessagePreview?, + val pinned: Boolean +) diff --git a/app/src/main/java/dev/lonami/talaria/models/MessageAck.kt b/app/src/main/java/dev/lonami/talaria/models/MessageAck.kt new file mode 100644 index 0000000..fc1a7b8 --- /dev/null +++ b/app/src/main/java/dev/lonami/talaria/models/MessageAck.kt @@ -0,0 +1,7 @@ +package dev.lonami.talaria.models + +enum class MessageAck { + RECEIVED, + SENT, + SEEN, +} diff --git a/app/src/main/java/dev/lonami/talaria/models/MessagePreview.kt b/app/src/main/java/dev/lonami/talaria/models/MessagePreview.kt new file mode 100644 index 0000000..18481db --- /dev/null +++ b/app/src/main/java/dev/lonami/talaria/models/MessagePreview.kt @@ -0,0 +1,10 @@ +package dev.lonami.talaria.models + +import java.time.LocalDateTime + +data class MessagePreview( + val sender: String, + val text: String, + val date: LocalDateTime, + val ack: MessageAck +) diff --git a/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt b/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt index d35598c..3a42cd1 100644 --- a/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt +++ b/app/src/main/java/dev/lonami/talaria/ui/screens/DialogScreen.kt @@ -2,48 +2,101 @@ package dev.lonami.talaria.ui.screens import androidx.compose.foundation.Image import androidx.compose.foundation.clickable -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.Row -import androidx.compose.foundation.layout.fillMaxWidth -import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.Card -import androidx.compose.material.Switch +import androidx.compose.material.Icon +import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.lifecycle.viewmodel.compose.viewModel import dev.lonami.talaria.R import dev.lonami.talaria.data.MockDialogRepository import dev.lonami.talaria.models.Dialog +import dev.lonami.talaria.models.MessageAck import dev.lonami.talaria.ui.state.DialogViewModel import dev.lonami.talaria.ui.theme.TalariaTheme +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle @Composable fun DialogCard(dialog: Dialog, onDialogSelected: () -> Unit) { Card( + shape = MaterialTheme.shapes.large, modifier = Modifier .fillMaxWidth() - .padding(8.dp, 16.dp) + .padding(0.dp, 2.dp) .clickable(onClick = onDialogSelected) ) { - Row { + Row(modifier = Modifier.padding(4.dp)) { Image( painter = painterResource(R.drawable.ic_launcher_foreground), contentDescription = stringResource(R.string.profile_photo), + contentScale = ContentScale.Fit, + modifier = Modifier.height(48.dp), ) Column(modifier = Modifier.weight(1.0f)) { - Text(dialog.title, fontWeight = FontWeight.Bold) - Text(dialog.lastMessage) + Text( + dialog.title, + fontWeight = FontWeight.SemiBold, + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + if (dialog.lastMessage != null) { + Text( + stringResource( + R.string.message_preview, + dialog.lastMessage.sender, + dialog.lastMessage.text + ), + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } + } + Column { + if (dialog.lastMessage != null) { + Row { + when (dialog.lastMessage.ack) { + MessageAck.RECEIVED -> {} + MessageAck.SENT -> Icon( + painterResource(R.drawable.sent), + stringResource(R.string.sent) + ) + MessageAck.SEEN -> Icon( + painterResource(R.drawable.seen), + stringResource(R.string.seen) + ) + } + Spacer(Modifier.width(8.dp)) + Text( + dialog.lastMessage.date.format( + DateTimeFormatter.ofLocalizedTime( + FormatStyle.SHORT + ) + ), + ) + } + } + if (dialog.pinned) { + Icon( + painterResource(R.drawable.tack), + stringResource(R.string.pinned), + modifier = Modifier.align(Alignment.End) + ) + } } - Switch(dialog.pinned, enabled = false, onCheckedChange = null) } } } @@ -53,7 +106,7 @@ fun DialogList(dialogs: List, onDialogSelected: (String) -> Unit) { LazyColumn { items(dialogs.size) { DialogCard(dialogs[it], onDialogSelected = { - onDialogSelected(dialogs[it].lastMessage) + onDialogSelected(dialogs[it].lastMessage!!.text) }) } } diff --git a/app/src/main/java/dev/lonami/talaria/ui/theme/Theme.kt b/app/src/main/java/dev/lonami/talaria/ui/theme/Theme.kt index b1dd9ad..1530a47 100644 --- a/app/src/main/java/dev/lonami/talaria/ui/theme/Theme.kt +++ b/app/src/main/java/dev/lonami/talaria/ui/theme/Theme.kt @@ -41,4 +41,4 @@ fun TalariaTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composabl shapes = Shapes, content = content ) -} \ No newline at end of file +} diff --git a/app/src/main/res/drawable/seen.xml b/app/src/main/res/drawable/seen.xml new file mode 100644 index 0000000..1ef4eac --- /dev/null +++ b/app/src/main/res/drawable/seen.xml @@ -0,0 +1,12 @@ + + + + diff --git a/app/src/main/res/drawable/sent.xml b/app/src/main/res/drawable/sent.xml new file mode 100644 index 0000000..5af82b9 --- /dev/null +++ b/app/src/main/res/drawable/sent.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/tack.xml b/app/src/main/res/drawable/tack.xml new file mode 100644 index 0000000..cd116fc --- /dev/null +++ b/app/src/main/res/drawable/tack.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 01c9a0e..9489681 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,4 +15,8 @@ Back All chats Messages + Pinned + Sent + Seen + %s: %s