Skip to content

Commit

Permalink
Refactor default data separator handle to add data when messages are …
Browse files Browse the repository at this point in the history
…from different days
  • Loading branch information
JcMinarro committed Nov 5, 2024
1 parent 6846c1f commit 7e2d345
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ public abstract interface class io/getstream/chat/android/ui/common/feature/mess
}

public final class io/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler$Companion {
public final fun getDefaultDateSeparatorHandler (J)Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
public static synthetic fun getDefaultDateSeparatorHandler$default (Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler$Companion;JILjava/lang/Object;)Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
public final fun getDefaultThreadDateSeparatorHandler (J)Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
public static synthetic fun getDefaultThreadDateSeparatorHandler$default (Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler$Companion;JILjava/lang/Object;)Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
public final fun getDefaultDateSeparatorHandler ()Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
public final fun getDefaultThreadDateSeparatorHandler ()Lio/getstream/chat/android/ui/common/feature/messages/list/DateSeparatorHandler;
}

public final class io/getstream/chat/android/ui/common/feature/messages/list/MessageListController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import io.getstream.chat.android.client.extensions.getCreatedAtOrDefault
import io.getstream.chat.android.client.extensions.getCreatedAtOrNull
import io.getstream.chat.android.client.extensions.internal.NEVER
import io.getstream.chat.android.models.Message
import java.util.Calendar
import java.util.Date

/**
* A SAM designed to evaluate if a date separator should be added between messages.
Expand All @@ -38,60 +40,37 @@ public fun interface DateSeparatorHandler {

public companion object {

private val defaultDateSeparatorHandler = DateSeparatorHandler { previousMessage, message ->
!message.getCreatedAtOrDefault(NEVER).isInTheSameDay(
previousMessage?.getCreatedAtOrNull() ?: NEVER,
)
}

/**
* @param separatorTimeMillis Time difference between two message after which we add the date separator.
* Creates a [DateSeparatorHandler] returning true if the messages are not in the same day.
*
* @return The default normal list date separator handler.
*/
public fun getDefaultDateSeparatorHandler(
separatorTimeMillis: Long = DateSeparatorDefaultHourThreshold,
): DateSeparatorHandler = DateSeparatorHandler { previousMessage, message ->
if (previousMessage == null) {
true
} else {
shouldAddDateSeparator(previousMessage, message, separatorTimeMillis)
}
}
public fun getDefaultDateSeparatorHandler(): DateSeparatorHandler = defaultDateSeparatorHandler

/**
* @param separatorTimeMillis Time difference between two message after which we add the date separator.
* Creates a [DateSeparatorHandler] returning true if the messages are not in the same day.
*
* @return The default thread date separator handler.
*/
public fun getDefaultThreadDateSeparatorHandler(
separatorTimeMillis: Long = DateSeparatorDefaultHourThreshold,
): DateSeparatorHandler = DateSeparatorHandler { previousMessage, message ->
if (previousMessage == null) {
false
} else {
shouldAddDateSeparator(previousMessage, message, separatorTimeMillis)
}
}
public fun getDefaultThreadDateSeparatorHandler(): DateSeparatorHandler = defaultDateSeparatorHandler

/**
* @param previousMessage The [Message] before the one we are currently evaluating.
* @param message The [Message] before which we want to add a date separator or not.
* Checks if the two dates are in the same day.
*
* @return Whether to add the date separator or not depending on the time difference.
* @param that The date to compare with.
* @return True if the two dates are in the same day, false otherwise.
*/
private fun shouldAddDateSeparator(
previousMessage: Message?,
message: Message,
separatorTimeMillis: Long,
): Boolean {
return (
message.getCreatedAtOrDefault(NEVER).time - (
previousMessage?.getCreatedAtOrNull()?.time
?: NEVER.time
)
) >
separatorTimeMillis
private fun Date.isInTheSameDay(that: Date): Boolean {
val thisCalendar = Calendar.getInstance().apply { time = this@isInTheSameDay }
val thatCalendar = Calendar.getInstance().apply { time = that }
return thisCalendar.get(Calendar.DAY_OF_YEAR) == thatCalendar.get(Calendar.DAY_OF_YEAR) &&
thisCalendar.get(Calendar.YEAR) == thatCalendar.get(Calendar.YEAR)
}

/**
* The default threshold for showing date separators. If the message difference in millis is equal to this
* number, then we show a separator, if it's enabled in the list.
*/
private const val DateSeparatorDefaultHourThreshold: Long = 4 * 60 * 60 * 1000
}
}

0 comments on commit 7e2d345

Please sign in to comment.