|
| 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 | +} |
0 commit comments