-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts ApplicationPreferencesMigrations out of ApplicationPreferenc…
…esImpl
- Loading branch information
Showing
6 changed files
with
121 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ore/src/main/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Theme, NightMode> { | ||
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) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...src/test/kotlin/org/piepmeyer/gauguin/preferences/ApplicationPreferencesMigrationsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ApplicationPreferences> { | ||
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<ApplicationPreferences> { | ||
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 | ||
} | ||
} | ||
}) |