Skip to content

Commit

Permalink
Fix crash an first app start while reading out the theme from prefere…
Browse files Browse the repository at this point in the history
…nces
  • Loading branch information
meikpiep committed Oct 13, 2024
1 parent 40340e9 commit f5fa9cc
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fix crash during start if the app starts for the first time.

### Security

## [0.31.1] - 2024-10-10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.piepmeyer.gauguin.preferences

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import org.koin.core.component.KoinComponent
Expand All @@ -15,22 +16,23 @@ import org.piepmeyer.gauguin.options.SingleCageUsage

class ApplicationPreferencesImpl(
private val androidContext: Context,
private val preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(androidContext),
) : KoinComponent,
ApplicationPreferences {
private val preferences = PreferenceManager.getDefaultSharedPreferences(androidContext)

override fun clear() {
preferences.edit { clear() }
}

override var theme: Theme
get() {
val themePref = preferences.getString("theme", null)!!
return try {
enumValueOf(themePref)
} catch (e: IllegalArgumentException) {
return Theme.DARK
}
val themePref = preferences.getString("theme", null)
return themePref?.let {
try {
enumValueOf<Theme>(it)
} catch (e: IllegalArgumentException) {
return Theme.DARK
}
} ?: Theme.DARK
}
set(value) {
preferences.edit {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.piepmeyer.gauguin.preferences

import android.content.SharedPreferences
import io.kotest.core.spec.style.FunSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import org.piepmeyer.gauguin.Theme

class ApplicationPreferencesImplTest :
FunSpec({

data class ThemeTestData(
val sharedPreferenceValue: String?,
val expectedTheme: Theme,
)

withData(
ThemeTestData(null, Theme.DARK),
ThemeTestData("unknown", Theme.DARK),
ThemeTestData("DARK", Theme.DARK),
ThemeTestData("LIGHT", Theme.LIGHT),
ThemeTestData("DYNAMIC_COLORS", Theme.DYNAMIC_COLORS),
ThemeTestData("SYSTEM_DEFAULT", Theme.SYSTEM_DEFAULT),
) { testData ->
val sharedPreferences =
mockk<SharedPreferences> {
every { getString("theme", null) } returns testData.sharedPreferenceValue
}

val preferences =
ApplicationPreferencesImpl(
mockk(),
sharedPreferences,
)

preferences.theme shouldBe testData.expectedTheme
}
})

0 comments on commit f5fa9cc

Please sign in to comment.