Skip to content

Commit

Permalink
fix: group messages date | design changes (WPB-1733) (#2980)
Browse files Browse the repository at this point in the history
Signed-off-by: alexandreferris <[email protected]>
Co-authored-by: Vitor Hugo Schwaab <[email protected]>
  • Loading branch information
2 people authored and github-actions[bot] committed May 8, 2024
1 parent d76c189 commit eb30213
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1061,11 +1061,12 @@ fun MessageList(

if (index > 0) {
val previousMessage = lazyPagingMessages[index - 1] ?: message
val shouldDisplayDateTimeDivider = message.header.messageTime.shouldDisplayDatesDifferenceDivider(
previousDate = previousMessage.header.messageTime.utcISO
)

val currentGroup = message.header.messageTime.getFormattedDateGroup(now = currentTime)
val previousGroup = previousMessage.header.messageTime.getFormattedDateGroup(now = currentTime)

if (currentGroup != previousGroup) {
if (shouldDisplayDateTimeDivider) {
val previousGroup = previousMessage.header.messageTime.getFormattedDateGroup(now = currentTime)
previousMessage.header.messageTime.utcISO.serverDate()?.let { serverDate ->
MessageGroupDateTime(
messageDateTime = serverDate,
Expand Down Expand Up @@ -1110,6 +1111,19 @@ fun MessageList(
isSelectedMessage = (message.header.messageId == selectedMessageId),
isInteractionAvailable = interactionAvailability == InteractionAvailability.ENABLED
)

val isTheOnlyItem = index == 0 && lazyPagingMessages.itemCount == 1
val isTheLastItem = (index + 1) == lazyPagingMessages.itemCount
if (isTheOnlyItem || isTheLastItem) {
val currentGroup = message.header.messageTime.getFormattedDateGroup(now = currentTime)
message.header.messageTime.utcISO.serverDate()?.let { serverDate ->
MessageGroupDateTime(
messageDateTime = serverDate,
messageDateTimeGroup = currentGroup,
now = currentTime
)
}
}
}
}
JumpToLastMessageButton(lazyListState = lazyListState)
Expand Down Expand Up @@ -1178,6 +1192,10 @@ private fun MessageGroupDateTime(
Row(
Modifier
.fillMaxWidth()
.padding(
top = dimensions().spacing4x,
bottom = dimensions().spacing8x
)
.background(color = colorsScheme().divider)
.padding(
top = dimensions().spacing6x,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.wire.android.ui.theme.Accent
import com.wire.android.util.Copyable
import com.wire.android.util.MessageDateTimeGroup
import com.wire.android.util.groupedUIMessageDateTime
import com.wire.android.util.shouldDisplayDatesDifferenceDivider
import com.wire.android.util.ui.LocalizedStringResource
import com.wire.android.util.ui.UIText
import com.wire.android.util.uiMessageDateTime
Expand Down Expand Up @@ -626,6 +627,8 @@ enum class MessageSource {
data class MessageTime(val utcISO: String) {
val formattedDate: String = utcISO.uiMessageDateTime() ?: ""
fun getFormattedDateGroup(now: Long): MessageDateTimeGroup? = utcISO.groupedUIMessageDateTime(now = now)
fun shouldDisplayDatesDifferenceDivider(previousDate: String): Boolean =
utcISO.shouldDisplayDatesDifferenceDivider(previousDate = previousDate)
}

@Stable
Expand Down
33 changes: 24 additions & 9 deletions app/src/main/kotlin/com/wire/android/util/DateTimeUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

package com.wire.android.util

import android.text.format.DateUtils
import com.wire.android.appLogger
import kotlinx.datetime.Instant
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.LocalDate
import java.time.ZoneId
import java.time.temporal.ChronoUnit
import java.util.Calendar
import java.util.Date
import java.util.Locale
Expand All @@ -43,16 +43,15 @@ private val longDateShortTimeFormat = DateFormat
.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT)
private val mediumOnlyDateTimeFormat = DateFormat
.getDateInstance(DateFormat.MEDIUM)
val messageTimeFormatter = DateFormat
private val messageTimeFormatter = DateFormat
.getTimeInstance(DateFormat.SHORT)
.apply { timeZone = TimeZone.getDefault() }
private val messageDateTimeFormatter = DateFormat
.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT)
.apply { timeZone = TimeZone.getDefault() }
private const val ONE_MINUTE_FROM_MILLIS = 60 * 1000
private const val THIRTY_MINUTES = 30
private const val ONE_WEEK_IN_DAYS = 7
private const val ONE_DAY = 1
private const val FORTY_FIVE_MINUTES_DIFFERENCE = 45
private const val MINIMUM_DAYS_DIFFERENCE = 1

private val readReceiptDateTimeFormat = SimpleDateFormat(
"MMM dd yyyy, hh:mm a",
Expand Down Expand Up @@ -181,12 +180,28 @@ sealed interface MessageDateTimeGroup {

fun String.uiMessageDateTime(): String? = this
.serverDate()?.let { serverDate ->
when (DateUtils.isToday(serverDate.time)) {
true -> messageTimeFormatter.format(serverDate)
false -> messageDateTimeFormatter.format(serverDate)
}
messageTimeFormatter.format(serverDate)
}

fun String.shouldDisplayDatesDifferenceDivider(previousDate: String): Boolean {
val currentDate = this@shouldDisplayDatesDifferenceDivider

val currentLocalDateTime = currentDate.serverDate()?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDateTime()
val previousLocalDateTime = previousDate.serverDate()?.toInstant()?.atZone(ZoneId.systemDefault())?.toLocalDateTime()

val differenceInMinutes = ChronoUnit.MINUTES.between(
currentLocalDateTime,
previousLocalDateTime
)

val differenceInDays = ChronoUnit.DAYS.between(
currentLocalDateTime,
previousLocalDateTime
)

return differenceInMinutes > FORTY_FIVE_MINUTES_DIFFERENCE || differenceInDays >= MINIMUM_DAYS_DIFFERENCE
}

fun String.groupedUIMessageDateTime(now: Long): MessageDateTimeGroup? = this
.serverDate()?.let { serverDate ->
val localDate = serverDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
Expand Down

0 comments on commit eb30213

Please sign in to comment.