From c65fd53a96e1c52a69b10e55e048635d42e61e77 Mon Sep 17 00:00:00 2001 From: machiav3lli Date: Sun, 29 May 2022 02:42:11 +0200 Subject: [PATCH] Add: SwitchPreference --- .../ui/compose/components/Controller.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/kotlin/com/looker/droidify/ui/compose/components/Controller.kt diff --git a/src/main/kotlin/com/looker/droidify/ui/compose/components/Controller.kt b/src/main/kotlin/com/looker/droidify/ui/compose/components/Controller.kt new file mode 100644 index 00000000..7e6a83f2 --- /dev/null +++ b/src/main/kotlin/com/looker/droidify/ui/compose/components/Controller.kt @@ -0,0 +1,39 @@ +package com.looker.droidify.ui.compose.components + +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.material.Switch +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp + +@Composable +fun SwitchPreference( + modifier: Modifier = Modifier, + text: String, + initSelected: Boolean, + onCheckedChanged: (Boolean) -> Unit +) { + Row( + modifier = modifier + .padding(horizontal = 6.dp) + .fillMaxWidth(), + verticalAlignment = Alignment.CenterVertically + ) { + val (selected, select) = remember { mutableStateOf(initSelected) } + Text(text = text) + Spacer(modifier = Modifier.weight(1f)) + Switch( + checked = selected, + onCheckedChange = { + select(it) + onCheckedChanged(it) + }) + } +}