Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Add preference for enabling the "New Edit" screen
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed May 29, 2022
1 parent 3f04d5a commit fe82974
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.ivy.wallet.domain.data.preference

import androidx.datastore.preferences.core.booleanPreferencesKey

data class NewEditScreenPref(
override val value: Boolean = false
) : Preference<Boolean> {
override val key = booleanPreferencesKey("exp_new_edit_screen")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ sealed class ExpEvent {
object Load : ExpEvent()

data class SetSmallTrnsPref(val newValue: Boolean) : ExpEvent()

data class SetNewEditPref(val newValue: Boolean) : ExpEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ sealed class ExpState {
object Initial : ExpState()

data class Loaded(
val smallTrnsPref: Boolean
val smallTrnsPref: Boolean,
val newEditScreen: Boolean
) : ExpState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ private fun LoadedState(
{ ExpEvent.SetSmallTrnsPref(it) } then2 onEvent
)
}

item {
SpacerVer(height = 16.dp)

BooleanPreference(
name = "New edit screen",
value = state.newEditScreen,
onValueChanged = forward<Boolean>() then2
{ ExpEvent.SetNewEditPref(it) } then2 onEvent
)
}
}
}

Expand Down Expand Up @@ -117,7 +128,8 @@ private fun Preview() {
IvyWalletPreview {
UI(
state = ExpState.Loaded(
smallTrnsPref = false
smallTrnsPref = false,
newEditScreen = true
),
onEvent = {}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.ivy.frp.thenInvokeAfter
import com.ivy.frp.viewmodel.FRPViewModel
import com.ivy.wallet.domain.action.settings.preference.PreferenceAct
import com.ivy.wallet.domain.action.settings.preference.SetPreferenceAct
import com.ivy.wallet.domain.data.preference.NewEditScreenPref
import com.ivy.wallet.domain.data.preference.SmallTrnsPref
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -14,23 +15,50 @@ import javax.inject.Inject
@HiltViewModel
class ExperimentalViewModel @Inject constructor(
private val smallTrnsPrefAct: PreferenceAct<SmallTrnsPref, Boolean>,
private val setSmallTrnsPrefAct: SetPreferenceAct<SmallTrnsPref, Boolean>
private val setSmallTrnsPrefAct: SetPreferenceAct<SmallTrnsPref, Boolean>,

private val newEditScreenPrefAct: PreferenceAct<NewEditScreenPref, Boolean>,
private val setNewEditScreenPrefAct: SetPreferenceAct<NewEditScreenPref, Boolean>
) : FRPViewModel<ExpState, ExpEvent>() {
override val _state: MutableStateFlow<ExpState> = MutableStateFlow(ExpState.Initial)

override suspend fun handleEvent(event: ExpEvent): suspend () -> ExpState = when (event) {
ExpEvent.Load -> load(Unit)
is ExpEvent.SetSmallTrnsPref -> setSmallTrnsPref(event)
is ExpEvent.SetNewEditPref -> setNewEditPref(event)
}

private fun load(unit: Unit) = loadSmallTrnsPref() then ::loadNewEditScreenPref

private fun loadSmallTrnsPref() = SmallTrnsPref() asParamTo smallTrnsPrefAct then { value ->
updateLoaded {
it.copy(smallTrnsPref = value ?: false)
}
}

private fun load(unit: Unit) = SmallTrnsPref() asParamTo smallTrnsPrefAct then { smallTrns ->
updateState {
ExpState.Loaded(
smallTrnsPref = smallTrns ?: false
private suspend fun loadNewEditScreenPref(state: ExpState) = NewEditScreenPref() asParamTo
newEditScreenPrefAct thenInvokeAfter { value ->
updateLoaded {
it.copy(newEditScreen = value ?: false)
}
}

private suspend fun updateLoaded(update: (ExpState.Loaded) -> ExpState.Loaded) = updateState {
when (it) {
ExpState.Initial -> update(
ExpState.Loaded(
smallTrnsPref = false,
newEditScreen = false
)
)
is ExpState.Loaded -> update(it)
}
}

private suspend fun setSmallTrnsPref(event: ExpEvent.SetSmallTrnsPref) =
SmallTrnsPref(value = event.newValue) asParamTo setSmallTrnsPrefAct thenInvokeAfter ::load

private suspend fun setNewEditPref(event: ExpEvent.SetNewEditPref) =
NewEditScreenPref(value = event.newValue) asParamTo
setNewEditScreenPrefAct thenInvokeAfter ::load
}

0 comments on commit fe82974

Please sign in to comment.