Skip to content

Commit

Permalink
added ability to cancel changes in bool preference as well (was alrea…
Browse files Browse the repository at this point in the history
…dy possible elsewhere) as well as in the KotPreference versions
  • Loading branch information
MFlisar committed Mar 30, 2024
1 parent 9c9648b commit 63a8e0d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object Demo2Prefs : SettingsModel(DataStoreStorage(name = "demo2_prefs")) {
// demo preferences
val stringValue by stringPref("some string")
val intValue by intPref(123)
val boolValue by boolPref(true)

// master/node example
val master1 by boolPref(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ private fun PreferenceScope.PreferenceInfoExamples() {
private fun PreferenceScope.PreferenceBoolExamples() {
val dataStore = LocalDataStore.current
val scope = rememberCoroutineScope()
val context = LocalContext.current
DemoPreferenceGroup(
type = "Bool",
details = "Click to see some switch/checkbox preference examples",
Expand Down Expand Up @@ -256,6 +257,22 @@ private fun PreferenceScope.PreferenceBoolExamples() {
},
title = { Text("Bool 5 - No Icon") }
)
// Cancel any change via callbak
val bool6 = dataStore.getBool("bool6", true).collectAsState(initial = true)
PreferenceBool(
style = PreferenceBool.Style.Switch,
value = bool6.value,
onValueChange = {
if (it) {
scope.launch(Dispatchers.IO) {
dataStore.update("bool5", it)
}
} else {
ToastHelper.show(context, "Change was rejected!")
}
},
title = { Text("Bool 6 - Can't be changed because onValueChange only accept true") }
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.DoubleArrow
import androidx.compose.material.icons.filled.Group
import androidx.compose.material.icons.filled.Numbers
import androidx.compose.material.icons.filled.TextSnippet
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import com.michaelflisar.composedemobaseactivity.classes.ToastHelper
import com.michaelflisar.composepreferences.core.PreferenceDivider
import com.michaelflisar.composepreferences.core.PreferenceSectionHeader
import com.michaelflisar.composepreferences.core.PreferenceScreen
import com.michaelflisar.composepreferences.core.PreferenceSectionHeader
import com.michaelflisar.composepreferences.core.PreferenceSubScreen
import com.michaelflisar.composepreferences.core.classes.PreferenceSettingsDefaults
import com.michaelflisar.composepreferences.core.classes.PreferenceStyleDefaults
Expand All @@ -26,6 +27,8 @@ import com.michaelflisar.composepreferences.screen.input.PreferenceInputText
@Composable
fun PrefScreenDemoKotPreferences1() {

val context = LocalContext.current

PreferenceScreen(
// optional settings for this screen...
settings = PreferenceSettingsDefaults.settings(
Expand Down Expand Up @@ -144,5 +147,27 @@ fun PrefScreenDemoKotPreferences1() {
)
}
}

// -----------
// Cancelable change
// -----------

PreferenceSectionHeader(
title = { Text("Reject changes") },
visible = Demo2Prefs.master2.asDependency { it }
)

PreferenceBool(
style = PreferenceBool.Style.Checkbox,
data = Demo2Prefs.boolValue.asPreferenceData() {
// we cancel the change and don't allow the user to change this preference this way
ToastHelper.show(context, "Change was rejected!")
false
},
visible = Demo2Prefs.master2.asDependency { it },
title = { Text("2b) Node") },
subtitle = { Text("Sub Item Master2") },
icon = { Icon(Icons.Default.Check, null) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import androidx.compose.runtime.MutableState
*/
data class PreferenceData<T>(
val value: T,
val onValueChange: (data: T) -> Unit = {}
val onValueChange: (data: T) -> Unit
) {
companion object
}
Expand All @@ -21,10 +21,14 @@ data class PreferenceData<T>(
*
* @return a [PreferenceData]
*/
fun <T> MutableState<T>.asPreferenceData(): PreferenceData<T> {
return PreferenceData(
value
) { value = it }
fun <T> MutableState<T>.asPreferenceData(
onValueChange: (data: T) -> Boolean = { true }
): PreferenceData<T> {
return PreferenceData(value) {
if (onValueChange(it)) {
value = it
}
}
}

//@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,23 @@ fun <T> StorageSetting<T>.collectSetting(
}

/**
simple extension function to plug in a setting as preference data provider and updater
* simple extension function to plug in a setting as preference data provider and updater
*
* @param onValueChange the value changed callback of this item (return true to accept the change, false to cancel it)
*/
@Composable
fun <T> StorageSetting<T>.asPreferenceData(): PreferenceData<T> {
fun <T> StorageSetting<T>.asPreferenceData(
onValueChange: (data: T) -> Boolean = { true }
): PreferenceData<T> {
val scope = rememberCoroutineScope()
val state = collectSetting()
return PreferenceData(state.value) {
scope.launch(Dispatchers.IO) {
update(it)
return PreferenceData(
state.value
) {
if (onValueChange(it)) {
scope.launch(Dispatchers.IO) {
update(it)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,10 @@ fun PreferenceScope.PreferenceBool(
preferenceStyle: PreferenceStyle = LocalPreferenceSettings.current.itemStyle,
itemSetup: PreferenceItemSetup = PreferenceBoolDefaults.itemSetup()
) {
var checked by remember(value) { mutableStateOf(value) }
val onClick = if (LocalPreferenceSettings.current.toggleBooleanOnItemClick) {
{
val updated = !checked
val updated = !value
onValueChange(updated)
checked = updated
}
} else null

Expand All @@ -111,16 +109,14 @@ fun PreferenceScope.PreferenceBool(
) {
when (style) {
PreferenceBool.Style.Checkbox -> {
Checkbox(checked = checked, onCheckedChange = {
Checkbox(checked = value, onCheckedChange = {
onValueChange(it)
checked = it
})
}

PreferenceBool.Style.Switch -> {
Switch(checked = checked, onCheckedChange = {
Switch(checked = value, onCheckedChange = {
onValueChange(it)
checked = it
})
}
}
Expand Down

0 comments on commit 63a8e0d

Please sign in to comment.