Skip to content

Commit 3230497

Browse files
committed
Refactor: Add PreferenceContainer and theme management
- Add `PreferenceContainer` for flexible stylization of `PreferenceItem`s. - Implement `ProvidePreferenceTheme` for managing color, typography, and spacing. - Migrate `BottomSheetListPreference` to commonMain. - Update `PreferenceScreen` to use `ProvidePreferenceTheme`. - Update `VERSION_NAME` to 1.1.0. - Update `defaultDataStoreManager` docstrings.
1 parent 02d1957 commit 3230497

5 files changed

Lines changed: 90 additions & 10 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.strabled.composepreferences
2+
3+
import androidx.compose.foundation.layout.padding
4+
import androidx.compose.foundation.lazy.LazyColumn
5+
import androidx.compose.material3.HorizontalDivider
6+
import androidx.compose.material3.MaterialTheme
7+
import androidx.compose.material3.Surface
8+
import androidx.compose.runtime.Composable
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.unit.dp
11+
12+
/**
13+
* A container for [PreferenceItem]s for more flexible stylization.
14+
* The container is styled using [MaterialTheme]. You can override the styling by providing custom [PreferenceColorTheme], [PreferenceTypography] and [PreferenceSpacing].
15+
* The container uses the [Surface] composable to provide the basic structure.
16+
*
17+
* @param modifier The [Modifier] to be applied to the container.
18+
* @param theme The [color theme][PreferenceColorTheme] for the preferences and the container. The default is [lightPreferenceColorTheme].
19+
* @param typography The typography to be used for the preferences and the container.
20+
* @param spacing The spacing to be used for the preferences and the screen.
21+
* @param content The content of the preference container, defined within a PreferenceScope.
22+
* @see Surface
23+
* @see PreferenceColorTheme
24+
* @see PreferenceTypography
25+
* @see PreferenceSpacing
26+
* @see PreferenceTheme
27+
* @see PreferenceItem
28+
*/
29+
@Composable
30+
fun PreferenceContainer(
31+
modifier: Modifier,
32+
theme: PreferenceColorTheme = PreferenceTheme.colorScheme,
33+
typography: PreferenceTypography = PreferenceTheme.typography,
34+
spacing: PreferenceSpacing = PreferenceTheme.spacing,
35+
content: PreferenceScope.() -> Unit
36+
) {
37+
val preferenceScope = PreferenceScopeImpl().apply(content)
38+
39+
ProvidePreferenceTheme(theme, typography, spacing) {
40+
Surface {
41+
LazyColumn(modifier = modifier) {
42+
items(preferenceScope.preferenceItemNumber) { index ->
43+
preferenceScope.getPreferenceItem(index)()
44+
45+
if (PreferenceTheme.spacing.dividerThickness != 0.dp && preferenceScope.needsDivider(index)) {
46+
HorizontalDivider(
47+
color = PreferenceTheme.colorScheme.dividerColor,
48+
thickness = PreferenceTheme.spacing.dividerThickness,
49+
modifier = Modifier.padding(horizontal = PreferenceTheme.spacing.dividerIndent)
50+
)
51+
}
52+
}
53+
item {
54+
preferenceScope.getFooter()()
55+
}
56+
}
57+
}
58+
}
59+
}

ComposePreferences/src/commonMain/kotlin/com/strabled/composepreferences/PreferenceProviders.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import com.strabled.composepreferences.utilis.PreferenceBuilder
1010
import com.strabled.composepreferences.utilis.buildPreferences
1111

