diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f5a0799676..4aa95c2f7c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -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) diff --git a/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/FakeLastMessageFormatter.kt b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/FakeLastMessageFormatter.kt new file mode 100644 index 0000000000..997846056a --- /dev/null +++ b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/FakeLastMessageFormatter.kt @@ -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 + } +} diff --git a/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt index c1c2e8b141..5fd2da920b 100644 --- a/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt +++ b/features/roomlist/src/test/kotlin/io/element/android/features/roomlist/RoomListPresenterTests.kt @@ -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, diff --git a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt new file mode 100644 index 0000000000..85deac5274 --- /dev/null +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/di/DateFormatterModule.kt @@ -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() +} diff --git a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt index 13ffb9c7e0..28d76551ae 100644 --- a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt @@ -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)