mirror of
https://github.com/Helium314/HeliBoard.git
synced 2025-04-20 22:29:10 +00:00
fix broken list preferences
This commit is contained in:
parent
7696beeb29
commit
5d1545687f
4 changed files with 73 additions and 65 deletions
|
@ -0,0 +1,69 @@
|
||||||
|
package helium314.keyboard.settings.preferences
|
||||||
|
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import androidx.compose.material3.Text
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
|
import androidx.core.content.edit
|
||||||
|
import helium314.keyboard.latin.utils.prefs
|
||||||
|
import helium314.keyboard.settings.Setting
|
||||||
|
import helium314.keyboard.settings.dialogs.ListPickerDialog
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
/** [items] are displayString to value */
|
||||||
|
fun <T: Any> ListPreference(
|
||||||
|
setting: Setting,
|
||||||
|
items: List<Pair<String, T>>,
|
||||||
|
default: T,
|
||||||
|
onChanged: (T) -> Unit = { }
|
||||||
|
) {
|
||||||
|
var showDialog by rememberSaveable { mutableStateOf(false) }
|
||||||
|
val prefs = LocalContext.current.prefs()
|
||||||
|
val selected = items.firstOrNull { it.second == getPrefOfType(prefs, setting.key, default) }
|
||||||
|
Preference(
|
||||||
|
name = setting.title,
|
||||||
|
description = selected?.first,
|
||||||
|
onClick = { showDialog = true }
|
||||||
|
)
|
||||||
|
if (showDialog) {
|
||||||
|
ListPickerDialog(
|
||||||
|
onDismissRequest = { showDialog = false },
|
||||||
|
items = items,
|
||||||
|
onItemSelected = {
|
||||||
|
if (it == selected) return@ListPickerDialog
|
||||||
|
putPrefOfType(prefs, setting.key, it.second)
|
||||||
|
onChanged(it.second)
|
||||||
|
},
|
||||||
|
selectedItem = selected,
|
||||||
|
title = { Text(setting.title) },
|
||||||
|
getItemName = { it.first }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST")
|
||||||
|
fun <T: Any> getPrefOfType(prefs: SharedPreferences, key: String, default: T): T =
|
||||||
|
when (default) {
|
||||||
|
is String -> prefs.getString(key, default)
|
||||||
|
is Int -> prefs.getInt(key, default)
|
||||||
|
is Long -> prefs.getLong(key, default)
|
||||||
|
is Float -> prefs.getFloat(key, default)
|
||||||
|
is Boolean -> prefs.getBoolean(key, default)
|
||||||
|
else -> throw IllegalArgumentException("unknown type ${default.javaClass}")
|
||||||
|
} as T
|
||||||
|
|
||||||
|
private fun <T: Any> putPrefOfType(prefs: SharedPreferences, key: String, value: T) =
|
||||||
|
prefs.edit {
|
||||||
|
when (value) {
|
||||||
|
is String -> putString(key, value)
|
||||||
|
is Int -> putInt(key, value)
|
||||||
|
is Long -> putLong(key, value)
|
||||||
|
is Float -> putFloat(key, value)
|
||||||
|
is Boolean -> putBoolean(key, value)
|
||||||
|
else -> throw IllegalArgumentException("unknown type ${value.javaClass}")
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,24 +1,18 @@
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
package helium314.keyboard.settings.preferences
|
package helium314.keyboard.settings.preferences
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
|
||||||
import androidx.compose.material3.Text
|
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.saveable.rememberSaveable
|
import androidx.compose.runtime.saveable.rememberSaveable
|
||||||
import androidx.compose.runtime.setValue
|
import androidx.compose.runtime.setValue
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.core.content.edit
|
|
||||||
import helium314.keyboard.latin.utils.Log
|
import helium314.keyboard.latin.utils.Log
|
||||||
import helium314.keyboard.latin.utils.getActivity
|
import helium314.keyboard.latin.utils.getActivity
|
||||||
import helium314.keyboard.latin.utils.prefs
|
import helium314.keyboard.latin.utils.prefs
|
||||||
import helium314.keyboard.settings.Setting
|
|
||||||
import helium314.keyboard.settings.SettingsActivity
|
import helium314.keyboard.settings.SettingsActivity
|
||||||
import helium314.keyboard.settings.dialogs.ListPickerDialog
|
|
||||||
import helium314.keyboard.settings.dialogs.SliderDialog
|
import helium314.keyboard.settings.dialogs.SliderDialog
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
|
@ -72,58 +66,3 @@ fun <T: Number> SliderPreference(
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
|
||||||
// just in here so we can keep getPrefOfType private... rename file?
|
|
||||||
fun <T: Any> ListPreference(
|
|
||||||
setting: Setting,
|
|
||||||
items: List<Pair<String, T>>,
|
|
||||||
default: T,
|
|
||||||
onChanged: (T) -> Unit = { }
|
|
||||||
) {
|
|
||||||
var showDialog by rememberSaveable { mutableStateOf(false) }
|
|
||||||
val prefs = LocalContext.current.prefs()
|
|
||||||
val selected = items.firstOrNull { it.second == getPrefOfType(prefs, setting.key, default) }
|
|
||||||
Preference(
|
|
||||||
name = setting.title,
|
|
||||||
description = selected?.first,
|
|
||||||
onClick = { showDialog = true }
|
|
||||||
)
|
|
||||||
if (showDialog) {
|
|
||||||
ListPickerDialog(
|
|
||||||
onDismissRequest = { showDialog = false },
|
|
||||||
items = items,
|
|
||||||
onItemSelected = {
|
|
||||||
if (it == selected) return@ListPickerDialog
|
|
||||||
putPrefOfType(prefs, setting.key, it.second)
|
|
||||||
onChanged(it.second)
|
|
||||||
},
|
|
||||||
selectedItem = selected,
|
|
||||||
title = { Text(setting.title) },
|
|
||||||
getItemName = { it.first }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
private fun <T: Any> getPrefOfType(prefs: SharedPreferences, key: String, default: T): T =
|
|
||||||
when (default) {
|
|
||||||
is String -> prefs.getString(key, default)
|
|
||||||
is Int -> prefs.getInt(key, default)
|
|
||||||
is Long -> prefs.getLong(key, default)
|
|
||||||
is Float -> prefs.getFloat(key, default)
|
|
||||||
is Boolean -> prefs.getBoolean(key, default)
|
|
||||||
else -> throw IllegalArgumentException("unknown type ${default.javaClass}")
|
|
||||||
} as T
|
|
||||||
|
|
||||||
private fun <T: Any> putPrefOfType(prefs: SharedPreferences, key: String, value: T) =
|
|
||||||
prefs.edit {
|
|
||||||
when (value) {
|
|
||||||
is String -> putString(key, value)
|
|
||||||
is Int -> putInt(key, value)
|
|
||||||
is Long -> putLong(key, value)
|
|
||||||
is Float -> putFloat(key, value)
|
|
||||||
is Boolean -> putBoolean(key, value)
|
|
||||||
else -> throw IllegalArgumentException("unknown type ${value.javaClass}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -182,7 +182,7 @@ fun createAdvancedSettings(context: Context) = listOf(
|
||||||
},
|
},
|
||||||
Setting(context, Settings.PREF_MORE_POPUP_KEYS, R.string.show_popup_keys_title) {
|
Setting(context, Settings.PREF_MORE_POPUP_KEYS, R.string.show_popup_keys_title) {
|
||||||
val items = listOf(POPUP_KEYS_NORMAL, POPUP_KEYS_MAIN, POPUP_KEYS_MORE, POPUP_KEYS_ALL).map { setting ->
|
val items = listOf(POPUP_KEYS_NORMAL, POPUP_KEYS_MAIN, POPUP_KEYS_MORE, POPUP_KEYS_ALL).map { setting ->
|
||||||
setting to stringResource(morePopupKeysResId(setting))
|
stringResource(morePopupKeysResId(setting)) to setting
|
||||||
}
|
}
|
||||||
ListPreference(it, items, Defaults.PREF_MORE_POPUP_KEYS) { KeyboardLayoutSet.onSystemLocaleChanged() }
|
ListPreference(it, items, Defaults.PREF_MORE_POPUP_KEYS) { KeyboardLayoutSet.onSystemLocaleChanged() }
|
||||||
},
|
},
|
||||||
|
|
|
@ -125,9 +125,9 @@ fun createPreferencesSettings(context: Context) = listOf(
|
||||||
ListPreference(
|
ListPreference(
|
||||||
it,
|
it,
|
||||||
listOf(
|
listOf(
|
||||||
"internal" to stringResource(R.string.switch_language),
|
stringResource(R.string.switch_language) to "internal",
|
||||||
"input_method" to stringResource(R.string.language_switch_key_switch_input_method),
|
stringResource(R.string.language_switch_key_switch_input_method) to "input_method",
|
||||||
"both" to stringResource(R.string.language_switch_key_switch_both)
|
stringResource(R.string.language_switch_key_switch_both) to "both"
|
||||||
),
|
),
|
||||||
Defaults.PREF_LANGUAGE_SWITCH_KEY
|
Defaults.PREF_LANGUAGE_SWITCH_KEY
|
||||||
) { keyboardNeedsReload = true }
|
) { keyboardNeedsReload = true }
|
||||||
|
|
Loading…
Add table
Reference in a new issue