From 5632477ad4b586ed27fe817015f5670357d1ab60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9luchu?= Date: Tue, 20 Aug 2024 16:12:34 +0200 Subject: [PATCH] Add delete all for DataStore and Include text style for preferences --- .../kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt | 4 ++++ .../ui/composables/preferences/PreferenceItem.kt | 7 ++++++- .../ui/composables/preferences/PreferenceSwitch.kt | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt b/jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt index b9961237..f27c5322 100644 --- a/jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt +++ b/jchucomponents-prefs/src/main/kotlin/com/jeluchu/prefs/datastore/DataStoreHelpers.kt @@ -114,6 +114,10 @@ class DataStoreHelpers { dataStore?.data?.first()?.toPreferences()?.get(key) ?: default } + fun clearAllPreferences() = runBlocking { + dataStore?.edit { preferences -> preferences.clear() } + } + companion object { var dataStore: DataStore? = null val Context.defaultPreferencesDataStore by preferencesDataStore(name = "default") diff --git a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceItem.kt b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceItem.kt index 68866457..7716fd74 100644 --- a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceItem.kt +++ b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceItem.kt @@ -19,6 +19,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.text.TextStyle import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.jeluchu.jchucomponents.ktx.colors.opacity @@ -35,6 +36,8 @@ fun PreferenceItem( onLongClickLabel: String? = null, onLongClick: (() -> Unit)? = null, onClickLabel: String? = null, + titleStyle: TextStyle = MaterialTheme.typography.titleLarge, + descriptionStyle: TextStyle = MaterialTheme.typography.bodyMedium, onClick: () -> Unit = {}, ) = Surface( modifier = Modifier.combinedClickable( @@ -95,13 +98,15 @@ fun PreferenceItem( PreferenceItemTitle( text = title, enabled = enabled, + style = titleStyle, color = contentColor ) if (description != null) PreferenceItemDescription( enabled = enabled, text = description, - color = contentColor + color = contentColor, + style = descriptionStyle ) } } diff --git a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceSwitch.kt b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceSwitch.kt index 8f6a508a..4685e481 100644 --- a/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceSwitch.kt +++ b/jchucomponents-ui/src/main/kotlin/com/jeluchu/jchucomponents/ui/composables/preferences/PreferenceSwitch.kt @@ -21,6 +21,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.text.TextStyle import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.jeluchu.jchucomponents.ktx.colors.opacity @@ -36,6 +37,8 @@ fun PreferenceSwitch( contentColor: Color = Color.DarkGray, colors: SwitchColors = SwitchDefaults.colors(), checkedIcon: ImageVector = Icons.Outlined.Check, + titleStyle: TextStyle = MaterialTheme.typography.titleLarge, + descriptionStyle: TextStyle = MaterialTheme.typography.bodyMedium, onClick: (() -> Unit) = {}, ) { val thumbContent: (@Composable () -> Unit)? = if (isChecked) { @@ -80,12 +83,14 @@ fun PreferenceSwitch( PreferenceItemTitle( text = title, enabled = enabled, + style = titleStyle, color = contentColor ) if (!description.isNullOrEmpty()) PreferenceItemDescription( enabled = enabled, text = description, - color = contentColor + color = contentColor, + style = descriptionStyle ) } Switch(