add a first "real" reorder dialog

This commit is contained in:
Helium314 2025-01-28 22:17:23 +01:00
parent ce37888985
commit 724b292edb
4 changed files with 58 additions and 9 deletions

View file

@ -3,9 +3,11 @@ package helium314.keyboard.settings
import android.content.Context import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.content.SharedPreferences
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.settings.screens.createAboutPrefs import helium314.keyboard.settings.screens.createAboutPrefs
import helium314.keyboard.settings.screens.createCorrectionPrefs import helium314.keyboard.settings.screens.createCorrectionPrefs
import helium314.keyboard.settings.screens.createPreferencesPrefs import helium314.keyboard.settings.screens.createPreferencesPrefs
@ -64,6 +66,8 @@ fun Context.getActivity(): ComponentActivity? {
return componentActivity return componentActivity
} }
fun Context.prefs(): SharedPreferences = DeviceProtectedUtils.getSharedPreferences(this)
object NonSettingsPrefs { object NonSettingsPrefs {
const val EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary" const val EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"
const val APP = "app" const val APP = "app"

View file

@ -15,7 +15,9 @@ import kotlinx.coroutines.flow.MutableStateFlow
// todo // todo
// add reorder / enable dialog // add reorder / enable dialog
// reorder part is already done // maybe a more generic / reusable wrapper dialog
// need to adjust icons, because compose refuses to work with rotated drawables (arrows)
// drag handle should have "dimmed" color, iirc there are some theme defaults with the localCompositionProvider?
// more pref screens, and other super-custom things // more pref screens, and other super-custom things
// consider IME insets when searching // consider IME insets when searching
// improve performance when loading screens with many settings (lazyColumn?) // improve performance when loading screens with many settings (lazyColumn?)
@ -46,7 +48,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
// show as disabled -> users confused // show as disabled -> users confused
// show (but change will not do anything because another setting needs to be enabled first) // show (but change will not do anything because another setting needs to be enabled first)
// -> users confused, but probably better than the 2 above // -> users confused, but probably better than the 2 above
// adjust layout a little, there is too much empty space // adjust layout a little, there is too much empty space and titles are too large (dialogs!)
// maybe later // maybe later
// weird problem with app sometimes closing on back, but that's related to "old" settings (don't care if all are removed) // weird problem with app sometimes closing on back, but that's related to "old" settings (don't care if all are removed)

View file

@ -4,6 +4,7 @@ import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.lazy.rememberLazyListState
@ -19,6 +20,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape import androidx.compose.ui.graphics.Shape
@ -76,8 +78,8 @@ fun <T: Any> ReorderDialog(
) { dragging -> ) { dragging ->
val elevation by animateDpAsState(if (dragging) 4.dp else 0.dp) val elevation by animateDpAsState(if (dragging) 4.dp else 0.dp)
Surface(shadowElevation = elevation) { Surface(shadowElevation = elevation) {
Row(modifier = Modifier.longPressDraggableHandle()) { Row(modifier = Modifier.longPressDraggableHandle(), verticalAlignment = Alignment.CenterVertically) {
Icon(painterResource(R.drawable.ic_drag_indicator), "Reorder") Icon(painterResource(R.drawable.ic_drag_indicator), "Reorder", Modifier.padding(end = 8.dp))
displayItem(item) displayItem(item)
} }
} }

View file

@ -1,21 +1,31 @@
package helium314.keyboard.settings.screens package helium314.keyboard.settings.screens
import android.content.Context import android.content.Context
import androidx.compose.foundation.layout.fillMaxWidth 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 import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
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.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.Settings import helium314.keyboard.latin.settings.Settings
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.AllPrefs
import helium314.keyboard.settings.PrefDef import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Preference import helium314.keyboard.settings.Preference
@ -23,6 +33,8 @@ import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2 import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Theme import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ReorderDialog import helium314.keyboard.settings.dialogs.ReorderDialog
import helium314.keyboard.settings.prefs
import helium314.keyboard.settings.themeChanged
@Composable @Composable
fun ToolbarScreen( fun ToolbarScreen(
@ -44,21 +56,50 @@ fun createToolbarPrefs(context: Context) = listOf(
onClick = { showDialog = true }, onClick = { showDialog = true },
) )
if (showDialog) { if (showDialog) {
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( ReorderDialog(
onConfirmed = { }, onConfirmed = { reorderedItems ->
val value = reorderedItems.joinToString(";") { it.name + "," + it.state }
prefs.edit().putString(def.key, value).apply()
themeChanged = true
},
onDismissRequest = { showDialog = false }, onDismissRequest = { showDialog = false },
items = (1..40).toList(), items = items,
displayItem = { Text(it.toString(), Modifier.fillMaxWidth(), textAlign = TextAlign.Center) }, 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() } getKey = { it.hashCode() }
) )
} }
}, },
) )
private class KeyAndState(var name: String, var state: Boolean)
@Preview @Preview
@Composable @Composable
private fun Preview() { private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current) SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
KeyboardIconsSet.instance.loadIcons(LocalContext.current)
Theme(true) { Theme(true) {
Surface { Surface {
ToolbarScreen { } ToolbarScreen { }