Create module dateformatter

This commit is contained in:
Benoit Marty
2023-02-06 18:09:18 +01:00
committed by Benoit Marty
parent 2516d0cae7
commit 674a813f3b
12 changed files with 119 additions and 10 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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()

1
libraries/dateformatter/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

View File

@@ -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)
}
}

View File

@@ -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

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ 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.
-->
<manifest />

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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"))
}

View File

@@ -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")