diff --git a/app/src/main/java/com/vanced/manager/ui/components/preference/CheckboxDialogPreference.kt b/app/src/main/java/com/vanced/manager/ui/components/preference/CheckboxDialogPreference.kt new file mode 100644 index 00000000..4eae7392 --- /dev/null +++ b/app/src/main/java/com/vanced/manager/ui/components/preference/CheckboxDialogPreference.kt @@ -0,0 +1,67 @@ +package com.vanced.manager.ui.components.preference + +import androidx.annotation.StringRes +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.toMutableStateList +import androidx.compose.ui.Modifier +import com.vanced.manager.R +import com.vanced.manager.ui.components.button.ManagerThemedTextButton +import com.vanced.manager.ui.components.list.CheckboxItem +import com.vanced.manager.ui.preferences.CheckboxPreference +import com.vanced.manager.ui.preferences.ManagerPreference +import kotlinx.coroutines.launch + +@Composable +fun CheckboxDialogPreference( + @StringRes preferenceTitle: Int, + @StringRes preferenceDescription: Int? = null, + preference: ManagerPreference>, + trailing: @Composable () -> Unit = {}, + buttons: List, + onSave: (checkedButtons: List) -> Unit = {} +) { + val selectedButtons = remember { preference.value.value.toMutableStateList() } + val coroutineScope = rememberCoroutineScope() + DialogPreference( + preferenceTitleId = preferenceTitle, + preferenceDescriptionId = preferenceDescription, + trailing = trailing, + buttons = { isShown -> + ManagerThemedTextButton( + stringId = R.string.dialog_button_save, + modifier = Modifier.fillMaxWidth(), + onClick = { + coroutineScope.launch { + isShown.value = false + preference.save(selectedButtons.toSet()) + onSave(selectedButtons) + } + } + ) + } + ) { + LazyColumn( + modifier = Modifier + ) { + items(buttons) { button -> + val (title, key) = button + CheckboxItem( + text = title, + isChecked = selectedButtons.contains(key), + onCheck = { isChecked -> + if (isChecked) { + selectedButtons.add(key) + } else { + selectedButtons.remove(key) + } + } + ) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/vanced/manager/ui/components/preference/RadiobuttonDialogPreference.kt b/app/src/main/java/com/vanced/manager/ui/components/preference/RadiobuttonDialogPreference.kt index bd4d0423..7018348e 100644 --- a/app/src/main/java/com/vanced/manager/ui/components/preference/RadiobuttonDialogPreference.kt +++ b/app/src/main/java/com/vanced/manager/ui/components/preference/RadiobuttonDialogPreference.kt @@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items -import androidx.compose.material.Text import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp @@ -36,9 +35,9 @@ fun RadiobuttonDialogPreference( modifier = Modifier.fillMaxWidth(), onClick = { coroutineScope.launch { + isShown.value = false preference.save(currentSelection) onSave(currentSelection) - isShown.value = false } } )