This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a way for setting User Preferences in DataStore
- Loading branch information
1 parent
741e740
commit 3f04d5a
Showing
17 changed files
with
297 additions
and
34 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
app/src/main/java/com/ivy/wallet/domain/action/settings/preference/PreferenceAct.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,14 @@ | ||
package com.ivy.wallet.domain.action.settings.preference | ||
|
||
import com.ivy.frp.action.FPAction | ||
import com.ivy.frp.asParamTo | ||
import com.ivy.wallet.domain.data.preference.Preference | ||
import com.ivy.wallet.io.persistence.datastore.IvyDataStore | ||
import javax.inject.Inject | ||
|
||
class PreferenceAct<P : Preference<V>, V> @Inject constructor( | ||
private val dataStore: IvyDataStore | ||
) : FPAction<P, V?>() { | ||
override suspend fun P.compose(): suspend () -> V? = | ||
this.key asParamTo dataStore::get | ||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/com/ivy/wallet/domain/action/settings/preference/SetPreferenceAct.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,18 @@ | ||
package com.ivy.wallet.domain.action.settings.preference | ||
|
||
import com.ivy.frp.action.FPAction | ||
import com.ivy.frp.asParamTo | ||
import com.ivy.wallet.domain.data.preference.Preference | ||
import com.ivy.wallet.io.persistence.datastore.IvyDataStore | ||
import javax.inject.Inject | ||
|
||
class SetPreferenceAct<P : Preference<V>, V> @Inject constructor( | ||
private val dataStore: IvyDataStore | ||
) : FPAction<P, Unit>() { | ||
override suspend fun P.compose(): suspend () -> Unit = | ||
when (val newValue = value) { | ||
newValue != null -> (key to newValue) asParamTo dataStore::insert | ||
else -> key asParamTo dataStore::remove | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/ivy/wallet/domain/data/preference/Preference.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,8 @@ | ||
package com.ivy.wallet.domain.data.preference | ||
|
||
import androidx.datastore.preferences.core.Preferences | ||
|
||
interface Preference<T> { | ||
val key: Preferences.Key<T> | ||
val value: T? | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/ivy/wallet/domain/data/preference/SmallTrnsPref.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,9 @@ | ||
package com.ivy.wallet.domain.data.preference | ||
|
||
import androidx.datastore.preferences.core.booleanPreferencesKey | ||
|
||
data class SmallTrnsPref( | ||
override val value: Boolean = false | ||
) : Preference<Boolean> { | ||
override val key = booleanPreferencesKey("exp_small_trns") | ||
} |
44 changes: 44 additions & 0 deletions
44
app/src/main/java/com/ivy/wallet/io/persistence/datastore/IvyDataStore.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,44 @@ | ||
package com.ivy.wallet.io.persistence.datastore | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class IvyDataStore @Inject constructor( | ||
@ApplicationContext private val appContext: Context | ||
) { | ||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "ivy_wallet") | ||
|
||
suspend fun <T> insert(pair: Preferences.Pair<T>) { | ||
appContext.dataStore.edit { | ||
it.putAll(pair) | ||
} | ||
} | ||
|
||
suspend fun <T> insert( | ||
key: Preferences.Key<T>, | ||
value: T | ||
) { | ||
appContext.dataStore.edit { | ||
it[key] = value | ||
} | ||
} | ||
|
||
suspend fun <T> remove(key: Preferences.Key<T>) { | ||
appContext.dataStore.edit { | ||
it.remove(key = key) | ||
} | ||
} | ||
|
||
suspend fun <T> get(key: Preferences.Key<T>): T? = appContext.dataStore.data.map { | ||
it[key] | ||
}.firstOrNull() | ||
} |
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
29 changes: 0 additions & 29 deletions
29
app/src/main/java/com/ivy/wallet/ui/architecture/UIArchitecture.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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/ivy/wallet/ui/settings/experimental/ExpEvent.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,7 @@ | ||
package com.ivy.wallet.ui.settings.experimental | ||
|
||
sealed class ExpEvent { | ||
object Load : ExpEvent() | ||
|
||
data class SetSmallTrnsPref(val newValue: Boolean) : ExpEvent() | ||
} |
9 changes: 9 additions & 0 deletions
9
app/src/main/java/com/ivy/wallet/ui/settings/experimental/ExpState.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,9 @@ | ||
package com.ivy.wallet.ui.settings.experimental | ||
|
||
sealed class ExpState { | ||
object Initial : ExpState() | ||
|
||
data class Loaded( | ||
val smallTrnsPref: Boolean | ||
) : ExpState() | ||
} |
125 changes: 125 additions & 0 deletions
125
app/src/main/java/com/ivy/wallet/ui/settings/experimental/ExperimentalScreen.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,125 @@ | ||
package com.ivy.wallet.ui.settings.experimental | ||
|
||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.clickable | ||
import androidx.compose.foundation.layout.BoxWithConstraintsScope | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.ivy.design.l0_system.UI | ||
import com.ivy.design.l0_system.style | ||
import com.ivy.design.l1_buildingBlocks.* | ||
import com.ivy.frp.forward | ||
import com.ivy.frp.then2 | ||
import com.ivy.frp.view.FRP | ||
import com.ivy.frp.view.navigation.Screen | ||
import com.ivy.wallet.ui.IvyWalletPreview | ||
import com.ivy.wallet.ui.theme.components.IvySwitch | ||
|
||
object ExperimentalScreen : Screen | ||
|
||
@Composable | ||
fun BoxWithConstraintsScope.ExperimentalScreen(screen: ExperimentalScreen) { | ||
FRP<ExpState, ExpEvent, ExperimentalViewModel>( | ||
initialEvent = ExpEvent.Load | ||
) { state, onEvent -> | ||
UI(state, onEvent) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun UI( | ||
state: ExpState, | ||
|
||
onEvent: (ExpEvent) -> Unit | ||
) { | ||
ColumnRoot( | ||
modifier = Modifier.padding(horizontal = 24.dp) | ||
) { | ||
SpacerVer(height = 24.dp) | ||
|
||
IvyText(text = "Experimental", typo = UI.typo.h2) | ||
|
||
SpacerVer(height = 32.dp) | ||
|
||
when (state) { | ||
ExpState.Initial -> {} | ||
is ExpState.Loaded -> LoadedState(state = state, onEvent = onEvent) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun LoadedState( | ||
state: ExpState.Loaded, | ||
onEvent: (ExpEvent) -> Unit | ||
) { | ||
LazyColumn { | ||
item { | ||
BooleanPreference( | ||
name = "Small transactions", | ||
value = state.smallTrnsPref, | ||
onValueChanged = forward<Boolean>() then2 | ||
{ ExpEvent.SetSmallTrnsPref(it) } then2 onEvent | ||
) | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
private fun BooleanPreference( | ||
name: String, | ||
value: Boolean, | ||
onValueChanged: (Boolean) -> Unit | ||
) { | ||
Row( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.clip(UI.shapes.r2) | ||
.background(UI.colors.medium, UI.shapes.r4) | ||
.clickable { | ||
onValueChanged(!value) | ||
} | ||
.padding(vertical = 20.dp), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
SpacerHor(width = 16.dp) | ||
|
||
IvyText( | ||
text = name, | ||
typo = UI.typo.b2.style( | ||
fontWeight = FontWeight.Bold | ||
) | ||
) | ||
|
||
SpacerWeight(weight = 1f) | ||
|
||
IvySwitch( | ||
enabled = value, | ||
onEnabledChange = forward<Boolean>() then2 onValueChanged | ||
) | ||
|
||
SpacerHor(width = 16.dp) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun Preview() { | ||
IvyWalletPreview { | ||
UI( | ||
state = ExpState.Loaded( | ||
smallTrnsPref = false | ||
), | ||
onEvent = {} | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
app/src/main/java/com/ivy/wallet/ui/settings/experimental/ExperimentalViewModel.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,36 @@ | ||
package com.ivy.wallet.ui.settings.experimental | ||
|
||
import com.ivy.frp.asParamTo | ||
import com.ivy.frp.then | ||
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.SmallTrnsPref | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ExperimentalViewModel @Inject constructor( | ||
private val smallTrnsPrefAct: PreferenceAct<SmallTrnsPref, Boolean>, | ||
private val setSmallTrnsPrefAct: SetPreferenceAct<SmallTrnsPref, 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) | ||
} | ||
|
||
private fun load(unit: Unit) = SmallTrnsPref() asParamTo smallTrnsPrefAct then { smallTrns -> | ||
updateState { | ||
ExpState.Loaded( | ||
smallTrnsPref = smallTrns ?: false | ||
) | ||
} | ||
} | ||
|
||
private suspend fun setSmallTrnsPref(event: ExpEvent.SetSmallTrnsPref) = | ||
SmallTrnsPref(value = event.newValue) asParamTo setSmallTrnsPrefAct thenInvokeAfter ::load | ||
} |
Oops, something went wrong.