Skip to content

Commit

Permalink
Mark as overriden whenever there is something stored (#32)
Browse files Browse the repository at this point in the history
* Fixing TextFields

* Clearing the field

* Clean

* Fix lint & detekt

* Mark as overriden when there is a value stored, even if it is the same (makes sense for booleans)
  • Loading branch information
gmerinojimenez authored May 10, 2023
1 parent 7cd5880 commit 265dfbb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.telefonica.tweaks.domain

import com.telefonica.tweaks.data.TweaksRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapMerge
import kotlinx.coroutines.flow.flowOf
Expand Down Expand Up @@ -65,7 +64,7 @@ class TweaksBusinessLogic @Inject constructor(
private fun <T> getMutableValue(entry: Editable<T>): Flow<T?> {
val defaultValue: Flow<T> = entry.defaultValue ?: flowOf()

return isOverriden(entry)
return isOverridden(entry)
.flatMapMerge { overriden ->
when (overriden) {
true -> getFromStorage(entry)
Expand All @@ -74,23 +73,9 @@ class TweaksBusinessLogic @Inject constructor(
}
}

private fun isOverriden(entry: Editable<*>): Flow<Boolean> =
fun isOverridden(entry: Editable<*>): Flow<Boolean> =
tweaksRepository.isOverriden(entry).map { it ?: OVERRIDEN_DEFAULT_VALUE }

fun <T> isOverriddenOrDifferentFromDefaultValue(entry: Editable<T>): Flow<Boolean> {
val valueFlow = getValue<T>(entry.key)
return if (entry.defaultValue == null) {
isOverriden(entry)
} else {
valueFlow
.combine(entry.defaultValue!!) { currentValue, defaultValue ->
currentValue == defaultValue
}.combine(isOverriden(entry)) { areEquals, isOverriden ->
isOverriden && !areEquals
}
}
}

private fun <T> getFromStorage(entry: Editable<T>) = tweaksRepository.get(entry)

internal suspend fun clearValue(entry: Editable<*>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EditableTweakEntryViewModel<T>(
}

fun isOverridden(): Flow<Boolean> =
tweaksBusinessLogic.isOverriddenOrDifferentFromDefaultValue(tweakEntry)
tweaksBusinessLogic.isOverridden(tweakEntry)

fun clearValue() {
this.value = null
Expand Down

0 comments on commit 265dfbb

Please sign in to comment.