-
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix compatibility of shared preferences with previous version. Gracef…
…ully handle misplaced settings.
- Loading branch information
1 parent
fc23c42
commit aadde1a
Showing
11 changed files
with
128 additions
and
141 deletions.
There are no files selected for viewing
93 changes: 0 additions & 93 deletions
93
...in/java/com/swordfish/lemuroid/app/mobile/feature/gamemenu/GameMenuCoreOptionsFragment.kt
This file was deleted.
Oops, something went wrong.
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
16 changes: 0 additions & 16 deletions
16
lemuroid-app/src/main/java/com/swordfish/lemuroid/app/utils/android/PreferenceStates.kt
This file was deleted.
Oops, something went wrong.
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
46 changes: 46 additions & 0 deletions
46
.../java/com/swordfish/lemuroid/app/utils/settings/SafeBooleanPreferenceSettingValueState.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,46 @@ | ||
package com.swordfish.lemuroid.app.utils.settings | ||
|
||
import android.content.SharedPreferences | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.core.content.edit | ||
import androidx.preference.PreferenceManager | ||
import com.alorma.compose.settings.storage.base.SettingValueState | ||
|
||
@Composable | ||
fun rememberSafePreferenceBooleanSettingState( | ||
key: String, | ||
defaultValue: Boolean, | ||
preferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(LocalContext.current), | ||
): SafeBooleanPreferenceSettingValueState { | ||
return remember { | ||
SafeBooleanPreferenceSettingValueState( | ||
preferences = preferences, | ||
key = key, | ||
defaultValue = defaultValue, | ||
) | ||
} | ||
} | ||
|
||
class SafeBooleanPreferenceSettingValueState( | ||
private val preferences: SharedPreferences, | ||
val key: String, | ||
val defaultValue: Boolean = false, | ||
) : SettingValueState<Boolean> { | ||
private var _value by mutableStateOf(preferences.safeGetBoolean(key, defaultValue)) | ||
|
||
override var value: Boolean | ||
set(value) { | ||
_value = value | ||
preferences.edit { putBoolean(key, value) } | ||
} | ||
get() = _value | ||
|
||
override fun reset() { | ||
value = defaultValue | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
...roid-app/src/main/java/com/swordfish/lemuroid/app/utils/settings/SharedPreferenceUtils.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,27 @@ | ||
package com.swordfish.lemuroid.app.utils.settings | ||
|
||
import android.content.SharedPreferences | ||
|
||
fun SharedPreferences.safeGetString( | ||
key: String, | ||
defValue: String?, | ||
): String? { | ||
val result = runCatching { getString(key, defValue) } | ||
return result.getOrDefault(defValue) | ||
} | ||
|
||
fun SharedPreferences.safeGetBoolean( | ||
key: String, | ||
defValue: Boolean, | ||
): Boolean { | ||
val result = runCatching { getBoolean(key, defValue) } | ||
return result.getOrDefault(defValue) | ||
} | ||
|
||
fun SharedPreferences.safeGetStringSet( | ||
key: String, | ||
defValue: Set<String>?, | ||
): Set<String>? { | ||
val result = runCatching { getStringSet(key, defValue) } | ||
return result.getOrDefault(defValue) | ||
} |