Skip to content

Commit e3ff3a4

Browse files
committed
Add Compose Settings library
1 parent 8118314 commit e3ff3a4

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ dependencies {
6262
implementation(libs.ui.tooling.preview)
6363
implementation(libs.material3)
6464
implementation(libs.bundles.datastore)
65+
implementation(libs.bundles.settings)
6566
testImplementation(libs.junit)
6667
androidTestImplementation(libs.androidx.test.ext.junit)
6768
androidTestImplementation(libs.espresso.core)

app/src/main/java/io/github/janmalch/composeprotosettings/MainActivity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.compose.material3.Text
1010
import androidx.compose.runtime.Composable
1111
import androidx.compose.ui.Modifier
1212
import androidx.compose.ui.tooling.preview.Preview
13+
import io.github.janmalch.composeprotosettings.ui.preference.AppSettings
1314
import io.github.janmalch.composeprotosettings.ui.theme.ComposeProtoSettingsTheme
1415

1516
class MainActivity : ComponentActivity() {
@@ -22,7 +23,7 @@ class MainActivity : ComponentActivity() {
2223
modifier = Modifier.fillMaxSize(),
2324
color = MaterialTheme.colorScheme.background
2425
) {
25-
Greeting("Android")
26+
AppSettings()
2627
}
2728
}
2829
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.github.janmalch.composeprotosettings.ui.preference
2+
3+
import androidx.compose.material.icons.Icons
4+
import androidx.compose.material.icons.filled.Settings
5+
import androidx.compose.material3.Icon
6+
import androidx.compose.material3.Text
7+
import androidx.compose.runtime.Composable
8+
import androidx.compose.ui.tooling.preview.Preview
9+
import com.alorma.compose.settings.storage.base.rememberBooleanSettingState
10+
import com.alorma.compose.settings.storage.datastore.proto.rememberProtoDataStoreSettingState
11+
import com.alorma.compose.settings.storage.datastore.proto.rememberProtoDataStoreState
12+
import com.alorma.compose.settings.storage.datastore.proto.rememberProtoDataStoreTransformSettingState
13+
import com.alorma.compose.settings.ui.SettingsCheckbox
14+
import io.github.janmalch.composeprotosettings.preferences.UserPreferencesSerializer
15+
import io.github.janmalch.composeprotosettings.preferences.copy
16+
import io.github.janmalch.composeprotosettings.ui.theme.ComposeProtoSettingsTheme
17+
18+
@Composable
19+
fun AppSettings() {
20+
val dataStoreState = rememberProtoDataStoreState(
21+
// "filename" is optional, but it is recommended to set the protobuf file name so that it wouldn't conflict with other datastore definition.
22+
filename = "compose_settings_datastore_proto.pb",
23+
serializer = UserPreferencesSerializer,
24+
)
25+
val state = rememberProtoDataStoreTransformSettingState(
26+
protoDataStoreState = dataStoreState,
27+
encoder = { saved, newValue -> saved.copy { showCompleted = newValue } },
28+
decoder = { it.showCompleted }
29+
)
30+
31+
SettingsCheckbox(
32+
state = state,
33+
icon = { Icon(imageVector = Icons.Default.Settings, contentDescription = "Wifi") },
34+
title = { Text(text = "Hello") },
35+
subtitle = { Text(text = "This is a longer text") },
36+
)
37+
}
38+
39+
@Preview(showBackground = true)
40+
@Composable
41+
fun AppSettingsPreview() {
42+
ComposeProtoSettingsTheme {
43+
AppSettings()
44+
}
45+
}

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ compose-bom = "2023.06.01"
1111
datastore = "1.0.0"
1212
protobuf = "3.23.2" # "3.23.3" is broken
1313
protobuf-plugin = "0.9.3"
14+
compose-settings = "0.27.0" # https://central.sonatype.com/artifact/com.github.alorma/compose-settings-ui-m3/0.27.0, https://github.com/alorma/Compose-Settings
1415

1516
[libraries]
1617
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
@@ -33,6 +34,8 @@ protobuf-java = { group = "com.google.protobuf", name = "protobuf-javalite", ver
3334
protobuf-kotlin = { group = "com.google.protobuf", name = "protobuf-kotlin-lite", version.ref = "protobuf" }
3435
protoc = { group = "com.google.protobuf", name = "protoc", version.ref = "protobuf" }
3536

37+
compose-settings-ui = { group = "com.github.alorma", name = "compose-settings-ui-m3", version.ref = "compose-settings" }
38+
compose-settings-proto = { group = "com.github.alorma", name = "compose-settings-storage-datastore-proto", version.ref = "compose-settings" }
3639

3740
[plugins]
3841
com-android-application = { id = "com.android.application", version.ref = "agp" }
@@ -41,3 +44,4 @@ com-google-protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plug
4144

4245
[bundles]
4346
datastore = ["datatore", "protobuf-java", "protobuf-kotlin"]
47+
settings = ["compose-settings-ui", "compose-settings-proto"]

0 commit comments

Comments
 (0)