1212
/**
13-
* Provides a default instance of [DataStoreManager] using the current [LocalContext].
14-
* The default name for the [DataStore] is "preferences".
13+
* Provides a platform-specific default implementation of [DataStoreManager].
14+
*
15+
* This function must be implemented in each platform module (e.g., Android, iOS, Desktop).
16+
* It returns a concrete [DataStoreManager] instance, typically for managing DataStore-based persistence.
17+
*
18+
* @return A [DataStoreManager] instance for the current platform.
1519
*/
1620
@Composable
1721
expect fun defaultDataStoreManager(): DataStoreManager
@@ -47,6 +51,26 @@ fun ProvideDataStoreManager(dataStoreManager: DataStoreManager = defaultDataStor
4751
CompositionLocalProvider(LocalDataStoreManager provides dataStoreManager, content = content)
4852
}
4953

54+
/**
55+
* Provides a theme for preferences, including color, typography, and spacing.
56+
* This function sets the current value of [LocalPreferenceColorTheme], [LocalPreferenceTypography], and [LocalPreferenceSpacing].
57+
* It allows the composable content to access the provided theme settings.
58+
*
59+
* @param colorTheme The color theme to be used.
60+
* @param typography The typography settings to be used.
61+
* @param spacing The spacing settings to be used.
62+
* @param content The composable content that can access the provided theme.
63+
*/
64+
@Composable
65+
fun ProvidePreferenceTheme(colorTheme: PreferenceColorTheme, typography: PreferenceTypography, spacing: PreferenceSpacing, content: @Composable () -> Unit) {
66+
CompositionLocalProvider(
67+
LocalPreferenceColorTheme provides colorTheme,
68+
LocalPreferenceTypography provides typography,
69+
LocalPreferenceSpacing provides spacing,
70+
content = content
71+
)
72+
}
73+
5074
/**
5175
* A convenience function to set preferences in the current [DataStoreManager].
5276
* This function can only be used inside of [ProvideDataStoreManager].

ComposePreferences/src/commonMain/kotlin/com/strabled/composepreferences/PreferenceScreen.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ fun PreferenceScreen(
4444
) {
4545
val preferenceScope = PreferenceScopeImpl().apply(content)
4646

47-
CompositionLocalProvider(
48-
LocalPreferenceColorTheme provides theme,
49-
LocalPreferenceTypography provides typography,
50-
LocalPreferenceSpacing provides spacing
51-
) {
47+
ProvidePreferenceTheme(theme, typography, spacing) {
5248
Scaffold(
5349
topBar = scaffoldComponents.topBar,
5450
bottomBar = scaffoldComponents.bottomBar,

ComposePreferences/src/main/java/com/strabled/composepreferences/preferences/BottomSheetListPreference.kt renamed to ComposePreferences/src/commonMain/kotlin/com/strabled/composepreferences/preferences/BottomSheetListPreference.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ import androidx.compose.runtime.setValue
1818
import androidx.compose.ui.Alignment
1919
import androidx.compose.ui.Modifier
2020
import androidx.compose.ui.semantics.Role
21+
import com.strabled.composepreferences.PreferenceItem
2122
import com.strabled.composepreferences.utilis.*
2223

2324
/**
24-
* A [Composable] function that displays a [preference item][com.strabled.composepreferences.PreferenceItem] which, when clicked, shows a [bottom sheet][PreferenceBottomSheet]
25-
* with a list of selectable items. The selected item is stored in a [DataStore].
25+
* A [Composable] function that displays a [preference item][PreferenceItem] which, when clicked, shows a [bottom sheet][PreferenceBottomSheet]
26+
* with a list of selectable items. The selected item is stored with a [DataStoreManager].
2627
*
2728
* @param T The type of the [Preference] value.
2829
* @param preference The [DataStore preference][Preference] to be managed.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ SONATYPE_AUTOMATIC_RELEASE=true
2525

2626
GROUP=io.github.strabled
2727
POM_ARTIFACT_ID=ComposePreferences
28-
VERSION_NAME=1.0.2
28+
VERSION_NAME=1.1.0
2929

3030
POM_NAME=Compose Preferences
3131
POM_DESCRIPTION=A library for creating custom preferences with Jetpack Compose for Android.

0 commit comments

Comments
 (0)