Skip to content

Commit

Permalink
refactor: move Message date formatting to ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Oct 26, 2024
1 parent 76ea419 commit 26ca4af
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/com/geeksville/mesh/model/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/com/geeksville/mesh/model/UIState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand Down Expand Up @@ -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),
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/geeksville/mesh/ui/MessageListView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) },
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/geeksville/mesh/ui/MessagesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 26ca4af

Please sign in to comment.