mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: move Message date formatting to ViewModel
This commit is contained in:
parent
76ea419313
commit
26ca4af568
4 changed files with 18 additions and 20 deletions
|
|
@ -31,7 +31,7 @@ data class Message(
|
|||
val receivedTime: Long,
|
||||
val user: MeshProtos.User,
|
||||
val text: String,
|
||||
val time: Long,
|
||||
val time: String,
|
||||
val read: Boolean,
|
||||
val status: MessageStatus?,
|
||||
val routingError: Int,
|
||||
|
|
|
|||
|
|
@ -143,8 +143,8 @@ data class Contact(
|
|||
)
|
||||
|
||||
// return time if within 24 hours, otherwise date
|
||||
internal fun getShortDateTime(time: Long): String? {
|
||||
val date = if (time != 0L) Date(time) else return null
|
||||
private fun getShortDate(time: Long): String {
|
||||
val date = Date(time)
|
||||
val isWithin24Hours = System.currentTimeMillis() - date.time <= TimeUnit.DAYS.toMillis(1)
|
||||
|
||||
return if (isWithin24Hours) {
|
||||
|
|
@ -154,6 +154,18 @@ internal fun getShortDateTime(time: Long): String? {
|
|||
}
|
||||
}
|
||||
|
||||
// return time if within 24 hours, otherwise date/time
|
||||
private fun getShortDateTime(time: Long): String {
|
||||
val date = Date(time)
|
||||
val isWithin24Hours = System.currentTimeMillis() - date.time <= TimeUnit.DAYS.toMillis(1)
|
||||
|
||||
return if (isWithin24Hours) {
|
||||
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
|
||||
} else {
|
||||
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
@HiltViewModel
|
||||
class UIViewModel @Inject constructor(
|
||||
|
|
@ -316,7 +328,7 @@ class UIViewModel @Inject constructor(
|
|||
contactKey = contactKey,
|
||||
shortName = if (toBroadcast) "${data.channel}" else shortName,
|
||||
longName = longName,
|
||||
lastMessageTime = getShortDateTime(data.time),
|
||||
lastMessageTime = getShortDate(data.time),
|
||||
lastMessageText = if (fromLocal) data.text else "$shortName: ${data.text}",
|
||||
unreadCount = packetRepository.getUnreadCount(contactKey),
|
||||
messageCount = packetRepository.getMessageCount(contactKey),
|
||||
|
|
@ -337,7 +349,7 @@ class UIViewModel @Inject constructor(
|
|||
receivedTime = it.received_time,
|
||||
user = getUser(it.data.from),
|
||||
text = it.data.text.orEmpty(),
|
||||
time = it.data.time,
|
||||
time = getShortDateTime(it.data.time),
|
||||
read = it.read,
|
||||
status = it.data.status,
|
||||
routingError = it.routingError,
|
||||
|
|
|
|||
|
|
@ -20,7 +20,6 @@ import com.geeksville.mesh.ui.components.SimpleAlertDialog
|
|||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.debounce
|
||||
import java.util.Date
|
||||
|
||||
@Composable
|
||||
internal fun MessageListView(
|
||||
|
|
@ -56,7 +55,7 @@ internal fun MessageListView(
|
|||
MessageItem(
|
||||
shortName = msg.user.shortName.takeIf { msg.user.id != DataPacket.ID_LOCAL },
|
||||
messageText = msg.text,
|
||||
messageTime = getShortDateTime(Date(msg.time)),
|
||||
messageTime = msg.time,
|
||||
messageStatus = msg.status,
|
||||
selected = selected,
|
||||
onClick = { onClick(msg) },
|
||||
|
|
|
|||
|
|
@ -36,19 +36,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.DateFormat
|
||||
import java.util.Date
|
||||
|
||||
// return time if within 24 hours, otherwise date/time
|
||||
internal fun getShortDateTime(date: Date): String {
|
||||
val isWithin24Hours = System.currentTimeMillis() - date.time <= 24 * 60 * 60 * 1000L
|
||||
|
||||
return if (isWithin24Hours) {
|
||||
DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
|
||||
} else {
|
||||
DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun FragmentManager.navigateToMessages(contactKey: String, contactName: String) {
|
||||
val messagesFragment = MessagesFragment().apply {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue