-
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.
- Loading branch information
Showing
6 changed files
with
200 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
69 changes: 69 additions & 0 deletions
69
...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,69 @@ | ||
package org.piepmeyer.gauguin.preferences | ||
|
||
import org.piepmeyer.gauguin.NightMode | ||
import org.piepmeyer.gauguin.Theme | ||
import org.piepmeyer.gauguin.options.DifficultySetting | ||
|
||
class ApplicationPreferencesMigrations( | ||
private val applicationPreferences: ApplicationPreferences, | ||
) { | ||
fun migrateDifficultySettingIfNecessary() { | ||
if (applicationPreferences.getStringSet("difficulties", null) != null) { | ||
return | ||
} | ||
|
||
val oldDifficultyValue = applicationPreferences.getString("difficulty", null) | ||
/* | ||
* Possible values: | ||
* ANY | ||
* VERY_EASY | ||
* EASY | ||
* MEDIUM | ||
* HARD | ||
* EXTREME | ||
*/ | ||
|
||
if (oldDifficultyValue != null) { | ||
applicationPreferences.difficultiesSetting = | ||
if (oldDifficultyValue == "ANY") { | ||
DifficultySetting.all() | ||
} else { | ||
setOf(DifficultySetting.valueOf(oldDifficultyValue)) | ||
} | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...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,110 @@ | ||
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 | ||
import org.piepmeyer.gauguin.options.DifficultySetting | ||
|
||
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 | ||
} | ||
} | ||
|
||
test("difficulty migration gets not triggered if difficulties are already in use") { | ||
val preferences = | ||
mockk<ApplicationPreferences> { | ||
every { getStringSet("difficulties", null) } returns setOf(DifficultySetting.EASY.name) | ||
// no setter of 'difficulties' allowed here | ||
} | ||
|
||
val migrations = ApplicationPreferencesMigrations(preferences) | ||
|
||
migrations.migrateDifficultySettingIfNecessary() | ||
} | ||
|
||
test("difficulty migration gets not triggered if no difficulty preference is used yet") { | ||
val preferences = | ||
mockk<ApplicationPreferences> { | ||
every { getStringSet("difficulties", null) } returns null | ||
every { getString("difficulty", null) } returns null | ||
// no setter of 'difficulties' allowed here | ||
} | ||
|
||
val migrations = ApplicationPreferencesMigrations(preferences) | ||
|
||
migrations.migrateDifficultySettingIfNecessary() | ||
} | ||
|
||
data class DifficultyMigrationTestData( | ||
val sharedPreferenceDifficultyValue: String?, | ||
val expectedDifficultiesValue: Set<DifficultySetting>, | ||
) | ||
|
||
withData( | ||
DifficultyMigrationTestData("EASY", setOf(DifficultySetting.EASY)), | ||
DifficultyMigrationTestData("EXTREME", setOf(DifficultySetting.EXTREME)), | ||
DifficultyMigrationTestData("ANY", DifficultySetting.all()), | ||
) { testData -> | ||
val preferences = | ||
mockk<ApplicationPreferences> { | ||
every { getStringSet("difficulties", null) } returns null | ||
every { getString("difficulty", null) } returns testData.sharedPreferenceDifficultyValue | ||
every { difficultiesSetting = testData.expectedDifficultiesValue } just runs | ||
} | ||
|
||
val migrations = ApplicationPreferencesMigrations(preferences) | ||
|
||
migrations.migrateDifficultySettingIfNecessary() | ||
|
||
verify { | ||
preferences.difficultiesSetting = testData.expectedDifficultiesValue | ||
} | ||
} | ||
}) |