Implement month separator for the Gallery.
Improve day separator rendering in the timeline. Use Today, Yesterday, and the name of the day if less than 7 days and do not render the year for the current year. Improve date format for the media viewer. Rework how date and time are computed. ActionListView: Time can take more space, so update the layout.
This commit is contained in:
committed by
Benoit Marty
parent
91444aee67
commit
4188d58b56
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.api
|
||||
|
||||
interface DateFormatter {
|
||||
fun format(
|
||||
timestamp: Long?,
|
||||
mode: DateFormatterMode = DateFormatterMode.Full,
|
||||
useRelative: Boolean = false,
|
||||
): String
|
||||
}
|
||||
|
||||
enum class DateFormatterMode {
|
||||
Full,
|
||||
Month,
|
||||
Day,
|
||||
// Time if same day, else date
|
||||
TimeOrDate,
|
||||
// Only time whatever the day
|
||||
TimeOnly,
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.api
|
||||
|
||||
interface DaySeparatorFormatter {
|
||||
fun format(timestamp: Long): String
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.api
|
||||
|
||||
fun interface LastMessageTimestampFormatter {
|
||||
fun format(timestamp: Long?): String
|
||||
}
|
||||
@@ -16,15 +16,29 @@ setupAnvil()
|
||||
android {
|
||||
namespace = "io.element.android.libraries.dateformatter.impl"
|
||||
|
||||
testOptions {
|
||||
unitTests {
|
||||
isIncludeAndroidResources = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.dagger)
|
||||
implementation(projects.libraries.core)
|
||||
implementation(projects.libraries.di)
|
||||
implementation(projects.libraries.uiStrings)
|
||||
implementation(projects.services.toolbox.api)
|
||||
|
||||
api(projects.libraries.dateformatter.api)
|
||||
api(libs.datetime)
|
||||
|
||||
testImplementation(libs.test.junit)
|
||||
testImplementation(libs.test.truth)
|
||||
testImplementation(libs.test.turbine)
|
||||
testImplementation(libs.test.robolectric)
|
||||
testImplementation(projects.libraries.dateformatter.test)
|
||||
testImplementation(projects.services.toolbox.test)
|
||||
testImplementation(projects.tests.testutils)
|
||||
testImplementation(libs.androidx.compose.ui.test.junit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.core.extensions.safeCapitalize
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
interface DateFormatterDay {
|
||||
fun format(
|
||||
timestamp: Long,
|
||||
useRelative: Boolean,
|
||||
): String
|
||||
}
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultDateFormatterDay @Inject constructor(
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
) : DateFormatterDay {
|
||||
override fun format(
|
||||
timestamp: Long,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
val today = localDateTimeProvider.providesNow()
|
||||
return if (useRelative) {
|
||||
val dayDiff = today.date.toEpochDays() - dateToFormat.date.toEpochDays()
|
||||
when (dayDiff) {
|
||||
0 -> dateFormatters.getRelativeDay(timestamp, "Today")
|
||||
1 -> dateFormatters.getRelativeDay(timestamp, "Yesterday")
|
||||
else -> if (dayDiff < 7) {
|
||||
dateFormatters.formatDateWithDay(dateToFormat)
|
||||
} else {
|
||||
if (today.year == dateToFormat.year) {
|
||||
dateFormatters.formatDateWithFullFormatNoYear(dateToFormat)
|
||||
} else {
|
||||
dateFormatters.formatDateWithFullFormat(dateToFormat)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (today.year == dateToFormat.year) {
|
||||
dateFormatters.formatDateWithFullFormatNoYear(dateToFormat)
|
||||
} else {
|
||||
dateFormatters.formatDateWithFullFormat(dateToFormat)
|
||||
}
|
||||
}
|
||||
.safeCapitalize()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import io.element.android.services.toolbox.api.strings.StringProvider
|
||||
import javax.inject.Inject
|
||||
|
||||
class DateFormatterFull @Inject constructor(
|
||||
private val stringProvider: StringProvider,
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
private val dateFormatterDay: DateFormatterDay,
|
||||
) {
|
||||
fun format(
|
||||
timestamp: Long,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
val time = dateFormatters.formatTime(dateToFormat)
|
||||
return if (useRelative) {
|
||||
val now = localDateTimeProvider.providesNow()
|
||||
if (now.date == dateToFormat.date) {
|
||||
time
|
||||
} else {
|
||||
val dateStr = dateFormatterDay.format(timestamp, true)
|
||||
stringProvider.getString(R.string.common_date_date_at_time, dateStr, time)
|
||||
}
|
||||
} else {
|
||||
val dateStr = dateFormatters.formatDateWithFullFormat(dateToFormat)
|
||||
stringProvider.getString(R.string.common_date_date_at_time, dateStr, time)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import io.element.android.libraries.core.extensions.safeCapitalize
|
||||
import io.element.android.services.toolbox.api.strings.StringProvider
|
||||
import javax.inject.Inject
|
||||
|
||||
class DateFormatterMonth @Inject constructor(
|
||||
private val stringProvider: StringProvider,
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
) {
|
||||
fun format(
|
||||
timestamp: Long,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
val today = localDateTimeProvider.providesNow()
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
return if (useRelative && dateToFormat.month == today.month && dateToFormat.year == today.year) {
|
||||
stringProvider.getString(R.string.common_date_this_month)
|
||||
} else {
|
||||
dateFormatters.formatDateWithMonthAndYear(dateToFormat)
|
||||
}
|
||||
.safeCapitalize()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
@@ -7,18 +7,16 @@
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.dateformatter.api.LastMessageTimestampFormatter
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultLastMessageTimestampFormatter @Inject constructor(
|
||||
class DateFormatterTime @Inject constructor(
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
) : LastMessageTimestampFormatter {
|
||||
override fun format(timestamp: Long?): String {
|
||||
if (timestamp == null) return ""
|
||||
) {
|
||||
fun format(
|
||||
timestamp: Long,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
val currentDate = localDateTimeProvider.providesNow()
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
val isSameDay = currentDate.date == dateToFormat.date
|
||||
@@ -30,7 +28,7 @@ class DefaultLastMessageTimestampFormatter @Inject constructor(
|
||||
dateFormatters.formatDate(
|
||||
dateToFormat = dateToFormat,
|
||||
currentDate = currentDate,
|
||||
useRelative = true
|
||||
useRelative = useRelative,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import javax.inject.Inject
|
||||
|
||||
class DateFormatterTimeOnly @Inject constructor(
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
) {
|
||||
fun format(
|
||||
timestamp: Long,
|
||||
): String {
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
return dateFormatters.formatTime(dateToFormat)
|
||||
}
|
||||
}
|
||||
@@ -7,57 +7,63 @@
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import android.text.format.DateFormat
|
||||
import android.text.format.DateUtils
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.toInstant
|
||||
import kotlinx.datetime.toJavaLocalDate
|
||||
import kotlinx.datetime.toJavaLocalDateTime
|
||||
import timber.log.Timber
|
||||
import java.time.Period
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
import kotlin.math.absoluteValue
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
class DateFormatters @Inject constructor(
|
||||
private val locale: Locale,
|
||||
localeChangeObserver: LocaleChangeObserver,
|
||||
private val clock: Clock,
|
||||
private val timeZoneProvider: TimezoneProvider,
|
||||
) {
|
||||
private val onlyTimeFormatter: DateTimeFormatter by lazy {
|
||||
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
|
||||
) : LocaleChangeListener {
|
||||
init {
|
||||
localeChangeObserver.addListener(this)
|
||||
}
|
||||
|
||||
private val dateWithMonthFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = DateFormat.getBestDateTimePattern(locale, "d MMM") ?: "d MMM"
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
private var dateTimeFormatters: DateTimeFormatters = DateTimeFormatters(Locale.getDefault())
|
||||
|
||||
private val dateWithYearFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = DateFormat.getBestDateTimePattern(locale, "dd.MM.yyyy") ?: "dd.MM.yyyy"
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
private val dateWithFullFormatFormatter: DateTimeFormatter by lazy {
|
||||
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(locale)
|
||||
override fun onLocaleChange() {
|
||||
Timber.w("Locale changed, updating formatters")
|
||||
dateTimeFormatters = DateTimeFormatters(Locale.getDefault())
|
||||
}
|
||||
|
||||
internal fun formatTime(localDateTime: LocalDateTime): String {
|
||||
return onlyTimeFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
return dateTimeFormatters.onlyTimeFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithMonthAndYear(localDateTime: LocalDateTime): String {
|
||||
return dateTimeFormatters.dateWithMonthAndYearFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithMonth(localDateTime: LocalDateTime): String {
|
||||
return dateWithMonthFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
return dateTimeFormatters.dateWithMonthFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithDay(localDateTime: LocalDateTime): String {
|
||||
return dateTimeFormatters.dateWithDayFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithYear(localDateTime: LocalDateTime): String {
|
||||
return dateWithYearFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
return dateTimeFormatters.dateWithYearFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithFullFormat(localDateTime: LocalDateTime): String {
|
||||
return dateWithFullFormatFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
return dateTimeFormatters.dateWithFullFormatFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDateWithFullFormatNoYear(localDateTime: LocalDateTime): String {
|
||||
return dateTimeFormatters.dateWithFullFormatNoYearFormatter.format(localDateTime.toJavaLocalDateTime())
|
||||
}
|
||||
|
||||
internal fun formatDate(
|
||||
@@ -75,12 +81,12 @@ class DateFormatters @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun getRelativeDay(ts: Long): String {
|
||||
internal fun getRelativeDay(ts: Long, default: String = ""): String {
|
||||
return DateUtils.getRelativeTimeSpanString(
|
||||
ts,
|
||||
clock.now().toEpochMilliseconds(),
|
||||
DateUtils.DAY_IN_MILLIS,
|
||||
DateUtils.FORMAT_SHOW_WEEKDAY
|
||||
)?.toString() ?: ""
|
||||
)?.toString() ?: default
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import android.text.format.DateFormat
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
import java.util.Locale
|
||||
|
||||
class DateTimeFormatters(
|
||||
private val locale: Locale,
|
||||
) {
|
||||
val onlyTimeFormatter: DateTimeFormatter by lazy {
|
||||
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).withLocale(locale)
|
||||
}
|
||||
|
||||
val dateWithMonthAndYearFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = bestDateTimePattern("MMMM YYYY")
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
val dateWithMonthFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = bestDateTimePattern("d MMM")
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
val dateWithDayFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = bestDateTimePattern("EEEE")
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
val dateWithYearFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = bestDateTimePattern("dd.MM.yyyy")
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
val dateWithFullFormatFormatter: DateTimeFormatter by lazy {
|
||||
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(locale)
|
||||
}
|
||||
|
||||
val dateWithFullFormatNoYearFormatter: DateTimeFormatter by lazy {
|
||||
val pattern = DateFormat.getBestDateTimePattern(locale, "EEEE d MMMM") ?: "EEEE d MMMM"
|
||||
DateTimeFormatter.ofPattern(pattern, locale)
|
||||
}
|
||||
|
||||
private fun bestDateTimePattern(pattern: String): String {
|
||||
return DateFormat.getBestDateTimePattern(locale, pattern) ?: pattern
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatter
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatterMode
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultDateFormatter @Inject constructor(
|
||||
private val dateFormatterFull: DateFormatterFull,
|
||||
private val dateFormatterMonth: DateFormatterMonth,
|
||||
private val dateFormatterDay: DateFormatterDay,
|
||||
private val dateFormatterTime: DateFormatterTime,
|
||||
private val dateFormatterTimeOnly: DateFormatterTimeOnly,
|
||||
) : DateFormatter {
|
||||
override fun format(
|
||||
timestamp: Long?,
|
||||
mode: DateFormatterMode,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
timestamp ?: return ""
|
||||
return when (mode) {
|
||||
DateFormatterMode.Full -> {
|
||||
dateFormatterFull.format(timestamp, useRelative)
|
||||
}
|
||||
DateFormatterMode.Month -> {
|
||||
dateFormatterMonth.format(timestamp, useRelative)
|
||||
}
|
||||
DateFormatterMode.Day -> {
|
||||
dateFormatterDay.format(timestamp, useRelative)
|
||||
}
|
||||
DateFormatterMode.TimeOrDate -> {
|
||||
dateFormatterTime.format(timestamp, useRelative)
|
||||
}
|
||||
DateFormatterMode.TimeOnly -> {
|
||||
dateFormatterTimeOnly.format(timestamp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.dateformatter.api.DaySeparatorFormatter
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import javax.inject.Inject
|
||||
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultDaySeparatorFormatter @Inject constructor(
|
||||
private val localDateTimeProvider: LocalDateTimeProvider,
|
||||
private val dateFormatters: DateFormatters,
|
||||
) : DaySeparatorFormatter {
|
||||
override fun format(timestamp: Long): String {
|
||||
val dateToFormat = localDateTimeProvider.providesFromTimestamp(timestamp)
|
||||
// TODO use relative formatting once iOS uses it too
|
||||
return dateFormatters.formatDateWithFullFormat(dateToFormat)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import io.element.android.libraries.di.ApplicationContext
|
||||
import io.element.android.libraries.di.SingleIn
|
||||
import javax.inject.Inject
|
||||
|
||||
fun interface LocaleChangeObserver {
|
||||
fun addListener(listener: LocaleChangeListener)
|
||||
}
|
||||
|
||||
interface LocaleChangeListener {
|
||||
fun onLocaleChange()
|
||||
}
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultLocaleChangeObserver @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
) : LocaleChangeObserver {
|
||||
init {
|
||||
registerReceiver(object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
listeners.forEach(LocaleChangeListener::onLocaleChange)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private val listeners = mutableSetOf<LocaleChangeListener>()
|
||||
|
||||
override fun addListener(listener: LocaleChangeListener) {
|
||||
listeners.add(listener)
|
||||
}
|
||||
|
||||
private fun registerReceiver(receiver: BroadcastReceiver) {
|
||||
val filter = IntentFilter()
|
||||
filter.addAction(Intent.ACTION_LOCALE_CHANGED)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
filter.addAction(Intent.ACTION_APPLICATION_LOCALE_CHANGED)
|
||||
}
|
||||
context.registerReceiver(receiver, filter)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@ import io.element.android.libraries.dateformatter.impl.TimezoneProvider
|
||||
import io.element.android.libraries.di.AppScope
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.TimeZone
|
||||
import java.util.Locale
|
||||
|
||||
@Module
|
||||
@ContributesTo(AppScope::class)
|
||||
@@ -22,9 +21,6 @@ object DateFormatterModule {
|
||||
@Provides
|
||||
fun providesClock(): Clock = Clock.System
|
||||
|
||||
@Provides
|
||||
fun providesLocale(): Locale = Locale.getDefault()
|
||||
|
||||
@Provides
|
||||
fun providesTimezone(): TimezoneProvider = TimezoneProvider { TimeZone.currentSystemDefault() }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||
<string name="common_date_date_at_time">"%1$s à %2$s"</string>
|
||||
<string name="common_date_this_month">"Ce mois-ci"</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
|
||||
<string name="common_date_date_at_time">"%1$s at %2$s"</string>
|
||||
<string name="common_date_this_month">"This month"</string>
|
||||
</resources>
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatterMode
|
||||
import io.element.android.libraries.dateformatter.test.FakeClock
|
||||
import io.element.android.tests.testutils.InstrumentationStringProvider
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.TimeZone
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Config(qualifiers = "fr")
|
||||
class DefaultDateFormatterFrTest {
|
||||
@Test
|
||||
fun `test null`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts: Long? = null
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test epoch`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts = 0L
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("1 janvier 1970 à 00:00")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Janvier 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("1 janvier 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("01.01.1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("00:00")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test epoch relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts = 0L
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("1 janvier 1970 à 00:00")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Janvier 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("1 janvier 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("01.01.1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("00:00")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test now`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 avril 1980 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Dimanche 6 avril")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test now relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Ce mois-ci")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Aujourd’hui")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one second before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 avril 1980 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Dimanche 6 avril")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one second before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Ce mois-ci")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Aujourd’hui")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one minute before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 avril 1980 à 18:34")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Dimanche 6 avril")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("18:34")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:34")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one minute before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("18:34")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Ce mois-ci")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Aujourd’hui")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("18:34")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:34")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one hour before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 avril 1980 à 17:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Dimanche 6 avril")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("17:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("17:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one hour before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("17:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Ce mois-ci")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Aujourd’hui")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("17:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("17:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one day before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-05T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("5 avril 1980 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Samedi 5 avril")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("5 avr.")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one day before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-05T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("Hier à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Ce mois-ci")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Hier")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("Hier")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one month before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-03-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 mars 1980 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Mars 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Jeudi 6 mars")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("6 mars")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one month before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-03-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("Jeudi 6 mars à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Mars 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Jeudi 6 mars")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("6 mars")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one year before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("6 avril 1979 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("Avril 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("6 avril 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("06.04.1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one year before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("6 avril 1979 à 18:35")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("Avril 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("6 avril 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("06.04.1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("18:35")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DefaultLastMessageFormatter and set current time to the provided date.
|
||||
*/
|
||||
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): DefaultDateFormatter {
|
||||
val clock = FakeClock().apply { givenInstant(Instant.parse(currentDate)) }
|
||||
val localDateTimeProvider = LocalDateTimeProvider(clock) { TimeZone.UTC }
|
||||
val dateFormatters = DateFormatters(
|
||||
localeChangeObserver = {},
|
||||
clock = clock,
|
||||
timeZoneProvider = { TimeZone.UTC },
|
||||
)
|
||||
val stringProvider = InstrumentationStringProvider()
|
||||
val dateFormatterDay = DefaultDateFormatterDay(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
)
|
||||
return DefaultDateFormatter(
|
||||
dateFormatterFull = DateFormatterFull(
|
||||
stringProvider = stringProvider,
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
dateFormatterDay = dateFormatterDay,
|
||||
),
|
||||
dateFormatterMonth = DateFormatterMonth(
|
||||
stringProvider = stringProvider,
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
dateFormatterDay = dateFormatterDay,
|
||||
dateFormatterTime = DateFormatterTime(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
dateFormatterTimeOnly = DateFormatterTimeOnly(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatterMode
|
||||
import io.element.android.libraries.dateformatter.test.FakeClock
|
||||
import io.element.android.tests.testutils.InstrumentationStringProvider
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.TimeZone
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@Config(qualifiers = "en")
|
||||
class DefaultDateFormatterTest {
|
||||
@Test
|
||||
fun `test null`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts: Long? = null
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test epoch`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts = 0L
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("January 1, 1970 at 12:00 AM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("January 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("January 1, 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("01.01.1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("12:00 AM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test epoch relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val ts = 0L
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("January 1, 1970 at 12:00 AM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("January 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("January 1, 1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("01.01.1970")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("12:00 AM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test now`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 6, 1980 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Sunday 6 April")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test now relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("This month")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Today")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one second before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 6, 1980 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Sunday 6 April")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one second before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("This month")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Today")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one minute before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 6, 1980 at 6:34 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Sunday 6 April")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("6:34 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:34 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one minute before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("6:34 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("This month")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Today")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("6:34 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:34 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one hour before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 6, 1980 at 5:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Sunday 6 April")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("5:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("5:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one hour before relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("5:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("This month")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Today")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("5:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("5:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one day before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-05T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 5, 1980 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Saturday 5 April")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("5 Apr")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one day before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-05T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("Yesterday at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("This month")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Yesterday")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("Yesterday")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one month before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-03-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("March 6, 1980 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("March 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("Thursday 6 March")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("6 Mar")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one month before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-03-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("Thursday 6 March at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("March 1980")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("Thursday 6 March")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("6 Mar")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one year before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full)).isEqualTo("April 6, 1979 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month)).isEqualTo("April 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day)).isEqualTo("April 6, 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate)).isEqualTo("06.04.1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one year before same time relative`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val ts = Instant.parse(dat).toEpochMilliseconds()
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Full, true)).isEqualTo("April 6, 1979 at 6:35 PM")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Month, true)).isEqualTo("April 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.Day, true)).isEqualTo("April 6, 1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOrDate, true)).isEqualTo("06.04.1979")
|
||||
assertThat(formatter.format(ts, DateFormatterMode.TimeOnly, true)).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DefaultLastMessageFormatter and set current time to the provided date.
|
||||
*/
|
||||
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): DefaultDateFormatter {
|
||||
val clock = FakeClock().apply { givenInstant(Instant.parse(currentDate)) }
|
||||
val localDateTimeProvider = LocalDateTimeProvider(clock) { TimeZone.UTC }
|
||||
val dateFormatters = DateFormatters(
|
||||
localeChangeObserver = {},
|
||||
clock = clock,
|
||||
timeZoneProvider = { TimeZone.UTC },
|
||||
)
|
||||
val stringProvider = InstrumentationStringProvider()
|
||||
val dateFormatterDay = DefaultDateFormatterDay(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
)
|
||||
return DefaultDateFormatter(
|
||||
dateFormatterFull = DateFormatterFull(
|
||||
stringProvider = stringProvider,
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
dateFormatterDay = dateFormatterDay,
|
||||
),
|
||||
dateFormatterMonth = DateFormatterMonth(
|
||||
stringProvider = stringProvider,
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
dateFormatterDay = dateFormatterDay,
|
||||
dateFormatterTime = DateFormatterTime(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
dateFormatterTimeOnly = DateFormatterTimeOnly(
|
||||
localDateTimeProvider = localDateTimeProvider,
|
||||
dateFormatters = dateFormatters,
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.impl
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.dateformatter.api.LastMessageTimestampFormatter
|
||||
import io.element.android.libraries.dateformatter.test.FakeClock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import org.junit.Test
|
||||
import java.util.Locale
|
||||
|
||||
class DefaultLastMessageTimestampFormatterTest {
|
||||
@Test
|
||||
fun `test null`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(null)).isEmpty()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test epoch`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(0)).isEqualTo("01.01.1970")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test now`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one second before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:35:23.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("6:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one minute before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T18:34:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("6:34 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one hour before`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-06T17:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("5:35 PM")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one day before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-04-05T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
// TODO DateUtils.getRelativeTimeSpanString returns null.
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one month before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1980-03-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("6 Mar")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test one year before same time`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val formatter = createFormatter(now)
|
||||
assertThat(formatter.format(Instant.parse(dat).toEpochMilliseconds())).isEqualTo("06.04.1979")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test full format`() {
|
||||
val now = "1980-04-06T18:35:24.00Z"
|
||||
val dat = "1979-04-06T18:35:24.00Z"
|
||||
val clock = FakeClock().apply { givenInstant(Instant.parse(now)) }
|
||||
val dateFormatters = DateFormatters(Locale.US, clock) { TimeZone.UTC }
|
||||
assertThat(dateFormatters.formatDateWithFullFormat(Instant.parse(dat).toLocalDateTime(TimeZone.UTC))).isEqualTo("Friday, April 6, 1979")
|
||||
}
|
||||
|
||||
/**
|
||||
* Create DefaultLastMessageFormatter and set current time to the provided date.
|
||||
*/
|
||||
private fun createFormatter(@Suppress("SameParameterValue") currentDate: String): LastMessageTimestampFormatter {
|
||||
val clock = FakeClock().apply { givenInstant(Instant.parse(currentDate)) }
|
||||
val localDateTimeProvider = LocalDateTimeProvider(clock) { TimeZone.UTC }
|
||||
val dateFormatters = DateFormatters(Locale.US, clock) { TimeZone.UTC }
|
||||
return DefaultLastMessageTimestampFormatter(localDateTimeProvider, dateFormatters)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.test
|
||||
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatter
|
||||
import io.element.android.libraries.dateformatter.api.DateFormatterMode
|
||||
|
||||
class FakeDateFormatter(
|
||||
private val formatLambda: (Long?, DateFormatterMode, Boolean) -> String = { timestamp, mode, useRelative ->
|
||||
"$timestamp $mode $useRelative"
|
||||
},
|
||||
) : DateFormatter {
|
||||
override fun format(
|
||||
timestamp: Long?,
|
||||
mode: DateFormatterMode,
|
||||
useRelative: Boolean,
|
||||
): String {
|
||||
return formatLambda(timestamp, mode, useRelative)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.test
|
||||
|
||||
import io.element.android.libraries.dateformatter.api.DaySeparatorFormatter
|
||||
|
||||
class FakeDaySeparatorFormatter : DaySeparatorFormatter {
|
||||
private var format = ""
|
||||
|
||||
fun givenFormat(format: String) {
|
||||
this.format = format
|
||||
}
|
||||
|
||||
override fun format(timestamp: Long): String {
|
||||
return format
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2023, 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.dateformatter.test
|
||||
|
||||
import io.element.android.libraries.dateformatter.api.LastMessageTimestampFormatter
|
||||
|
||||
const val A_FORMATTED_DATE = "formatted_date"
|
||||
|
||||
class FakeLastMessageTimestampFormatter(
|
||||
var format: String = "",
|
||||
) : LastMessageTimestampFormatter {
|
||||
fun givenFormat(format: String) {
|
||||
this.format = format
|
||||
}
|
||||
|
||||
override fun format(timestamp: Long?): String {
|
||||
return format
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user