Skip to content

Commit 1862d3b

Browse files
committed
#1871 clean up ChooseSettingScreen.kt and add previews
1 parent 16ef439 commit 1862d3b

7 files changed

Lines changed: 106 additions & 41 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/BaseMainNavHost.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import androidx.navigation.compose.NavHost
1818
import androidx.navigation.compose.composable
1919
import io.github.sds100.keymapper.base.actions.ChooseActionScreen
2020
import io.github.sds100.keymapper.base.actions.ChooseActionViewModel
21+
import io.github.sds100.keymapper.base.actions.ChooseSettingScreen
2122
import io.github.sds100.keymapper.base.actions.ConfigShellCommandViewModel
2223
import io.github.sds100.keymapper.base.actions.ShellCommandActionScreen
2324
import io.github.sds100.keymapper.base.actions.uielement.InteractUiElementScreen
@@ -164,6 +165,13 @@ fun BaseMainNavHost(
164165
)
165166
}
166167

168+
composable<NavDestination.ChooseSetting> {
169+
ChooseSettingScreen(
170+
modifier = Modifier.fillMaxSize(),
171+
viewModel = hiltViewModel(),
172+
)
173+
}
174+
167175
composableDestinations()
168176
}
169177
}

base/src/main/java/io/github/sds100/keymapper/base/actions/ChooseSettingScreen.kt

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@ import androidx.compose.material3.Text
2222
import androidx.compose.material3.TopAppBar
2323
import androidx.compose.runtime.Composable
2424
import androidx.compose.runtime.getValue
25-
import androidx.compose.runtime.mutableStateOf
26-
import androidx.compose.runtime.remember
27-
import androidx.compose.runtime.setValue
2825
import androidx.compose.ui.Alignment
2926
import androidx.compose.ui.Modifier
3027
import androidx.compose.ui.res.stringResource
28+
import androidx.compose.ui.tooling.preview.Preview
3129
import androidx.compose.ui.unit.dp
3230
import androidx.lifecycle.compose.collectAsStateWithLifecycle
3331
import io.github.sds100.keymapper.base.R
34-
import io.github.sds100.keymapper.base.utils.ui.compose.KeyMapperDropdownMenu
32+
import io.github.sds100.keymapper.base.compose.KeyMapperTheme
33+
import io.github.sds100.keymapper.base.utils.ui.compose.KeyMapperSegmentedButtonRow
3534
import io.github.sds100.keymapper.base.utils.ui.compose.SearchAppBarActions
3635
import io.github.sds100.keymapper.common.utils.State
3736
import io.github.sds100.keymapper.system.settings.SettingType
@@ -97,28 +96,21 @@ private fun ChooseSettingScreen(
9796
.padding(innerPadding),
9897
) {
9998
Column(modifier = Modifier.fillMaxSize()) {
100-
// Setting type dropdown
101-
var expanded by remember { mutableStateOf(false) }
102-
103-
KeyMapperDropdownMenu(
99+
KeyMapperSegmentedButtonRow(
104100
modifier = Modifier
105101
.fillMaxWidth()
106102
.padding(16.dp),
107-
expanded = expanded,
108-
onExpandedChange = { expanded = it },
109-
label = { Text(stringResource(R.string.modify_setting_type_label)) },
110-
selectedValue = settingType,
111-
values = listOf(
103+
buttonStates = listOf(
112104
SettingType.SYSTEM to stringResource(R.string.modify_setting_type_system),
113105
SettingType.SECURE to stringResource(R.string.modify_setting_type_secure),
114106
SettingType.GLOBAL to stringResource(R.string.modify_setting_type_global),
115107
),
116-
onValueChanged = onSettingTypeChange,
108+
selectedState = settingType,
109+
onStateSelected = onSettingTypeChange,
117110
)
118111

119112
HorizontalDivider()
120113

121-
// Settings list
122114
when (state) {
123115
State.Loading -> {
124116
Box(
@@ -159,3 +151,64 @@ private fun ChooseSettingScreen(
159151
}
160152
}
161153
}
154+
155+
@OptIn(ExperimentalMaterial3Api::class)
156+
@Preview
157+
@Composable
158+
private fun PreviewWithData() {
159+
KeyMapperTheme {
160+
ChooseSettingScreen(
161+
state = State.Data(
162+
listOf(
163+
SettingItem("adb_enabled", "0"),
164+
SettingItem("airplane_mode_on", "0"),
165+
SettingItem("bluetooth_on", "1"),
166+
SettingItem("screen_brightness", "128"),
167+
SettingItem("wifi_on", "1"),
168+
),
169+
),
170+
settingType = SettingType.GLOBAL,
171+
)
172+
}
173+
}
174+
175+
@OptIn(ExperimentalMaterial3Api::class)
176+
@Preview
177+
@Composable
178+
private fun PreviewLoading() {
179+
KeyMapperTheme {
180+
ChooseSettingScreen(
181+
state = State.Loading,
182+
settingType = SettingType.SECURE,
183+
)
184+
}
185+
}
186+
187+
@OptIn(ExperimentalMaterial3Api::class)
188+
@Preview
189+
@Composable
190+
private fun PreviewEmpty() {
191+
KeyMapperTheme {
192+
ChooseSettingScreen(
193+
state = State.Data(emptyList()),
194+
settingType = SettingType.SYSTEM,
195+
)
196+
}
197+
}
198+
199+
@OptIn(ExperimentalMaterial3Api::class)
200+
@Preview
201+
@Composable
202+
private fun PreviewWithSearch() {
203+
KeyMapperTheme {
204+
ChooseSettingScreen(
205+
state = State.Data(
206+
listOf(
207+
SettingItem("bluetooth_on", "1"),
208+
),
209+
),
210+
query = "bluetooth",
211+
settingType = SettingType.SECURE,
212+
)
213+
}
214+
}

base/src/main/java/io/github/sds100/keymapper/base/actions/ChooseSettingViewModel.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import kotlinx.coroutines.launch
2121
import kotlinx.serialization.Serializable
2222
import kotlinx.serialization.json.Json
2323

24-
data class SettingItem(val key: String, val value: String?)
25-
2624
@HiltViewModel
2725
class ChooseSettingViewModel @Inject constructor(
2826
private val settingsAdapter: SettingsAdapter,
@@ -33,10 +31,9 @@ class ChooseSettingViewModel @Inject constructor(
3331
ResourceProvider by resourceProvider,
3432
DialogProvider by dialogProvider,
3533
NavigationProvider by navigationProvider {
36-
3734
val searchQuery = MutableStateFlow<String?>(null)
38-
val selectedSettingType = MutableStateFlow(SettingType.SYSTEM)
3935

36+
val selectedSettingType = MutableStateFlow(SettingType.SYSTEM)
4037
val settings: StateFlow<State<List<SettingItem>>> =
4138
combine(selectedSettingType, searchQuery) { type, query ->
4239
val allSettings = settingsAdapter.getAll(type)
@@ -71,6 +68,8 @@ class ChooseSettingViewModel @Inject constructor(
7168
}
7269
}
7370

71+
data class SettingItem(val key: String, val value: String?)
72+
7473
@Serializable
7574
data class ChooseSettingResult(
7675
val settingType: SettingType,

base/src/main/java/io/github/sds100/keymapper/base/actions/CreateActionDelegate.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ class CreateActionDelegate(
222222
modifySettingActionBottomSheetState?.copy(settingKey = key)
223223
}
224224

225+
fun onChooseExistingSettingClick() {
226+
val type = modifySettingActionBottomSheetState?.settingType ?: return
227+
val destination = NavDestination.ChooseSetting(settingType = type)
228+
229+
coroutineScope.launch {
230+
val setting = navigate("choose_setting", destination) ?: return@launch
231+
232+
modifySettingActionBottomSheetState = modifySettingActionBottomSheetState?.copy(
233+
settingType = setting.settingType,
234+
settingKey = setting.key,
235+
value = setting.currentValue ?: "",
236+
)
237+
}
238+
}
239+
225240
fun onSettingValueChange(value: String) {
226241
modifySettingActionBottomSheetState =
227242
modifySettingActionBottomSheetState?.copy(value = value)

base/src/main/java/io/github/sds100/keymapper/base/actions/ModifySettingActionBottomSheet.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import androidx.compose.ui.Alignment
2525
import androidx.compose.ui.Modifier
2626
import androidx.compose.ui.platform.LocalDensity
2727
import androidx.compose.ui.res.stringResource
28+
import androidx.compose.ui.text.font.FontFamily
2829
import androidx.compose.ui.text.style.TextAlign
2930
import androidx.compose.ui.tooling.preview.Preview
3031
import androidx.compose.ui.unit.dp
@@ -56,6 +57,7 @@ fun ModifySettingActionBottomSheet(delegate: CreateActionDelegate) {
5657
onSelectSettingType = delegate::onSelectSettingType,
5758
onSettingKeyChange = delegate::onSettingKeyChange,
5859
onSettingValueChange = delegate::onSettingValueChange,
60+
onChooseExistingClick = delegate::onChooseExistingSettingClick,
5961
onDoneClick = {
6062
scope.launch {
6163
sheetState.hide()
@@ -75,6 +77,7 @@ private fun ModifySettingActionBottomSheet(
7577
onSelectSettingType: (SettingType) -> Unit = {},
7678
onSettingKeyChange: (String) -> Unit = {},
7779
onSettingValueChange: (String) -> Unit = {},
80+
onChooseExistingClick: () -> Unit = {},
7881
onDoneClick: () -> Unit = {},
7982
) {
8083
val scope = rememberCoroutineScope()
@@ -116,7 +119,7 @@ private fun ModifySettingActionBottomSheet(
116119
)
117120

118121
Button(
119-
onClick = { onSelectSettingType(state.settingType) },
122+
onClick = onChooseExistingClick,
120123
modifier = Modifier.fillMaxWidth(),
121124
) {
122125
Text(stringResource(R.string.choose_existing_setting))
@@ -128,6 +131,9 @@ private fun ModifySettingActionBottomSheet(
128131
label = { Text(stringResource(R.string.modify_setting_key_label)) },
129132
modifier = Modifier.fillMaxWidth(),
130133
singleLine = true,
134+
textStyle = MaterialTheme.typography.bodySmall.copy(
135+
fontFamily = FontFamily.Monospace,
136+
),
131137
)
132138

133139
OutlinedTextField(
@@ -136,21 +142,9 @@ private fun ModifySettingActionBottomSheet(
136142
label = { Text(stringResource(R.string.modify_setting_value_label)) },
137143
modifier = Modifier.fillMaxWidth(),
138144
singleLine = true,
139-
)
140-
141-
val exampleText = when (state.settingType) {
142-
SettingType.SYSTEM ->
143-
stringResource(R.string.modify_setting_example_system)
144-
SettingType.SECURE ->
145-
stringResource(R.string.modify_setting_example_secure)
146-
SettingType.GLOBAL ->
147-
stringResource(R.string.modify_setting_example_global)
148-
}
149-
150-
Text(
151-
text = exampleText,
152-
style = MaterialTheme.typography.bodySmall,
153-
color = MaterialTheme.colorScheme.onSurfaceVariant,
145+
textStyle = MaterialTheme.typography.bodySmall.copy(
146+
fontFamily = FontFamily.Monospace,
147+
),
154148
)
155149

156150
// TODO do not allow empty text fields

base/src/main/java/io/github/sds100/keymapper/base/utils/navigation/NavDestination.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ abstract class NavDestination<R>(val isCompose: Boolean = false) {
176176
}
177177

178178
@Serializable
179-
data class ChooseSetting(val currentSettingType: SettingType?) :
179+
data class ChooseSetting(val settingType: SettingType?) :
180180
NavDestination<ChooseSettingResult>(isCompose = true) {
181181
override val id: String = ID_CHOOSE_SETTING
182182
}

base/src/main/res/values/strings.xml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,11 +1181,7 @@
11811181
<string name="action_modify_setting">Modify setting</string>
11821182
<string name="modify_setting_key_label">Key</string>
11831183
<string name="modify_setting_value_label">Value</string>
1184-
<string name="modify_setting_type_label">Setting Type</string>
1185-
<string name="modify_setting_example_system">Example: screen_off_timeout = 2147483647 (System settings use WRITE_SETTINGS permission)</string>
1186-
<string name="modify_setting_example_secure">Example: accessibility_enabled = 1 (Secure settings require system bridge)</string>
1187-
<string name="modify_setting_example_global">Example: airplane_mode_on = 1 (Global settings require system bridge)</string>
1188-
<string name="choose_setting_title">Choose Setting</string>
1184+
<string name="choose_setting_title">Choose setting</string>
11891185
<string name="choose_setting_empty">No settings found</string>
11901186
<string name="choose_existing_setting">Choose existing setting</string>
11911187
<!-- action labels -->

0 commit comments

Comments
 (0)