Skip to content

Commit

Permalink
Fixing default values
Browse files Browse the repository at this point in the history
  • Loading branch information
gmerinojimenez committed Oct 15, 2021
1 parent 2d3361d commit b7e67f6
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ class TweaksBusinessLogic @Inject constructor(
}

@Suppress("UNCHECKED_CAST")
fun <T> getValue(key: String): Flow<T?> = tweaksDataStore.data
.map { preferences -> preferences[buildKey(keyToEntryValueMap[key] as TweakEntry<T>)] }
fun <T> getValue(key: String): Flow<T?> {
val tweakEntry = keyToEntryValueMap[key] as TweakEntry<T>
return getValue(tweakEntry)
}

fun <T> getValue(entry: TweakEntry<T>): Flow<T?> = when (entry as Modifiable) {
is ReadOnly<*> -> (entry as ReadOnly<T>).value
Expand All @@ -54,13 +56,10 @@ class TweaksBusinessLogic @Inject constructor(
private fun <T> getEditableValue(entry: TweakEntry<T>): Flow<T?> {
val editableCasted = entry as Editable<T>
val defaultValue = editableCasted.defaultValue
return if (defaultValue != null) {
defaultValue.combine(getFromStorage(entry)) { default, storage ->
storage ?: default
}
} else {
getFromStorage(entry)
return defaultValue?.combine(getFromStorage(entry)) { default, storage ->
storage ?: default
}
?: getFromStorage(entry)
}

private fun <T> getFromStorage(entry: TweakEntry<T>) =
Expand Down

0 comments on commit b7e67f6

Please sign in to comment.