Update API of RoomLatestEventFormatter.

This commit is contained in:
Benoit Marty
2025-12-02 16:51:23 +01:00
parent 4324f4ae67
commit f5d902a6f3
5 changed files with 33 additions and 25 deletions

View File

@@ -11,11 +11,12 @@ package io.element.android.features.home.impl.datasource
import io.element.android.libraries.dateformatter.api.DateFormatter
import io.element.android.libraries.dateformatter.test.FakeDateFormatter
import io.element.android.libraries.eventformatter.api.RoomLatestEventFormatter
import io.element.android.libraries.eventformatter.test.FakeRoomLatestEventFormatter
fun aRoomListRoomSummaryFactory(
dateFormatter: DateFormatter = FakeDateFormatter { _, _, _ -> "Today" },
roomLatestEventFormatter: RoomLatestEventFormatter = RoomLatestEventFormatter { _, _ -> "Hey" }
roomLatestEventFormatter: RoomLatestEventFormatter = FakeRoomLatestEventFormatter(),
) = RoomListRoomSummaryFactory(
dateFormatter = dateFormatter,
roomLatestEventFormatter = roomLatestEventFormatter
roomLatestEventFormatter = roomLatestEventFormatter,
)

View File

@@ -10,6 +10,7 @@ package io.element.android.libraries.eventformatter.api
import io.element.android.libraries.matrix.api.roomlist.LatestEventValue
fun interface RoomLatestEventFormatter {
fun format(latestEvent: LatestEventValue, isDmRoom: Boolean): CharSequence?
interface RoomLatestEventFormatter {
fun format(latestEvent: LatestEventValue.Local, isDmRoom: Boolean): CharSequence?
fun format(latestEvent: LatestEventValue.Remote, isDmRoom: Boolean): CharSequence?
}

View File

@@ -59,25 +59,27 @@ class DefaultRoomLatestEventFormatter(
private const val MAX_SAFE_LENGTH = 500
}
override fun format(latestEvent: LatestEventValue, isDmRoom: Boolean): CharSequence? {
return when (latestEvent) {
LatestEventValue.None -> null
is LatestEventValue.Local -> formatContent(
content = latestEvent.content,
isDmRoom = isDmRoom,
isOutgoing = true,
senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
)
is LatestEventValue.Remote -> formatContent(
content = latestEvent.content,
isDmRoom = isDmRoom,
isOutgoing = latestEvent.isOwn,
senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
)
}
}
override fun format(
latestEvent: LatestEventValue.Local,
isDmRoom: Boolean,
): CharSequence? = formatContent(
content = latestEvent.content,
isDmRoom = isDmRoom,
isOutgoing = true,
senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
)
override fun format(
latestEvent: LatestEventValue.Remote,
isDmRoom: Boolean,
): CharSequence? = formatContent(
content = latestEvent.content,
isDmRoom = isDmRoom,
isOutgoing = latestEvent.isOwn,
senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
)
private fun formatContent(
content: EventContent,

View File

@@ -929,7 +929,7 @@ class DefaultRoomLatestEventFormatterTest {
sentByYou: Boolean,
senderDisplayName: String?,
content: EventContent,
): LatestEventValue {
): LatestEventValue.Remote {
val sender = if (sentByYou) A_USER_ID else someoneElseId
val profile = aProfileDetails(senderDisplayName)
return aRemoteLatestEvent(

View File

@@ -14,7 +14,11 @@ import io.element.android.libraries.matrix.api.roomlist.LatestEventValue
class FakeRoomLatestEventFormatter : RoomLatestEventFormatter {
private var result: CharSequence? = null
override fun format(latestEvent: LatestEventValue, isDmRoom: Boolean): CharSequence? {
override fun format(latestEvent: LatestEventValue.Local, isDmRoom: Boolean): CharSequence? {
return result
}
override fun format(latestEvent: LatestEventValue.Remote, isDmRoom: Boolean): CharSequence? {
return result
}