Create FakeLastMessageFormatter

This commit is contained in:
Benoit Marty
2023-02-06 21:05:06 +01:00
committed by Benoit Marty
parent 674a813f3b
commit dfeb9d8c7f
5 changed files with 84 additions and 14 deletions

View File

@@ -175,6 +175,7 @@ dependencies {
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.startup)
implementation(libs.coil)
implementation(libs.datetime)
implementation(libs.dagger)
kapt(libs.dagger.compiler)

View File

@@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.features.roomlist
import io.element.android.libraries.dateformatter.LastMessageFormatter
class FakeLastMessageFormatter : LastMessageFormatter {
private var format = ""
fun givenFormat(format: String) {
this.format = format
}
override fun format(timestamp: Long?): String {
return format
}
}

View File

@@ -24,7 +24,7 @@ import app.cash.turbine.test
import com.google.common.truth.Truth.assertThat
import io.element.android.features.roomlist.model.RoomListEvents
import io.element.android.features.roomlist.model.RoomListRoomSummary
import io.element.android.libraries.dateformatter.impl.DefaultLastMessageFormatter
import io.element.android.libraries.dateformatter.LastMessageFormatter
import io.element.android.libraries.designsystem.components.avatar.AvatarData
import io.element.android.libraries.matrix.core.SessionId
import io.element.android.libraries.matrixtest.FakeMatrixClient
@@ -46,7 +46,7 @@ class RoomListPresenterTests {
FakeMatrixClient(
SessionId("sessionId")
),
DefaultLastMessageFormatter()
createDateFormatter()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
@@ -64,7 +64,7 @@ class RoomListPresenterTests {
FakeMatrixClient(
SessionId("sessionId")
),
DefaultLastMessageFormatter()
createDateFormatter()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
@@ -86,7 +86,7 @@ class RoomListPresenterTests {
sessionId = SessionId("sessionId"),
roomSummaryDataSource = roomSummaryDataSource
),
DefaultLastMessageFormatter()
createDateFormatter()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
@@ -112,7 +112,7 @@ class RoomListPresenterTests {
sessionId = SessionId("sessionId"),
roomSummaryDataSource = roomSummaryDataSource
),
DefaultLastMessageFormatter()
createDateFormatter()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
@@ -143,7 +143,7 @@ class RoomListPresenterTests {
sessionId = SessionId("sessionId"),
roomSummaryDataSource = roomSummaryDataSource
),
DefaultLastMessageFormatter()
createDateFormatter()
)
moleculeFlow(RecompositionClock.Immediate) {
presenter.present()
@@ -171,14 +171,22 @@ class RoomListPresenterTests {
assertThat(roomSummaryDataSource.latestSlidingSyncRange).isEqualTo(IntRange(129, 279))
}
}
private fun createDateFormatter(): LastMessageFormatter {
return FakeLastMessageFormatter().apply {
givenFormat(A_FORMATTED_DATE)
}
}
}
private const val A_FORMATTED_DATE = "formatted_date"
private val aRoomListRoomSummary = RoomListRoomSummary(
id = A_ROOM_ID_VALUE,
roomId = A_ROOM_ID,
name = A_ROOM_NAME,
hasUnread = true,
timestamp = "",
timestamp = A_FORMATTED_DATE,
lastMessage = A_LAST_MESSAGE,
avatarData = AvatarData(name = A_ROOM_NAME),
isPlaceholder = false,

View File

@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.element.android.libraries.dateformatter.di
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import io.element.android.libraries.di.AppScope
import kotlinx.datetime.Clock
import java.util.*
@Module
@ContributesTo(AppScope::class)
object DateFormatterModule {
@Provides
fun providesClock(): Clock = Clock.System
@Provides
fun providesLocale(): Locale = Locale.getDefault()
}

View File

@@ -36,13 +36,10 @@ import javax.inject.Inject
import kotlin.math.absoluteValue
@ContributesBinding(AppScope::class)
class DefaultLastMessageFormatter @Inject constructor() : LastMessageFormatter {
// TODO Inject in constructor
private val clock: Clock = Clock.System
// TODO Inject in constructor
private val locale: Locale = Locale.getDefault()
class DefaultLastMessageFormatter @Inject constructor(
private val clock: Clock,
private val locale: Locale,
) : LastMessageFormatter {
private val onlyTimeFormatter: DateTimeFormatter by lazy {
val pattern = DateFormat.getBestDateTimePattern(locale, "HH:mm")
DateTimeFormatter.ofPattern(pattern)