HeliBoard/app/src/main/java/helium314/keyboard/settings/screens/ToolbarScreen.kt

109 lines
4.2 KiB
Kotlin
Raw Normal View History

package helium314.keyboard.settings.screens
import android.content.Context
2025-01-28 22:17:23 +01:00
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
2025-01-28 22:17:23 +01:00
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
2025-01-28 22:17:23 +01:00
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
2025-01-28 22:17:23 +01:00
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
2025-01-28 22:17:23 +01:00
import androidx.compose.ui.unit.dp
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings
2025-01-28 22:17:23 +01:00
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ReorderDialog
2025-01-28 22:17:23 +01:00
import helium314.keyboard.settings.prefs
import helium314.keyboard.settings.themeChanged
@Composable
fun ToolbarScreen(
onClickBack: () -> Unit,
) {
SearchPrefScreen(
onClickBack = onClickBack,
title = stringResource(R.string.settings_screen_toolbar),
) {
SettingsActivity2.allPrefs.map[Settings.PREF_PINNED_TOOLBAR_KEYS]!!.Preference()
}
}
fun createToolbarPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_PINNED_TOOLBAR_KEYS, R.string.pinned_toolbar_keys) { def ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
onClick = { showDialog = true },
)
if (showDialog) {
2025-01-28 22:17:23 +01:00
val ctx = LocalContext.current
val prefs = ctx.prefs()
val items = prefs.getString(def.key, defaultPinnedToolbarPref)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",")
KeyAndState(both.first(), both.last().toBoolean())
}
ReorderDialog(
2025-01-28 22:17:23 +01:00
onConfirmed = { reorderedItems ->
val value = reorderedItems.joinToString(";") { it.name + "," + it.state }
prefs.edit().putString(def.key, value).apply()
themeChanged = true
},
onDismissRequest = { showDialog = false },
2025-01-28 22:17:23 +01:00
items = items,
title = { Text(def.title)},
displayItem = { item ->
var checked by remember { mutableStateOf(item.state) }
Row(verticalAlignment = Alignment.CenterVertically) {
Box(modifier = Modifier.width(40.dp)) {
val iconId = KeyboardIconsSet.instance.iconIds[item.name.lowercase()]
if (iconId != null) // using ids makes having user-provided icons more difficult...
Icon(painterResource(iconId), null)
}
val text = item.name.lowercase().getStringResourceOrName("", context).toString()
Text(text, Modifier.weight(1f))
Switch(
checked = checked,
onCheckedChange = { item.state = it; checked = it }
)
}
},
getKey = { it.hashCode() }
)
}
},
)
2025-01-28 22:17:23 +01:00
private class KeyAndState(var name: String, var state: Boolean)
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
2025-01-28 22:17:23 +01:00
KeyboardIconsSet.instance.loadIcons(LocalContext.current)
Theme(true) {
Surface {
ToolbarScreen { }
}
}
}