From cac6b9468fc589b52deec40d86b113a121b1caa3 Mon Sep 17 00:00:00 2001 From: meikpiep Date: Tue, 25 Feb 2025 15:56:06 +0100 Subject: [PATCH] Extracts ApplicationPreferencesMigrations out of ApplicationPreferencesImpl --- .../org/piepmeyer/gauguin/MainApplication.kt | 4 +- .../preferences/ApplicationPreferencesImpl.kt | 42 +++----------- .../ApplicationPreferencesImplTest.kt | 37 ------------ .../preferences/ApplicationPreferences.kt | 10 +++- .../ApplicationPreferencesMigrations.kt | 42 ++++++++++++++ .../ApplicationPreferencesMigrationsTest.kt | 58 +++++++++++++++++++ 6 files changed, 121 insertions(+), 72 deletions(-) create mode 100644 gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrations.kt create mode 100644 gauguin-core/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrationsTest.kt diff --git a/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/MainApplication.kt b/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/MainApplication.kt index 607f1876..8fc07254 100644 --- a/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/MainApplication.kt +++ b/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/MainApplication.kt @@ -14,6 +14,7 @@ import org.koin.core.module.dsl.withOptions import org.koin.dsl.module import org.piepmeyer.gauguin.preferences.ApplicationPreferences import org.piepmeyer.gauguin.preferences.ApplicationPreferencesImpl +import org.piepmeyer.gauguin.preferences.ApplicationPreferencesMigrations import org.piepmeyer.gauguin.preferences.StatisticsManager import org.piepmeyer.gauguin.preferences.StatisticsManagerImpl import org.piepmeyer.gauguin.ui.ActivityUtils @@ -28,7 +29,8 @@ class MainApplication : Application() { logger.info { "Starting application Gauguin..." } val applicationPreferences = ApplicationPreferencesImpl(this) - applicationPreferences.migrateThemeToNightModeIfNecessary() + val preferenceMigrations = ApplicationPreferencesMigrations(applicationPreferences) + preferenceMigrations.migrateThemeToNightModeIfNecessary() val options = DynamicColorsOptions diff --git a/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImpl.kt b/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImpl.kt index d9ee090b..88190365 100644 --- a/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImpl.kt +++ b/gauguin-app/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImpl.kt @@ -24,39 +24,15 @@ class ApplicationPreferencesImpl( preferences.edit { clear() } } - override fun migrateThemeToNightModeIfNecessary() { - if (!preferences.getString("nightMode", null).isNullOrEmpty()) { - return - } - - val oldThemeValue = preferences.getString("theme", null) - /* - * Possible values: - * LIGHT - * DARK - * SYSTEM_DEFAULT - * DYNAMIC_COLORS - */ - - val (newThemeValue, newNightModeValue) = migrateToNewThemeNightModesValues(oldThemeValue) - - theme = newThemeValue - nightMode = newNightModeValue - } - - fun migrateToNewThemeNightModesValues(oldThemeValue: String?): Pair { - val newThemeValue = if (oldThemeValue == "DYNAMIC_COLORS") Theme.DYNAMIC_COLORS else Theme.GAUGUIN - - val newNightModeValue = - when (oldThemeValue) { - "LIGHT", "DYNAMIC_COLORS" -> NightMode.LIGHT - "DARK" -> NightMode.DARK - "SYSTEM_DEFAULT" -> NightMode.SYSTEM_DEFAULT - else -> NightMode.DARK - } - - return Pair(newThemeValue, newNightModeValue) - } + override fun getString( + key: String?, + defValue: String?, + ): String? = preferences.getString(key, defValue) + + override fun getStringSet( + key: String?, + defValues: Set?, + ): Set? = preferences.getStringSet(key, defValues) override var theme: Theme get() { diff --git a/gauguin-app/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImplTest.kt b/gauguin-app/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImplTest.kt index 3e2a0fb8..01c46e9a 100644 --- a/gauguin-app/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImplTest.kt +++ b/gauguin-app/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesImplTest.kt @@ -1,8 +1,6 @@ package org.piepmeyer.gauguin.preferences import android.content.SharedPreferences -import io.kotest.assertions.assertSoftly -import io.kotest.assertions.withClue import io.kotest.core.spec.style.FunSpec import io.kotest.datatest.withData import io.kotest.matchers.shouldBe @@ -63,39 +61,4 @@ class ApplicationPreferencesImplTest : preferences.theme shouldBe testData.expectedTheme } - - data class OldThemeMigrationTestData( - val sharedPreferenceValue: String?, - val expectedTheme: Theme, - val expectedNightMode: NightMode, - ) - - withData( - OldThemeMigrationTestData(null, Theme.GAUGUIN, NightMode.DARK), - OldThemeMigrationTestData("unknown", Theme.GAUGUIN, NightMode.DARK), - OldThemeMigrationTestData("DARK", Theme.GAUGUIN, NightMode.DARK), - OldThemeMigrationTestData("LIGHT", Theme.GAUGUIN, NightMode.LIGHT), - OldThemeMigrationTestData("DYNAMIC_COLORS", Theme.DYNAMIC_COLORS, NightMode.LIGHT), - OldThemeMigrationTestData("SYSTEM_DEFAULT", Theme.GAUGUIN, NightMode.SYSTEM_DEFAULT), - ) { testData -> - val sharedPreferences = - mockk() - - val preferences = - ApplicationPreferencesImpl( - mockk(), - sharedPreferences, - ) - - val (theme, nightMode) = preferences.migrateToNewThemeNightModesValues(testData.sharedPreferenceValue) - - assertSoftly { - withClue("theme") { - theme shouldBe testData.expectedTheme - } - withClue("nightMode") { - nightMode shouldBe testData.expectedNightMode - } - } - } }) diff --git a/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferences.kt b/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferences.kt index 28b334e1..1f4cf9f6 100644 --- a/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferences.kt +++ b/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferences.kt @@ -61,5 +61,13 @@ interface ApplicationPreferences { var mergingCageAlgorithm: Boolean - fun migrateThemeToNightModeIfNecessary() + fun getString( + key: String?, + defValue: String?, + ): String? + + fun getStringSet( + key: String?, + defValues: Set?, + ): Set? } diff --git a/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrations.kt b/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrations.kt new file mode 100644 index 00000000..c7adb56a --- /dev/null +++ b/gauguin-core/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrations.kt @@ -0,0 +1,42 @@ +package org.piepmeyer.gauguin.preferences + +import org.piepmeyer.gauguin.NightMode +import org.piepmeyer.gauguin.Theme + +class ApplicationPreferencesMigrations( + private val applicationPreferences: ApplicationPreferences, +) { + fun migrateThemeToNightModeIfNecessary() { + if (applicationPreferences.getString("nightMode", null) != null) { + return + } + + val oldThemeValue = applicationPreferences.getString("theme", null) + /* + * Possible values: + * LIGHT + * DARK + * SYSTEM_DEFAULT + * DYNAMIC_COLORS + */ + + val (newThemeValue, newNightModeValue) = migrateToNewThemeNightModesValues(oldThemeValue) + + applicationPreferences.theme = newThemeValue + applicationPreferences.nightMode = newNightModeValue + } + + private fun migrateToNewThemeNightModesValues(oldThemeValue: String?): Pair { + val newThemeValue = if (oldThemeValue == "DYNAMIC_COLORS") Theme.DYNAMIC_COLORS else Theme.GAUGUIN + + val newNightModeValue = + when (oldThemeValue) { + "LIGHT", "DYNAMIC_COLORS" -> NightMode.LIGHT + "DARK" -> NightMode.DARK + "SYSTEM_DEFAULT" -> NightMode.SYSTEM_DEFAULT + else -> NightMode.DARK + } + + return Pair(newThemeValue, newNightModeValue) + } +} diff --git a/gauguin-core/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrationsTest.kt b/gauguin-core/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrationsTest.kt new file mode 100644 index 00000000..0cd11fb4 --- /dev/null +++ b/gauguin-core/src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrationsTest.kt @@ -0,0 +1,58 @@ +package org.piepmeyer.gauguin.preferences + +import io.kotest.core.spec.style.FunSpec +import io.kotest.datatest.withData +import io.mockk.every +import io.mockk.just +import io.mockk.mockk +import io.mockk.runs +import io.mockk.verify +import org.piepmeyer.gauguin.NightMode +import org.piepmeyer.gauguin.Theme + +class ApplicationPreferencesMigrationsTest : + FunSpec({ + test("night mode migration gets not triggered if night mode is already in use") { + val preferences = + mockk { + every { getString("nightMode", null) } returns "DARK" + // no setter of 'nightMode' or 'theme' allowed here + } + + val migrations = ApplicationPreferencesMigrations(preferences) + + migrations.migrateThemeToNightModeIfNecessary() + } + + data class OldThemeMigrationTestData( + val sharedPreferenceValue: String?, + val expectedTheme: Theme, + val expectedNightMode: NightMode, + ) + + withData( + OldThemeMigrationTestData(null, Theme.GAUGUIN, NightMode.DARK), + OldThemeMigrationTestData("unknown", Theme.GAUGUIN, NightMode.DARK), + OldThemeMigrationTestData("DARK", Theme.GAUGUIN, NightMode.DARK), + OldThemeMigrationTestData("LIGHT", Theme.GAUGUIN, NightMode.LIGHT), + OldThemeMigrationTestData("DYNAMIC_COLORS", Theme.DYNAMIC_COLORS, NightMode.LIGHT), + OldThemeMigrationTestData("SYSTEM_DEFAULT", Theme.GAUGUIN, NightMode.SYSTEM_DEFAULT), + ) { testData -> + val preferences = + mockk { + every { getString("nightMode", null) } returns null + every { getString("theme", null) } returns testData.sharedPreferenceValue + every { theme = testData.expectedTheme } just runs + every { nightMode = testData.expectedNightMode } just runs + } + + val migrations = ApplicationPreferencesMigrations(preferences) + + migrations.migrateThemeToNightModeIfNecessary() + + verify { + preferences.theme = testData.expectedTheme + preferences.nightMode = testData.expectedNightMode + } + } + })