diff --git a/features/roomlist/build.gradle.kts b/features/roomlist/build.gradle.kts index de67bb010f..fe8112fb81 100644 --- a/features/roomlist/build.gradle.kts +++ b/features/roomlist/build.gradle.kts @@ -41,7 +41,7 @@ dependencies { implementation(projects.libraries.designsystem) implementation(projects.libraries.elementresources) implementation(projects.libraries.uiStrings) - implementation(libs.datetime) + implementation(projects.libraries.dateformatter) implementation(libs.accompanist.placeholder) testImplementation(libs.test.junit) diff --git a/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt b/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt index c684b0dd45..26d3bfd4ae 100644 --- a/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt +++ b/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/RoomListPresenter.kt @@ -31,6 +31,7 @@ import io.element.android.features.roomlist.model.RoomListRoomSummaryPlaceholder import io.element.android.features.roomlist.model.RoomListState import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.core.coroutine.parallelMap +import io.element.android.libraries.dateformatter.LastMessageFormatter import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.designsystem.components.avatar.AvatarSize import io.element.android.libraries.matrix.MatrixClient 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 7af95b0196..c1c2e8b141 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,6 +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.designsystem.components.avatar.AvatarData import io.element.android.libraries.matrix.core.SessionId import io.element.android.libraries.matrixtest.FakeMatrixClient @@ -45,7 +46,7 @@ class RoomListPresenterTests { FakeMatrixClient( SessionId("sessionId") ), - LastMessageFormatter() + DefaultLastMessageFormatter() ) moleculeFlow(RecompositionClock.Immediate) { presenter.present() @@ -63,7 +64,7 @@ class RoomListPresenterTests { FakeMatrixClient( SessionId("sessionId") ), - LastMessageFormatter() + DefaultLastMessageFormatter() ) moleculeFlow(RecompositionClock.Immediate) { presenter.present() @@ -85,7 +86,7 @@ class RoomListPresenterTests { sessionId = SessionId("sessionId"), roomSummaryDataSource = roomSummaryDataSource ), - LastMessageFormatter() + DefaultLastMessageFormatter() ) moleculeFlow(RecompositionClock.Immediate) { presenter.present() @@ -111,7 +112,7 @@ class RoomListPresenterTests { sessionId = SessionId("sessionId"), roomSummaryDataSource = roomSummaryDataSource ), - LastMessageFormatter() + DefaultLastMessageFormatter() ) moleculeFlow(RecompositionClock.Immediate) { presenter.present() @@ -142,7 +143,7 @@ class RoomListPresenterTests { sessionId = SessionId("sessionId"), roomSummaryDataSource = roomSummaryDataSource ), - LastMessageFormatter() + DefaultLastMessageFormatter() ) moleculeFlow(RecompositionClock.Immediate) { presenter.present() diff --git a/libraries/dateformatter/.gitignore b/libraries/dateformatter/.gitignore new file mode 100644 index 0000000000..42afabfd2a --- /dev/null +++ b/libraries/dateformatter/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/libraries/dateformatter/build.gradle.kts b/libraries/dateformatter/build.gradle.kts new file mode 100644 index 0000000000..ef5b1fc980 --- /dev/null +++ b/libraries/dateformatter/build.gradle.kts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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. + */ + +// TODO: Remove once https://youtrack.jetbrains.com/issue/KTIJ-19369 is fixed +@Suppress("DSL_SCOPE_VIOLATION") +plugins { + id("io.element.android-library") + alias(libs.plugins.ksp) + alias(libs.plugins.anvil) +} + +anvil { + generateDaggerFactories.set(true) +} + +android { + namespace = "io.element.android.libraries.dateformatter" + + dependencies { + anvil(projects.anvilcodegen) + implementation(libs.dagger) + implementation(projects.libraries.di) + implementation(projects.anvilannotations) + implementation(libs.datetime) + ksp(libs.showkase.processor) + } +} diff --git a/libraries/dateformatter/consumer-rules.pro b/libraries/dateformatter/consumer-rules.pro new file mode 100644 index 0000000000..e69de29bb2 diff --git a/libraries/dateformatter/proguard-rules.pro b/libraries/dateformatter/proguard-rules.pro new file mode 100644 index 0000000000..ff59496d81 --- /dev/null +++ b/libraries/dateformatter/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle.kts. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/libraries/dateformatter/src/main/AndroidManifest.xml b/libraries/dateformatter/src/main/AndroidManifest.xml new file mode 100644 index 0000000000..cf0e6386de --- /dev/null +++ b/libraries/dateformatter/src/main/AndroidManifest.xml @@ -0,0 +1,16 @@ + + diff --git a/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/LastMessageFormatter.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/LastMessageFormatter.kt new file mode 100644 index 0000000000..caa5886cf9 --- /dev/null +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/LastMessageFormatter.kt @@ -0,0 +1,21 @@ +/* + * 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 + +interface LastMessageFormatter { + fun format(timestamp: Long?): String +} diff --git a/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/LastMessageFormatter.kt b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt similarity index 87% rename from features/roomlist/src/main/kotlin/io/element/android/features/roomlist/LastMessageFormatter.kt rename to libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt index 037ba5200d..13ffb9c7e0 100644 --- a/features/roomlist/src/main/kotlin/io/element/android/features/roomlist/LastMessageFormatter.kt +++ b/libraries/dateformatter/src/main/kotlin/io/element/android/libraries/dateformatter/impl/DefaultLastMessageFormatter.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 New Vector Ltd + * 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. @@ -14,10 +14,13 @@ * limitations under the License. */ -package io.element.android.features.roomlist +package io.element.android.libraries.dateformatter.impl import android.text.format.DateFormat import android.text.format.DateUtils +import com.squareup.anvil.annotations.ContributesBinding +import io.element.android.libraries.dateformatter.LastMessageFormatter +import io.element.android.libraries.di.AppScope import kotlinx.datetime.Clock import kotlinx.datetime.Instant import kotlinx.datetime.LocalDateTime @@ -32,9 +35,12 @@ import java.util.Locale import javax.inject.Inject import kotlin.math.absoluteValue -class LastMessageFormatter @Inject constructor() { +@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() private val onlyTimeFormatter: DateTimeFormatter by lazy { @@ -52,7 +58,7 @@ class LastMessageFormatter @Inject constructor() { DateTimeFormatter.ofPattern(pattern) } - fun format(timestamp: Long?): String { + override fun format(timestamp: Long?): String { if (timestamp == null) return "" val now: Instant = clock.now() val tsInstant = Instant.fromEpochMilliseconds(timestamp) diff --git a/plugins/src/main/kotlin/extension/DependencyHandleScope.kt b/plugins/src/main/kotlin/extension/DependencyHandleScope.kt index 710f603cad..8b63c38244 100644 --- a/plugins/src/main/kotlin/extension/DependencyHandleScope.kt +++ b/plugins/src/main/kotlin/extension/DependencyHandleScope.kt @@ -54,6 +54,7 @@ fun DependencyHandlerScope.allLibraries() { implementation(project(":libraries:matrixui")) implementation(project(":libraries:core")) implementation(project(":libraries:architecture")) + implementation(project(":libraries:dateformatter")) implementation(project(":libraries:di")) } diff --git a/settings.gradle.kts b/settings.gradle.kts index b85f5e6717..0c2cf97f2f 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -42,6 +42,7 @@ include(":libraries:rustsdk") include(":libraries:matrix") include(":libraries:matrixui") include(":libraries:textcomposer") +include(":libraries:dateformatter") include(":libraries:elementresources") include(":libraries:ui-strings") include(":libraries:testtags")