From d8a6be727e290ed3ba24b59128859bc681c43638 Mon Sep 17 00:00:00 2001 From: Paul Rybitskyi Date: Wed, 23 Oct 2024 19:57:56 +0300 Subject: [PATCH] Fix bugs related to invalid locale used for time formatting (#246) --- .../common/testing/FakeLocaleProvider.kt | 13 +++++++++++++ .../ArticlePublicationDateFormatter.kt | 6 +++++- .../formatters/GameReleaseDateFormatter.kt | 6 ++++-- .../gamedge/core/providers/LocaleProvider.kt | 18 ++++++++++++++++++ .../ArticlePublicationDateFormatterImplTest.kt | 2 ++ 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 common-testing/src/main/java/com/paulrybitskyi/gamedge/common/testing/FakeLocaleProvider.kt create mode 100644 core/src/main/java/com/paulrybitskyi/gamedge/core/providers/LocaleProvider.kt diff --git a/common-testing/src/main/java/com/paulrybitskyi/gamedge/common/testing/FakeLocaleProvider.kt b/common-testing/src/main/java/com/paulrybitskyi/gamedge/common/testing/FakeLocaleProvider.kt new file mode 100644 index 000000000..8c9dd0e60 --- /dev/null +++ b/common-testing/src/main/java/com/paulrybitskyi/gamedge/common/testing/FakeLocaleProvider.kt @@ -0,0 +1,13 @@ +package com.paulrybitskyi.gamedge.common.testing + +import com.paulrybitskyi.gamedge.core.providers.LocaleProvider +import java.util.Locale + +class FakeLocaleProvider( + private val locale: Locale = Locale.ENGLISH, +) : LocaleProvider { + + override fun getLocale(): Locale { + return locale + } +} diff --git a/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/ArticlePublicationDateFormatter.kt b/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/ArticlePublicationDateFormatter.kt index 865bdcc88..5458b8836 100644 --- a/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/ArticlePublicationDateFormatter.kt +++ b/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/ArticlePublicationDateFormatter.kt @@ -16,6 +16,7 @@ package com.paulrybitskyi.gamedge.core.formatters +import com.paulrybitskyi.gamedge.core.providers.LocaleProvider import com.paulrybitskyi.gamedge.core.providers.TimeFormat import com.paulrybitskyi.gamedge.core.providers.TimeFormatProvider import com.paulrybitskyi.gamedge.core.providers.TimeProvider @@ -36,6 +37,7 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor( private val relativeDateFormatter: RelativeDateFormatter, private val timeProvider: TimeProvider, private val timeFormatProvider: TimeFormatProvider, + private val localeProvider: LocaleProvider, ) : ArticlePublicationDateFormatter { private companion object { @@ -68,7 +70,9 @@ internal class ArticlePublicationDateFormatterImpl @Inject constructor( private fun formatAsAbsoluteDate(dateTime: LocalDateTime): String { val pattern = getAbsoluteDatePattern(dateTime) - val formattedDate = DateTimeFormatter.ofPattern(pattern).format(dateTime) + val formattedDate = DateTimeFormatter + .ofPattern(pattern, localeProvider.getLocale()) + .format(dateTime) return formattedDate } diff --git a/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/GameReleaseDateFormatter.kt b/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/GameReleaseDateFormatter.kt index bf62f4990..2ee99d864 100644 --- a/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/GameReleaseDateFormatter.kt +++ b/core/src/main/java/com/paulrybitskyi/gamedge/core/formatters/GameReleaseDateFormatter.kt @@ -20,6 +20,7 @@ import com.paulrybitskyi.gamedge.common.domain.games.entities.Game import com.paulrybitskyi.gamedge.common.domain.games.entities.ReleaseDate import com.paulrybitskyi.gamedge.common.domain.games.entities.ReleaseDateCategory import com.paulrybitskyi.gamedge.core.R +import com.paulrybitskyi.gamedge.core.providers.LocaleProvider import com.paulrybitskyi.gamedge.core.providers.StringProvider import com.paulrybitskyi.hiltbinder.BindType import java.time.Instant @@ -36,6 +37,7 @@ interface GameReleaseDateFormatter { internal class GameReleaseDateFormatterImpl @Inject constructor( private val stringProvider: StringProvider, private val relativeDateFormatter: RelativeDateFormatter, + private val localeProvider: LocaleProvider, ) : GameReleaseDateFormatter { private companion object { @@ -76,7 +78,7 @@ internal class GameReleaseDateFormatterImpl @Inject constructor( private fun ReleaseDate.formatCompleteDate(): String { val releaseLocalDateTime = toLocalDateTime() val formattedReleaseDate = DateTimeFormatter - .ofPattern(COMPLETE_DATE_FORMATTING_PATTERN) + .ofPattern(COMPLETE_DATE_FORMATTING_PATTERN, localeProvider.getLocale()) .format(releaseLocalDateTime) return buildString { @@ -89,7 +91,7 @@ internal class GameReleaseDateFormatterImpl @Inject constructor( private fun ReleaseDate.formatDaylessDate(): String { return DateTimeFormatter - .ofPattern(DAYLESS_DATE_FORMATTING_PATTERN) + .ofPattern(DAYLESS_DATE_FORMATTING_PATTERN, localeProvider.getLocale()) .format(toLocalDateTime()) } diff --git a/core/src/main/java/com/paulrybitskyi/gamedge/core/providers/LocaleProvider.kt b/core/src/main/java/com/paulrybitskyi/gamedge/core/providers/LocaleProvider.kt new file mode 100644 index 000000000..3218c3918 --- /dev/null +++ b/core/src/main/java/com/paulrybitskyi/gamedge/core/providers/LocaleProvider.kt @@ -0,0 +1,18 @@ +package com.paulrybitskyi.gamedge.core.providers + +import com.paulrybitskyi.hiltbinder.BindType +import java.util.Locale +import javax.inject.Inject + +interface LocaleProvider { + fun getLocale(): Locale +} + +@BindType +internal class LocaleProviderImpl @Inject constructor() : LocaleProvider { + + override fun getLocale(): Locale { + // App only supports the English language + return Locale.ENGLISH + } +} diff --git a/core/src/test/java/com/paulrybitskyi/gamedge/core/ArticlePublicationDateFormatterImplTest.kt b/core/src/test/java/com/paulrybitskyi/gamedge/core/ArticlePublicationDateFormatterImplTest.kt index 5ae95e9a5..7df8cac7f 100644 --- a/core/src/test/java/com/paulrybitskyi/gamedge/core/ArticlePublicationDateFormatterImplTest.kt +++ b/core/src/test/java/com/paulrybitskyi/gamedge/core/ArticlePublicationDateFormatterImplTest.kt @@ -17,6 +17,7 @@ package com.paulrybitskyi.gamedge.core import com.google.common.truth.Truth.assertThat +import com.paulrybitskyi.gamedge.common.testing.FakeLocaleProvider import com.paulrybitskyi.gamedge.core.formatters.ArticlePublicationDateFormatterImpl import com.paulrybitskyi.gamedge.core.formatters.RelativeDateFormatter import com.paulrybitskyi.gamedge.core.providers.TimeFormat @@ -50,6 +51,7 @@ internal class ArticlePublicationDateFormatterImplTest { relativeDateFormatter = relativeDateFormatter, timeProvider = timeProvider, timeFormatProvider = timeFormatProvider, + localeProvider = FakeLocaleProvider(), ) every { relativeDateFormatter.formatRelativeDate(any()) } returns RELATIVE_DATE