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.api.DateFormatter
import io.element.android.libraries.dateformatter.test.FakeDateFormatter import io.element.android.libraries.dateformatter.test.FakeDateFormatter
import io.element.android.libraries.eventformatter.api.RoomLatestEventFormatter import io.element.android.libraries.eventformatter.api.RoomLatestEventFormatter
import io.element.android.libraries.eventformatter.test.FakeRoomLatestEventFormatter
fun aRoomListRoomSummaryFactory( fun aRoomListRoomSummaryFactory(
dateFormatter: DateFormatter = FakeDateFormatter { _, _, _ -> "Today" }, dateFormatter: DateFormatter = FakeDateFormatter { _, _, _ -> "Today" },
roomLatestEventFormatter: RoomLatestEventFormatter = RoomLatestEventFormatter { _, _ -> "Hey" } roomLatestEventFormatter: RoomLatestEventFormatter = FakeRoomLatestEventFormatter(),
) = RoomListRoomSummaryFactory( ) = RoomListRoomSummaryFactory(
dateFormatter = dateFormatter, 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 import io.element.android.libraries.matrix.api.roomlist.LatestEventValue
fun interface RoomLatestEventFormatter { interface RoomLatestEventFormatter {
fun format(latestEvent: LatestEventValue, isDmRoom: Boolean): CharSequence? 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 private const val MAX_SAFE_LENGTH = 500
} }
override fun format(latestEvent: LatestEventValue, isDmRoom: Boolean): CharSequence? { override fun format(
return when (latestEvent) { latestEvent: LatestEventValue.Local,
LatestEventValue.None -> null isDmRoom: Boolean,
is LatestEventValue.Local -> formatContent( ): CharSequence? = formatContent(
content = latestEvent.content, content = latestEvent.content,
isDmRoom = isDmRoom, isDmRoom = isDmRoom,
isOutgoing = true, isOutgoing = true,
senderId = latestEvent.senderId, senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId) senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
) )
is LatestEventValue.Remote -> formatContent(
content = latestEvent.content, override fun format(
isDmRoom = isDmRoom, latestEvent: LatestEventValue.Remote,
isOutgoing = latestEvent.isOwn, isDmRoom: Boolean,
senderId = latestEvent.senderId, ): CharSequence? = formatContent(
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId) content = latestEvent.content,
) isDmRoom = isDmRoom,
} isOutgoing = latestEvent.isOwn,
} senderId = latestEvent.senderId,
senderDisambiguatedDisplayName = latestEvent.senderProfile.getDisambiguatedDisplayName(latestEvent.senderId)
)
private fun formatContent( private fun formatContent(
content: EventContent, content: EventContent,

View file

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

View file

@ -14,7 +14,11 @@ import io.element.android.libraries.matrix.api.roomlist.LatestEventValue
class FakeRoomLatestEventFormatter : RoomLatestEventFormatter { class FakeRoomLatestEventFormatter : RoomLatestEventFormatter {
private var result: CharSequence? = null 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 return result
} }