Skip to content

Commit

Permalink
refactor: unify selected value logic in ListPreferenceDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 6, 2024
1 parent 8930f63 commit dfb9354
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ package com.bnyro.translate.obj
data class ListPreferenceOption(
val name: String,
val value: Int,
val isSelected: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ fun ThemeModeDialog(
) {
val activity = LocalContext.current as MainActivity
ListPreferenceDialog(
title = stringResource(R.string.select_theme),
preferenceKey = Preferences.themeModeKey,
onDismissRequest = {
onDismiss.invoke()
Expand All @@ -42,24 +41,22 @@ fun ThemeModeDialog(
ListPreferenceOption(
name = stringResource(R.string.theme_auto),
value = ThemeMode.AUTO.value,
isSelected = activity.themeMode == ThemeMode.AUTO
),
ListPreferenceOption(
name = stringResource(R.string.theme_light),
value = ThemeMode.LIGHT.value,
isSelected = activity.themeMode == ThemeMode.LIGHT
),
ListPreferenceOption(
name = stringResource(R.string.theme_dark),
value = ThemeMode.DARK.value,
isSelected = activity.themeMode == ThemeMode.DARK
),
ListPreferenceOption(
name = stringResource(R.string.theme_black),
value = ThemeMode.BLACK.value,
isSelected = activity.themeMode == ThemeMode.BLACK
)
),
title = stringResource(R.string.select_theme),
currentValue = activity.themeMode.value,
onOptionSelected = {
activity.themeMode = ThemeMode.values()[it.value]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package com.bnyro.translate.ui.components.prefs
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
Expand All @@ -42,7 +43,7 @@ fun ListPreference(
}

var selectedIndex by remember {
mutableStateOf(values.indexOf(Preferences.get(preferenceKey, defaultValue)))
mutableIntStateOf(values.indexOf(Preferences.get(preferenceKey, defaultValue)))
}

PreferenceItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fun ListPreferenceDialog(
LazyColumn {
items(options) {
SelectableItem(
text = if (it.value == currentValue) "${it.name}" else it.name,
text = it.name,
onClick = {
if (preferenceKey != null) {
Preferences.put(
Expand All @@ -59,7 +59,7 @@ fun ListPreferenceDialog(
onOptionSelected.invoke(it)
onDismissRequest.invoke()
},
isSelected = it.isSelected
isSelected = it.value == currentValue
)
}
}
Expand All @@ -70,11 +70,7 @@ fun ListPreferenceDialog(
onDismissRequest.invoke()
}
) {
Text(
stringResource(
R.string.cancel
)
)
Text(stringResource(R.string.cancel))
}
}
)
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/bnyro/translate/ui/views/EnginePref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ fun EnginePref() {
ListPreferenceOption(
it.replace("_", " ").capitalize(),
value = index,
isSelected = selectedAvailableEngine == it
)
}
},
currentValue = engine.supportedEngines.indexOf(selectedAvailableEngine)
.takeIf { it >= 0 }
) { engineOption ->
val selectedEngine = engine.supportedEngines[engineOption.value]
Preferences.put(engine.selEnginePrefKey, selectedEngine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ fun SimTranslationDialogComponent(
if (showSelectionDialog) {
ListPreferenceDialog(
preferenceKey = null,
title = stringResource(R.string.selected_engine),
onDismissRequest = { showSelectionDialog = false },
title = stringResource(R.string.selected_engine),
options = viewModel.enabledSimEngines.mapIndexed { index, engine ->
ListPreferenceOption(engine.name, index, selected == engine)
}
ListPreferenceOption(engine.name, index)
},
currentValue = viewModel.enabledSimEngines.indexOf(viewModel.engine).takeIf { it >= 0 }
) { engineOption ->
selected = viewModel.enabledSimEngines[engineOption.value]
viewModel.engine = selected
Expand Down

0 comments on commit dfb9354

Please sign in to comment.