/
SettingsListMultiSelect.kt
132 lines (123 loc) · 4.6 KB
/
SettingsListMultiSelect.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.alorma.compose.settings.ui
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.selection.selectable
import androidx.compose.material.AlertDialog
import androidx.compose.material.Checkbox
import androidx.compose.material.MaterialTheme
import androidx.compose.material.ProvideTextStyle
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import com.alorma.compose.settings.storage.base.SettingValueState
import com.alorma.compose.settings.storage.base.rememberIntSetSettingState
@Composable
fun SettingsListMultiSelect(
modifier: Modifier = Modifier,
state: SettingValueState<Set<Int>> = rememberIntSetSettingState(),
title: @Composable () -> Unit,
items: List<String>,
icon: @Composable() (() -> Unit)? = null,
confirmButton: String,
useSelectedValuesAsSubtitle: Boolean = true,
subtitle: @Composable() (() -> Unit)? = null,
action: @Composable() (() -> Unit)? = null,
) {
if (state.value.any { index -> index >= items.size }) {
throw IndexOutOfBoundsException("Current indexes for $title list setting cannot be grater than items size")
}
var showDialog by remember { mutableStateOf(false) }
val safeSubtitle = if (state.value.size >= 0 && useSelectedValuesAsSubtitle) {
{
Text(text = state.value.map { index -> items[index] }
.joinToString(separator = ", ") { it })
}
} else subtitle
SettingsMenuLink(
modifier = modifier,
icon = icon,
title = title,
subtitle = safeSubtitle,
action = action,
onClick = { showDialog = true },
)
if (!showDialog) return
val onAdd: (Int) -> Unit = { selectedIndex ->
val mutable = state.value.toMutableSet()
mutable.add(selectedIndex)
state.value = mutable
}
val onRemove: (Int) -> Unit = { selectedIndex ->
val mutable = state.value.toMutableSet()
mutable.remove(selectedIndex)
state.value = mutable
}
AlertDialog(
title = title,
text = {
Column {
subtitle?.invoke()
items.forEachIndexed { index, item ->
val isSelected by rememberUpdatedState(newValue = state.value.contains(index))
Row(
modifier = Modifier
.fillMaxWidth()
.selectable(
role = Role.Checkbox,
selected = isSelected,
onClick = {
if (isSelected) {
onRemove(index)
} else {
onAdd(index)
}
}
)
.padding(
top = 16.dp,
bottom = 16.dp
)
) {
Text(
text = item,
style = MaterialTheme.typography.body1,
modifier = Modifier.weight(1f)
)
Checkbox(
checked = isSelected,
onCheckedChange = { checked ->
if (checked) {
onRemove(index)
} else {
onAdd(index)
}
}
)
}
}
}
},
onDismissRequest = { showDialog = false },
confirmButton = {
TextButton(
onClick = { showDialog = false },
) {
ProvideTextStyle(
value = MaterialTheme.typography.button.copy(fontFeatureSettings = "c2sc, smcp")
) {
Text(text = confirmButton)
}
}
}
)
}