rename some stuff

This commit is contained in:
Helium314 2025-02-07 16:12:43 +01:00
parent b62eaba3e4
commit efc7f1842b
26 changed files with 415 additions and 429 deletions

View file

@ -74,7 +74,7 @@ SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only
</intent-filter>
</activity>
<activity android:name="helium314.keyboard.settings.SettingsActivity2"
<activity android:name="helium314.keyboard.settings.SettingsActivity"
android:theme="@style/platformActivityTheme"
android:windowSoftInputMode="adjustResize"
android:label="@string/ime_settings"

View file

@ -88,7 +88,7 @@ import helium314.keyboard.latin.utils.StatsUtilsManager;
import helium314.keyboard.latin.utils.SubtypeLocaleUtils;
import helium314.keyboard.latin.utils.SubtypeSettingsKt;
import helium314.keyboard.latin.utils.ViewLayoutUtils;
import helium314.keyboard.settings.SettingsActivity2;
import helium314.keyboard.settings.SettingsActivity;
import helium314.keyboard.settings.SettingsActivityKt;
import kotlin.collections.CollectionsKt;
@ -1879,7 +1879,7 @@ public class LatinIME extends InputMethodService implements
mainKeyboardView.closing();
}
final Intent intent = new Intent();
intent.setClass(LatinIME.this, SettingsActivity2.class);
intent.setClass(LatinIME.this, SettingsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_CLEAR_TOP);

View file

@ -21,7 +21,7 @@ import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public final class SettingsActivity extends AppCompatActivity
public final class OldSettingsActivity extends AppCompatActivity
implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final String DEFAULT_FRAGMENT = SettingsFragment.class.getName();

View file

@ -26,13 +26,12 @@ import androidx.core.content.res.ResourcesCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import helium314.keyboard.latin.R;
import helium314.keyboard.latin.settings.SettingsActivity;
import helium314.keyboard.latin.utils.ActivityThemeUtils;
import helium314.keyboard.latin.utils.JniUtils;
import helium314.keyboard.latin.utils.LeakGuardHandlerWrapper;
import helium314.keyboard.latin.utils.ResourceUtils;
import helium314.keyboard.latin.utils.UncachedInputMethodManagerUtils;
import helium314.keyboard.settings.SettingsActivity2;
import helium314.keyboard.settings.SettingsActivity;
import java.util.ArrayList;
@ -166,7 +165,7 @@ public final class SetupWizardActivity extends AppCompatActivity implements View
0 /* finishedInstruction */, R.drawable.sym_keyboard_language_switch,
R.string.setup_step3_action);
step3.setAction(() -> {
final Intent intent = new Intent(getApplicationContext(), SettingsActivity2.class);
final Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
finish();
@ -224,11 +223,11 @@ public final class SetupWizardActivity extends AppCompatActivity implements View
private void invokeSettingsOfThisIme() {
final Intent intent = new Intent();
intent.setClass(this, SettingsActivity2.class);
intent.setClass(this, SettingsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(SettingsActivity.EXTRA_ENTRY_KEY,
SettingsActivity.EXTRA_ENTRY_VALUE_APP_ICON);
// intent.putExtra(OldSettingsActivity.EXTRA_ENTRY_KEY,
// OldSettingsActivity.EXTRA_ENTRY_VALUE_APP_ICON);
startActivity(intent);
}

View file

@ -1,80 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.settings
import android.content.Context
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import helium314.keyboard.settings.screens.createAboutPrefs
import helium314.keyboard.settings.screens.createAdvancedPrefs
import helium314.keyboard.settings.screens.createAppearancePrefs
import helium314.keyboard.settings.screens.createCorrectionPrefs
import helium314.keyboard.settings.screens.createDebugPrefs
import helium314.keyboard.settings.screens.createGestureTypingPrefs
import helium314.keyboard.settings.screens.createPreferencesPrefs
import helium314.keyboard.settings.screens.createToolbarPrefs
class AllPrefs(context: Context) {
private val list = createPrefDefs(context)
val map: Map<String, PrefDef> = HashMap<String, PrefDef>(list.size).apply {
list.forEach {
if (put(it.key, it) != null)
throw IllegalArgumentException("key $it added twice")
}
}
// could be more elaborate, but should be good enough for a start
fun filter(searchTerm: String): List<PrefDef> {
val term = searchTerm.lowercase()
val results = mutableSetOf<PrefDef>()
list.forEach { def -> if (def.title.lowercase().startsWith(term)) results.add(def) }
list.forEach { def -> if (def.title.lowercase().split(' ').any { it.startsWith(term) }) results.add(def) }
list.forEach { def ->
if (def.description?.lowercase()?.split(' ')?.any { it.startsWith(term) } == true)
results.add(def)
}
return results.toList()
}
}
class PrefDef(
context: Context,
val key: String,
@StringRes titleId: Int,
@StringRes descriptionId: Int? = null,
private val compose: @Composable (PrefDef) -> Unit
) {
val title = context.getString(titleId)
val description = descriptionId?.let { context.getString(it) }
@Composable
fun Preference() {
compose(this)
}
}
private fun createPrefDefs(context: Context) = createAboutPrefs(context) +
createCorrectionPrefs(context) + createPreferencesPrefs(context) + createToolbarPrefs(context) +
createGestureTypingPrefs(context) + createAdvancedPrefs(context) + createDebugPrefs(context) +
createAppearancePrefs(context)
object NonSettingsPrefs {
const val EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"
const val APP = "app"
const val VERSION = "version"
const val LICENSE = "license"
const val HIDDEN_FEATURES = "hidden_features"
const val GITHUB = "github"
const val SAVE_LOG = "save_log"
const val CUSTOM_KEY_CODES = "customize_key_codes"
const val CUSTOM_SYMBOLS_NUMBER_LAYOUTS = "custom_symbols_number_layouts"
const val CUSTOM_FUNCTIONAL_LAYOUTS = "custom_functional_key_layouts"
const val BACKUP_RESTORE = "backup_restore"
const val DEBUG_SETTINGS = "screen_debug"
const val LOAD_GESTURE_LIB = "load_gesture_library"
const val ADJUST_COLORS = "adjust_colors"
const val ADJUST_COLORS_NIGHT = "adjust_colors_night"
const val BACKGROUND_IMAGE = "background_image"
const val BACKGROUND_IMAGE_LANDSCAPE = "background_image_landscape"
const val CUSTOM_FONT = "custom_font"
}

View file

@ -63,13 +63,13 @@ fun SearchPrefScreen(
if (it is Int)
PreferenceCategory(stringResource(it))
else
SettingsActivity2.allPrefs.map[it]!!.Preference()
SettingsActivity.settingsContainer[it]!!.Preference()
}
}
}
},
filteredItems = { SettingsActivity2.allPrefs.filter(it) },
filteredItems = { SettingsActivity.settingsContainer.filter(it) },
itemContent = { it.Preference() }
)
}

View file

@ -14,13 +14,10 @@ import helium314.keyboard.latin.utils.prefs
import kotlinx.coroutines.flow.MutableStateFlow
// todo (roughly in order)
// use better / more structured and clear names and arrangement of files
// the prefDef and AllPrefs, also be clear about pref <-> key <-> prefKey (all used, probably should be latter)
// there is a lot more ambiguous naming...
// dialogs should be rememberSaveable to survive display orientation change and stuff?
// check dark and light theme (don't have dynamic)
// any way to get rid of the "old" background on starting settings? probably comes from app theme, can we avoid it?
// rename both settingsActivities
// try making old fragment back stuff work better, and try the different themes (with and without top bar, it should only appear for old fragments)
// calling KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(requireContext()) while keyboard is showing shows just full screen background
// but reload while keyboard is showing would be great (isn't it at least semi-done when changing one-handed mode?)
// bg image inconsistent about being on toolbar or not (is this new?)
@ -28,7 +25,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
// find a nice way of testing (probably add logs for measuring time and recompositions)
// consider that stuff in composables can get called quite often on any changes
// -> use remember for things that are slow, but be careful they don't change from outside the composable
// try making old fragment back stuff work better, and try the different themes (with and without top bar, it should only appear for old fragments)
// PRs adding prefs -> need to finish and merge main before finishing this PR
// 1263 (no response for several weeks now...)
// really use the restart dialog for debug settings stuff?
@ -65,6 +61,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
// actually lenient json parsing is not good in a certain way: we should show an error if a json property is unknown
// syntax highlighting for json? should show basic json errors
// does restore prefs not delete dictionaries?
// don't require to switch keyboard when entering settings
// preliminary results:
// looks ok (ugly M3 switches)
@ -83,7 +80,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
// -> too much, but still ok if we can get nicer preference stuff
// meh, and using a TextField adds another 300 kb... huge chunks for sth that seems so small
class SettingsActivity2 : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
class SettingsActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener {
private val prefs by lazy { this.prefs() }
val prefChanged = MutableStateFlow(0) // simple counter, as the only relevant information is that something changed
@ -92,7 +89,7 @@ class SettingsActivity2 : AppCompatActivity(), SharedPreferences.OnSharedPrefere
if (Settings.getInstance().current == null)
Settings.init(this)
allPrefs = AllPrefs(this)
settingsContainer = SettingsContainer(this)
// todo: when removing old settings completely, remove settings_activity.xml and supportFragmentManager stuff
// val cv = ComposeView(context = this)
@ -133,8 +130,8 @@ class SettingsActivity2 : AppCompatActivity(), SharedPreferences.OnSharedPrefere
}
companion object {
// public write so compose previews can show the pref screens
lateinit var allPrefs: AllPrefs
// public write so compose previews can show the screens
lateinit var settingsContainer: SettingsContainer
}
override fun onSharedPreferenceChanged(prefereces: SharedPreferences?, key: String?) {

View file

@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.settings
import android.content.Context
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import helium314.keyboard.settings.screens.createAboutSettings
import helium314.keyboard.settings.screens.createAdvancedSettings
import helium314.keyboard.settings.screens.createAppearanceSettings
import helium314.keyboard.settings.screens.createCorrectionSettings
import helium314.keyboard.settings.screens.createDebugSettings
import helium314.keyboard.settings.screens.createGestureTypingSettings
import helium314.keyboard.settings.screens.createPreferencesSettings
import helium314.keyboard.settings.screens.createToolbarSettingss
class SettingsContainer(context: Context) {
private val list = createSettings(context)
private val map: Map<String, Setting> = HashMap<String, Setting>(list.size).apply {
list.forEach {
if (put(it.key, it) != null)
throw IllegalArgumentException("key $it added twice")
}
}
operator fun get(key: Any): Setting? = map[key]
// could be more elaborate, but should be good enough for a start
fun filter(searchTerm: String): List<Setting> {
val term = searchTerm.lowercase()
val results = mutableSetOf<Setting>()
list.forEach { setting -> if (setting.title.lowercase().startsWith(term)) results.add(setting) }
list.forEach { setting -> if (setting.title.lowercase().split(' ').any { it.startsWith(term) }) results.add(setting) }
list.forEach { setting ->
if (setting.description?.lowercase()?.split(' ')?.any { it.startsWith(term) } == true)
results.add(setting)
}
return results.toList()
}
}
class Setting(
context: Context,
val key: String,
@StringRes titleId: Int,
@StringRes descriptionId: Int? = null,
private val content: @Composable (Setting) -> Unit
) {
val title = context.getString(titleId)
val description = descriptionId?.let { context.getString(it) }
@Composable
fun Preference() {
content(this)
}
}
private fun createSettings(context: Context) = createAboutSettings(context) +
createCorrectionSettings(context) + createPreferencesSettings(context) + createToolbarSettingss(context) +
createGestureTypingSettings(context) + createAdvancedSettings(context) + createDebugSettings(context) +
createAppearanceSettings(context)
object SettingsWithoutKey {
const val EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"
const val APP = "app"
const val VERSION = "version"
const val LICENSE = "license"
const val HIDDEN_FEATURES = "hidden_features"
const val GITHUB = "github"
const val SAVE_LOG = "save_log"
const val CUSTOM_KEY_CODES = "customize_key_codes"
const val CUSTOM_SYMBOLS_NUMBER_LAYOUTS = "custom_symbols_number_layouts"
const val CUSTOM_FUNCTIONAL_LAYOUTS = "custom_functional_key_layouts"
const val BACKUP_RESTORE = "backup_restore"
const val DEBUG_SETTINGS = "screen_debug"
const val LOAD_GESTURE_LIB = "load_gesture_library"
const val ADJUST_COLORS = "adjust_colors"
const val ADJUST_COLORS_NIGHT = "adjust_colors_night"
const val BACKGROUND_IMAGE = "background_image"
const val BACKGROUND_IMAGE_LANDSCAPE = "background_image_landscape"
const val CUSTOM_FONT = "custom_font"
}

View file

@ -23,8 +23,8 @@ import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import helium314.keyboard.settings.dialogs.InfoDialog
import helium314.keyboard.settings.keyboardNeedsReload
@ -32,14 +32,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@Composable
fun BackgroundImagePref(def: PrefDef, isLandscape: Boolean) {
fun BackgroundImagePref(setting: Setting, isLandscape: Boolean) {
var showDayNightDialog by remember { mutableStateOf(false) }
var showSelectionDialog by remember { mutableStateOf(false) }
var showErrorDialog by remember { mutableStateOf(false) }
var isNight by remember { mutableStateOf(false) }
val ctx = LocalContext.current
fun getFile() = Settings.getCustomBackgroundFile(ctx, isNight, isLandscape)
val b = (ctx.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0) // necessary to reload dayNightPref
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val dayNightPref = Settings.readDayNightPref(ctx.prefs(), ctx.resources)
@ -60,7 +60,7 @@ fun BackgroundImagePref(def: PrefDef, isLandscape: Boolean) {
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("image/*")
Preference(
name = def.title,
name = setting.title,
onClick = {
if (dayNightPref) {
showDayNightDialog = true

View file

@ -39,8 +39,8 @@ import helium314.keyboard.latin.utils.onCustomLayoutFileListChanged
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.latin.utils.reloadEnabledSubtypes
import helium314.keyboard.latin.utils.updateAdditionalSubtypes
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import helium314.keyboard.settings.dialogs.InfoDialog
import helium314.keyboard.settings.keyboardNeedsReload
@ -54,7 +54,7 @@ import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
@Composable
fun BackupRestorePreference(def: PrefDef) {
fun BackupRestorePreference(setting: Setting) {
var showDialog by remember { mutableStateOf(false) }
val ctx = LocalContext.current
val prefs = ctx.prefs()
@ -180,13 +180,10 @@ fun BackupRestorePreference(def: PrefDef) {
val newDictBroadcast = Intent(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION)
ctx.getActivity()?.sendBroadcast(newDictBroadcast)
onCustomLayoutFileListChanged()
(ctx.getActivity() as? SettingsActivity2)?.prefChanged?.value = 210 // for settings reload
(ctx.getActivity() as? SettingsActivity)?.prefChanged?.value = 210 // for settings reload
keyboardNeedsReload = true
}
Preference(
name = def.title,
onClick = { showDialog = true }
)
Preference(name = setting.title, onClick = { showDialog = true })
if (showDialog) {
ConfirmationDialog(
onDismissRequest = { showDialog = false },

View file

@ -17,14 +17,14 @@ import helium314.keyboard.latin.R
import helium314.keyboard.latin.common.FileUtils
import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.DeviceProtectedUtils
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import helium314.keyboard.settings.dialogs.InfoDialog
import helium314.keyboard.settings.keyboardNeedsReload
import java.io.File
@Composable
fun CustomFontPreference(def: PrefDef) {
fun CustomFontPreference(setting: Setting) {
val ctx = LocalContext.current
var showDialog by remember { mutableStateOf(false) }
var showErrorDialog by remember { mutableStateOf(false) }
@ -49,7 +49,7 @@ fun CustomFontPreference(def: PrefDef) {
.addCategory(Intent.CATEGORY_OPENABLE)
.setType("*/*")
Preference(
name = def.title,
name = setting.title,
onClick = {
if (fontFile.exists())
showDialog = true

View file

@ -9,14 +9,14 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import helium314.keyboard.latin.utils.CUSTOM_LAYOUT_PREFIX
import helium314.keyboard.latin.utils.getCustomLayoutFiles
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.dialogs.LayoutEditDialog
import helium314.keyboard.settings.dialogs.ListPickerDialog
import java.io.File
@Composable
fun LayoutEditPreference(
def: PrefDef,
setting: Setting,
items: List<String>,
getItemName: @Composable (String) -> String,
getDefaultLayout: @Composable (String?) -> String?,
@ -25,7 +25,7 @@ fun LayoutEditPreference(
val ctx = LocalContext.current
var layout: String? by remember { mutableStateOf(null) }
Preference(
name = def.title,
name = setting.title,
onClick = { showDialog = true }
)
if (showDialog) {
@ -36,7 +36,7 @@ fun LayoutEditPreference(
items = items,
getItemName = getItemName,
onItemSelected = { layout = it },
title = { Text(def.title) }
title = { Text(setting.title) }
)
}
if (layout != null) {

View file

@ -20,7 +20,7 @@ import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.ChecksumCalculator
import helium314.keyboard.latin.utils.JniUtils
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.dialogs.ConfirmationDialog
import java.io.File
import java.io.FileInputStream
@ -28,7 +28,7 @@ import java.io.FileOutputStream
import java.io.IOException
@Composable
fun LoadGestureLibPreference(def: PrefDef) {
fun LoadGestureLibPreference(setting: Setting) {
var showDialog by remember { mutableStateOf(false) }
val ctx = LocalContext.current
val prefs = ctx.prefs()
@ -74,7 +74,7 @@ fun LoadGestureLibPreference(def: PrefDef) {
}
}
Preference(
name = def.title,
name = setting.title,
onClick = { showDialog = true }
)
if (showDialog) {

View file

@ -119,7 +119,7 @@ private fun PreferencePreview() {
)
SliderPreference(
name = "SliderPreference",
pref = "",
key = "",
default = 1,
description = { it.toString() },
range = -5f..5f
@ -138,7 +138,7 @@ private fun PreferencePreview() {
}
SwitchPreference(
name = "SwitchPreference",
pref = "none",
key = "none",
default = true
)
Preference(

View file

@ -16,37 +16,37 @@ import helium314.keyboard.keyboard.internal.KeyboardIconsSet
import helium314.keyboard.latin.R
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.dialogs.ReorderDialog
import helium314.keyboard.settings.keyboardNeedsReload
import helium314.keyboard.settings.screens.GetIcon
@Composable
fun ReorderSwitchPreference(def: PrefDef, default: String) {
fun ReorderSwitchPreference(setting: Setting, default: String) {
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
description = def.description,
name = setting.title,
description = setting.description,
onClick = { showDialog = true },
)
if (showDialog) {
val ctx = LocalContext.current
val prefs = ctx.prefs()
val items = prefs.getString(def.key, default)!!.split(";").mapTo(ArrayList()) {
val items = prefs.getString(setting.key, default)!!.split(";").mapTo(ArrayList()) {
val both = it.split(",")
KeyAndState(both.first(), both.last().toBoolean())
}
ReorderDialog(
onConfirmed = { reorderedItems ->
val value = reorderedItems.joinToString(";") { it.name + "," + it.state }
prefs.edit().putString(def.key, value).apply()
prefs.edit().putString(setting.key, value).apply()
keyboardNeedsReload = true
},
onDismissRequest = { showDialog = false },
onNeutral = { prefs.edit().remove(def.key).apply() },
neutralButtonText = if (prefs.contains(def.key)) stringResource(R.string.button_default) else null,
onNeutral = { prefs.edit().remove(setting.key).apply() },
neutralButtonText = if (prefs.contains(setting.key)) stringResource(R.string.button_default) else null,
items = items,
title = { Text(def.title) },
title = { Text(setting.title) },
displayItem = { item ->
var checked by remember { mutableStateOf(item.state) }
Row(verticalAlignment = Alignment.CenterVertically) {

View file

@ -14,8 +14,8 @@ import androidx.core.content.edit
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.dialogs.ListPickerDialog
import helium314.keyboard.settings.dialogs.SliderDialog
import kotlin.math.roundToInt
@ -25,7 +25,7 @@ import kotlin.math.roundToInt
fun <T: Number> SliderPreference(
name: String,
modifier: Modifier = Modifier,
pref: String,
key: String,
description: @Composable (T) -> String,
default: T,
range: ClosedFloatingPointRange<Float>,
@ -34,11 +34,11 @@ fun <T: Number> SliderPreference(
) {
val ctx = LocalContext.current
val prefs = ctx.prefs()
val b = (ctx.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val initialValue = if (default is Int || default is Float)
getPrefOfType(prefs, pref, default)
getPrefOfType(prefs, key, default)
else throw IllegalArgumentException("only float and int are supported")
var showDialog by remember { mutableStateOf(false) }
@ -52,8 +52,8 @@ fun <T: Number> SliderPreference(
SliderDialog(
onDismissRequest = { showDialog = false },
onDone = {
if (default is Int) prefs.edit().putInt(pref, it.toInt()).apply()
else prefs.edit().putFloat(pref, it).apply()
if (default is Int) prefs.edit().putInt(key, it.toInt()).apply()
else prefs.edit().putFloat(key, it).apply()
},
initialValue = initialValue.toFloat(),
range = range,
@ -63,7 +63,7 @@ fun <T: Number> SliderPreference(
},
onValueChanged = onValueChanged,
showDefault = true,
onDefault = { prefs.edit().remove(pref).apply() },
onDefault = { prefs.edit().remove(key).apply() },
intermediateSteps = stepSize?.let {
// this is not nice, but slider wants it like this...
((range.endInclusive - range.start) / it - 1).toInt()
@ -74,16 +74,16 @@ fun <T: Number> SliderPreference(
@Composable
// just in here so we can keep getPrefOfType private... rename file?
fun <T: Any> ListPreference(
def: PrefDef,
setting: Setting,
items: List<Pair<String, T>>,
default: T,
onChanged: (T) -> Unit = { }
) {
var showDialog by remember { mutableStateOf(false) }
val prefs = LocalContext.current.prefs()
val selected = items.firstOrNull { it.second == getPrefOfType(prefs, def.key, default) }
val selected = items.firstOrNull { it.second == getPrefOfType(prefs, setting.key, default) }
Preference(
name = def.title,
name = setting.title,
description = selected?.first,
onClick = { showDialog = true }
)
@ -93,11 +93,11 @@ fun <T: Any> ListPreference(
items = items,
onItemSelected = {
if (it == selected) return@ListPickerDialog
putPrefOfType(prefs, def.key, it.second)
putPrefOfType(prefs, setting.key, it.second)
onChanged(it.second)
},
selectedItem = selected,
title = { Text(def.title) },
title = { Text(setting.title) },
getItemName = { it.first }
)
}

View file

@ -8,15 +8,31 @@ import androidx.compose.ui.platform.LocalContext
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SettingsActivity
@Composable
fun SwitchPreference(
setting: Setting,
default: Boolean,
allowCheckedChange: (Boolean) -> Boolean = { true },
onCheckedChange: (Boolean) -> Unit = { }
) {
SwitchPreference(
name = setting.title,
description = setting.description,
key = setting.key,
default = default,
allowCheckedChange = allowCheckedChange,
onCheckedChange = onCheckedChange
)
}
@Composable
fun SwitchPreference(
name: String,
modifier: Modifier = Modifier,
pref: String,
key: String,
default: Boolean,
description: String? = null,
allowCheckedChange: (Boolean) -> Boolean = { true }, // true means ok, usually for showing some dialog
@ -24,17 +40,17 @@ fun SwitchPreference(
) {
val ctx = LocalContext.current
val prefs = ctx.prefs()
val b = (ctx.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
var value = prefs.getBoolean(pref, default)
var value = prefs.getBoolean(key, default)
fun switched(newValue: Boolean) {
if (!allowCheckedChange(newValue)) {
value = !newValue
return
}
value = newValue
prefs.edit().putBoolean(pref, newValue).apply()
prefs.edit().putBoolean(key, newValue).apply()
onCheckedChange(newValue)
}
Preference(
@ -49,20 +65,3 @@ fun SwitchPreference(
)
}
}
@Composable
fun SwitchPreference(
def: PrefDef,
default: Boolean,
allowCheckedChange: (Boolean) -> Boolean = { true },
onCheckedChange: (Boolean) -> Unit = { }
) {
SwitchPreference(
name = def.title,
description = def.description,
pref = def.key,
default = default,
allowCheckedChange = allowCheckedChange,
onCheckedChange = onCheckedChange
)
}

View file

@ -29,12 +29,12 @@ import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.SpannableStringUtils
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.Theme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -44,12 +44,12 @@ fun AboutScreen(
onClickBack: () -> Unit,
) {
val items = listOf(
NonSettingsPrefs.APP,
NonSettingsPrefs.VERSION,
NonSettingsPrefs.LICENSE,
NonSettingsPrefs.HIDDEN_FEATURES,
NonSettingsPrefs.GITHUB,
NonSettingsPrefs.SAVE_LOG
SettingsWithoutKey.APP,
SettingsWithoutKey.VERSION,
SettingsWithoutKey.LICENSE,
SettingsWithoutKey.HIDDEN_FEATURES,
SettingsWithoutKey.GITHUB,
SettingsWithoutKey.SAVE_LOG
)
SearchPrefScreen(
onClickBack = onClickBack,
@ -58,8 +58,8 @@ fun AboutScreen(
)
}
fun createAboutPrefs(context: Context) = listOf(
PrefDef(context, NonSettingsPrefs.APP, R.string.english_ime_name, R.string.app_slogan) {
fun createAboutSettings(context: Context) = listOf(
Setting(context, SettingsWithoutKey.APP, R.string.english_ime_name, R.string.app_slogan) {
Preference(
name = it.title,
description = it.description,
@ -67,7 +67,7 @@ fun createAboutPrefs(context: Context) = listOf(
icon = R.drawable.ic_launcher_foreground // use the bitmap trick here if we really want the colored icon
)
},
PrefDef(context, NonSettingsPrefs.VERSION, R.string.version) {
Setting(context, SettingsWithoutKey.VERSION, R.string.version) {
var count by rememberSaveable { mutableIntStateOf(0) }
val ctx = LocalContext.current
val prefs = ctx.prefs()
@ -85,7 +85,7 @@ fun createAboutPrefs(context: Context) = listOf(
icon = R.drawable.ic_settings_about_foreground
)
},
PrefDef(context, NonSettingsPrefs.LICENSE, R.string.license, R.string.gnu_gpl) {
Setting(context, SettingsWithoutKey.LICENSE, R.string.license, R.string.gnu_gpl) {
val ctx = LocalContext.current
Preference(
name = it.title,
@ -99,7 +99,7 @@ fun createAboutPrefs(context: Context) = listOf(
icon = R.drawable.ic_settings_about_license_foreground
)
},
PrefDef(context, NonSettingsPrefs.HIDDEN_FEATURES, R.string.hidden_features_title, R.string.hidden_features_summary) {
Setting(context, SettingsWithoutKey.HIDDEN_FEATURES, R.string.hidden_features_title, R.string.hidden_features_summary) {
val ctx = LocalContext.current
Preference(
name = it.title,
@ -123,7 +123,7 @@ fun createAboutPrefs(context: Context) = listOf(
icon = R.drawable.ic_settings_about_hidden_features_foreground
)
},
PrefDef(context, NonSettingsPrefs.GITHUB, R.string.about_github_link) {
Setting(context, SettingsWithoutKey.GITHUB, R.string.about_github_link) {
val ctx = LocalContext.current
Preference(
name = it.title,
@ -137,7 +137,7 @@ fun createAboutPrefs(context: Context) = listOf(
icon = R.drawable.ic_settings_about_github_foreground
)
},
PrefDef(context, NonSettingsPrefs.SAVE_LOG, R.string.save_log) { def ->
Setting(context, SettingsWithoutKey.SAVE_LOG, R.string.save_log) { setting ->
val ctx = LocalContext.current
val scope = rememberCoroutineScope()
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
@ -150,8 +150,8 @@ fun createAboutPrefs(context: Context) = listOf(
}
}
Preference(
name = def.title,
description = def.description,
name = setting.title,
description = setting.description,
onClick = {
val intent = Intent(Intent.ACTION_CREATE_DOCUMENT)
.addCategory(Intent.CATEGORY_OPENABLE)
@ -171,7 +171,7 @@ fun createAboutPrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
AboutScreen { }

View file

@ -33,13 +33,13 @@ import helium314.keyboard.latin.utils.CUSTOM_FUNCTIONAL_LAYOUT_SYMBOLS_SHIFTED
import helium314.keyboard.latin.utils.CUSTOM_LAYOUT_PREFIX
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.SettingsDestination
import helium314.keyboard.settings.preferences.SliderPreference
import helium314.keyboard.settings.preferences.SwitchPreference
@ -73,14 +73,14 @@ fun AdvancedSettingsScreen(
Settings.PREF_ABC_AFTER_CLIP,
Settings.PREF_CUSTOM_CURRENCY_KEY,
Settings.PREF_MORE_POPUP_KEYS,
NonSettingsPrefs.CUSTOM_SYMBOLS_NUMBER_LAYOUTS,
NonSettingsPrefs.CUSTOM_FUNCTIONAL_LAYOUTS,
NonSettingsPrefs.BACKUP_RESTORE,
if (BuildConfig.DEBUG || prefs.getBoolean(DebugSettings.PREF_SHOW_DEBUG_SETTINGS, false)) NonSettingsPrefs.DEBUG_SETTINGS else null,
SettingsWithoutKey.CUSTOM_SYMBOLS_NUMBER_LAYOUTS,
SettingsWithoutKey.CUSTOM_FUNCTIONAL_LAYOUTS,
SettingsWithoutKey.BACKUP_RESTORE,
if (BuildConfig.DEBUG || prefs.getBoolean(DebugSettings.PREF_SHOW_DEBUG_SETTINGS, false)) SettingsWithoutKey.DEBUG_SETTINGS else null,
R.string.settings_category_experimental,
Settings.PREF_EMOJI_MAX_SDK,
Settings.PREF_URL_DETECTION,
if (BuildConfig.BUILD_TYPE != "nouserlib") NonSettingsPrefs.LOAD_GESTURE_LIB else null
if (BuildConfig.BUILD_TYPE != "nouserlib") SettingsWithoutKey.LOAD_GESTURE_LIB else null
)
SearchPrefScreen(
onClickBack = onClickBack,
@ -90,84 +90,84 @@ fun AdvancedSettingsScreen(
}
@SuppressLint("ApplySharedPref")
fun createAdvancedPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_ALWAYS_INCOGNITO_MODE,
fun createAdvancedSettings(context: Context) = listOf(
Setting(context, Settings.PREF_ALWAYS_INCOGNITO_MODE,
R.string.incognito, R.string.prefs_force_incognito_mode_summary)
{
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_KEY_LONGPRESS_TIMEOUT, R.string.prefs_key_longpress_timeout_settings) { def ->
Setting(context, Settings.PREF_KEY_LONGPRESS_TIMEOUT, R.string.prefs_key_longpress_timeout_settings) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 300,
range = 100f..700f,
description = { stringResource(R.string.abbreviation_unit_milliseconds, it.toString()) }
)
},
PrefDef(context, Settings.PREF_SPACE_HORIZONTAL_SWIPE, R.string.show_horizontal_space_swipe) { def ->
Setting(context, Settings.PREF_SPACE_HORIZONTAL_SWIPE, R.string.show_horizontal_space_swipe) {
val items = listOf(
stringResource(R.string.space_swipe_move_cursor_entry) to "move_cursor",
stringResource(R.string.switch_language) to "switch_language",
stringResource(R.string.space_swipe_toggle_numpad_entry) to "toggle_numpad",
stringResource(R.string.action_none) to "none",
)
ListPreference(def, items, "move_cursor")
ListPreference(it, items, "move_cursor")
},
PrefDef(context, Settings.PREF_SPACE_VERTICAL_SWIPE, R.string.show_vertical_space_swipe) { def ->
Setting(context, Settings.PREF_SPACE_VERTICAL_SWIPE, R.string.show_vertical_space_swipe) {
val items = listOf(
stringResource(R.string.space_swipe_move_cursor_entry) to "move_cursor",
stringResource(R.string.switch_language) to "switch_language",
stringResource(R.string.space_swipe_toggle_numpad_entry) to "toggle_numpad",
stringResource(R.string.action_none) to "none",
)
ListPreference(def, items, "none")
ListPreference(it, items, "none")
},
PrefDef(context, Settings.PREF_LANGUAGE_SWIPE_DISTANCE, R.string.prefs_language_swipe_distance) { def ->
Setting(context, Settings.PREF_LANGUAGE_SWIPE_DISTANCE, R.string.prefs_language_swipe_distance) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 5,
range = 2f..18f,
description = { it.toString() }
)
},
PrefDef(context, Settings.PREF_DELETE_SWIPE, R.string.delete_swipe, R.string.delete_swipe_summary) {
Setting(context, Settings.PREF_DELETE_SWIPE, R.string.delete_swipe, R.string.delete_swipe_summary) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_SPACE_TO_CHANGE_LANG,
Setting(context, Settings.PREF_SPACE_TO_CHANGE_LANG,
R.string.prefs_long_press_keyboard_to_change_lang,
R.string.prefs_long_press_keyboard_to_change_lang_summary)
{
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREFS_LONG_PRESS_SYMBOLS_FOR_NUMPAD, R.string.prefs_long_press_symbol_for_numpad) {
Setting(context, Settings.PREFS_LONG_PRESS_SYMBOLS_FOR_NUMPAD, R.string.prefs_long_press_symbol_for_numpad) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY, R.string.prefs_enable_emoji_alt_physical_key,
Setting(context, Settings.PREF_ENABLE_EMOJI_ALT_PHYSICAL_KEY, R.string.prefs_enable_emoji_alt_physical_key,
R.string.prefs_enable_emoji_alt_physical_key_summary)
{
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_SHOW_SETUP_WIZARD_ICON, R.string.prefs_enable_emoji_alt_physical_key_summary) {
Setting(context, Settings.PREF_SHOW_SETUP_WIZARD_ICON, R.string.prefs_enable_emoji_alt_physical_key_summary) {
val ctx = LocalContext.current
SwitchPreference(it, true) { SystemBroadcastReceiver.toggleAppIcon(ctx) }
},
PrefDef(context, Settings.PREF_ABC_AFTER_SYMBOL_SPACE,
Setting(context, Settings.PREF_ABC_AFTER_SYMBOL_SPACE,
R.string.switch_keyboard_after, R.string.after_symbol_and_space)
{
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_ABC_AFTER_EMOJI, R.string.switch_keyboard_after, R.string.after_emoji) {
Setting(context, Settings.PREF_ABC_AFTER_EMOJI, R.string.switch_keyboard_after, R.string.after_emoji) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_ABC_AFTER_CLIP, R.string.switch_keyboard_after, R.string.after_clip) {
Setting(context, Settings.PREF_ABC_AFTER_CLIP, R.string.switch_keyboard_after, R.string.after_clip) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_CUSTOM_CURRENCY_KEY, R.string.customize_currencies) { def ->
Setting(context, Settings.PREF_CUSTOM_CURRENCY_KEY, R.string.customize_currencies) { setting ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
name = setting.title,
onClick = { showDialog = true }
)
if (showDialog) {
@ -175,45 +175,45 @@ fun createAdvancedPrefs(context: Context) = listOf(
TextInputDialog(
onDismissRequest = { showDialog = false },
textInputLabel = { Text(stringResource(R.string.customize_currencies_detail)) },
initialText = prefs.getString(def.key, "")!!,
onConfirmed = { prefs.edit().putString(def.key, it).apply(); KeyboardLayoutSet.onSystemLocaleChanged() },
initialText = prefs.getString(setting.key, "")!!,
onConfirmed = { prefs.edit().putString(setting.key, it).apply(); KeyboardLayoutSet.onSystemLocaleChanged() },
title = { Text(stringResource(R.string.customize_currencies)) },
neutralButtonText = if (prefs.contains(def.key)) stringResource(R.string.button_default) else null,
onNeutral = { prefs.edit().remove(def.key).apply(); KeyboardLayoutSet.onSystemLocaleChanged() },
neutralButtonText = if (prefs.contains(setting.key)) stringResource(R.string.button_default) else null,
onNeutral = { prefs.edit().remove(setting.key).apply(); KeyboardLayoutSet.onSystemLocaleChanged() },
checkTextValid = { text -> text.splitOnWhitespace().none { it.length > 8 } }
)
}
},
PrefDef(context, Settings.PREF_MORE_POPUP_KEYS, R.string.show_popup_keys_title) { def ->
Setting(context, Settings.PREF_MORE_POPUP_KEYS, R.string.show_popup_keys_title) {
val items = listOf(
stringResource(R.string.show_popup_keys_normal) to "normal",
stringResource(R.string.show_popup_keys_main) to "main",
stringResource(R.string.show_popup_keys_more) to "more",
stringResource(R.string.show_popup_keys_all) to "all",
)
ListPreference(def, items, "main") { KeyboardLayoutSet.onSystemLocaleChanged() }
ListPreference(it, items, "main") { KeyboardLayoutSet.onSystemLocaleChanged() }
},
PrefDef(context, NonSettingsPrefs.CUSTOM_SYMBOLS_NUMBER_LAYOUTS, R.string.customize_symbols_number_layouts) { def ->
Setting(context, SettingsWithoutKey.CUSTOM_SYMBOLS_NUMBER_LAYOUTS, R.string.customize_symbols_number_layouts) { setting ->
LayoutEditPreference(
def = def,
setting = setting,
items = RawKeyboardParser.symbolAndNumberLayouts,
getItemName = { it.getStringResourceOrName("layout_", LocalContext.current) },
getDefaultLayout = { LocalContext.current.assets.list("layouts")?.firstOrNull { it.startsWith("$it.") } }
)
},
PrefDef(context, NonSettingsPrefs.CUSTOM_FUNCTIONAL_LAYOUTS, R.string.customize_functional_key_layouts) { def ->
Setting(context, SettingsWithoutKey.CUSTOM_FUNCTIONAL_LAYOUTS, R.string.customize_functional_key_layouts) { setting ->
LayoutEditPreference(
def = def,
setting = setting,
items = listOf(CUSTOM_FUNCTIONAL_LAYOUT_NORMAL, CUSTOM_FUNCTIONAL_LAYOUT_SYMBOLS, CUSTOM_FUNCTIONAL_LAYOUT_SYMBOLS_SHIFTED)
.map { it.substringBeforeLast(".") },
getItemName = { it.substringAfter(CUSTOM_LAYOUT_PREFIX).getStringResourceOrName("layout_", LocalContext.current) },
getDefaultLayout = { if (Settings.getInstance().isTablet) "functional_keys_tablet.json" else "functional_keys.json" }
)
},
PrefDef(context, NonSettingsPrefs.BACKUP_RESTORE, R.string.backup_restore_title) {
Setting(context, SettingsWithoutKey.BACKUP_RESTORE, R.string.backup_restore_title) {
BackupRestorePreference(it)
},
PrefDef(context, NonSettingsPrefs.DEBUG_SETTINGS, R.string.debug_settings_title) {
Setting(context, SettingsWithoutKey.DEBUG_SETTINGS, R.string.debug_settings_title) {
Preference(
name = it.title,
onClick = { SettingsDestination.navigateTo(SettingsDestination.Debug) }
@ -225,10 +225,10 @@ fun createAdvancedPrefs(context: Context) = listOf(
)
}
},
PrefDef(context, Settings.PREF_EMOJI_MAX_SDK, R.string.prefs_key_emoji_max_sdk) { def ->
Setting(context, Settings.PREF_EMOJI_MAX_SDK, R.string.prefs_key_emoji_max_sdk) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = Build.VERSION.SDK_INT,
range = 21f..35f,
description = {
@ -254,10 +254,10 @@ fun createAdvancedPrefs(context: Context) = listOf(
onValueChanged = { keyboardNeedsReload = true }
)
},
PrefDef(context, Settings.PREF_URL_DETECTION, R.string.url_detection_title, R.string.url_detection_summary) {
Setting(context, Settings.PREF_URL_DETECTION, R.string.url_detection_title, R.string.url_detection_summary) {
SwitchPreference(it, false)
},
PrefDef(context, NonSettingsPrefs.LOAD_GESTURE_LIB, R.string.load_gesture_library, R.string.load_gesture_library_summary) {
Setting(context, SettingsWithoutKey.LOAD_GESTURE_LIB, R.string.load_gesture_library, R.string.load_gesture_library_summary) {
LoadGestureLibPreference(it)
},
)
@ -265,7 +265,7 @@ fun createAdvancedPrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
AdvancedSettingsScreen { }

View file

@ -26,13 +26,13 @@ import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.getStringResourceOrName
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.latin.utils.switchTo
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SliderPreference
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
@ -48,7 +48,7 @@ fun AppearanceScreen(
) {
val ctx = LocalContext.current
val prefs = ctx.prefs()
val b = (LocalContext.current.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (LocalContext.current.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val dayNightMode = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && Settings.readDayNightPref(prefs, ctx.resources)
@ -59,16 +59,16 @@ fun AppearanceScreen(
Settings.PREF_CUSTOM_ICON_NAMES,
Settings.PREF_THEME_COLORS,
if (prefs.getString(Settings.PREF_THEME_COLORS, KeyboardTheme.THEME_LIGHT) == KeyboardTheme.THEME_USER)
NonSettingsPrefs.ADJUST_COLORS else null,
SettingsWithoutKey.ADJUST_COLORS else null,
Settings.PREF_THEME_KEY_BORDERS,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
Settings.PREF_THEME_DAY_NIGHT else null,
if (dayNightMode) Settings.PREF_THEME_COLORS_NIGHT else null,
if (dayNightMode && prefs.getString(Settings.PREF_THEME_COLORS_NIGHT, KeyboardTheme.THEME_DARK) == KeyboardTheme.THEME_USER_NIGHT)
NonSettingsPrefs.ADJUST_COLORS_NIGHT else null,
SettingsWithoutKey.ADJUST_COLORS_NIGHT else null,
Settings.PREF_NAVBAR_COLOR,
NonSettingsPrefs.BACKGROUND_IMAGE,
NonSettingsPrefs.BACKGROUND_IMAGE_LANDSCAPE,
SettingsWithoutKey.BACKGROUND_IMAGE,
SettingsWithoutKey.BACKGROUND_IMAGE_LANDSCAPE,
R.string.settings_category_miscellaneous,
Settings.PREF_ENABLE_SPLIT_KEYBOARD,
Settings.PREF_SPLIT_SPACER_SCALE,
@ -79,7 +79,7 @@ fun AppearanceScreen(
Settings.PREF_SIDE_PADDING_SCALE,
Settings.PREF_SIDE_PADDING_SCALE_LANDSCAPE,
Settings.PREF_SPACE_BAR_TEXT,
NonSettingsPrefs.CUSTOM_FONT,
SettingsWithoutKey.CUSTOM_FONT,
Settings.PREF_FONT_SCALE,
Settings.PREF_EMOJI_FONT_SCALE,
)
@ -90,15 +90,15 @@ fun AppearanceScreen(
)
}
fun createAppearancePrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_THEME_STYLE, R.string.theme_style) { def ->
fun createAppearanceSettings(context: Context) = listOf(
Setting(context, Settings.PREF_THEME_STYLE, R.string.theme_style) { setting ->
val ctx = LocalContext.current
val prefs = ctx.prefs()
val items = KeyboardTheme.STYLES.map {
it.getStringResourceOrName("style_name_", ctx) to it
}
ListPreference(
def,
setting,
items,
KeyboardTheme.STYLE_MATERIAL
) {
@ -111,19 +111,19 @@ fun createAppearancePrefs(context: Context) = listOf(
}
}
},
PrefDef(context, Settings.PREF_ICON_STYLE, R.string.icon_style) { def ->
Setting(context, Settings.PREF_ICON_STYLE, R.string.icon_style) { setting ->
val ctx = LocalContext.current
val items = KeyboardTheme.STYLES.map { it.getStringResourceOrName("style_name_", ctx) to it }
ListPreference(
def,
setting,
items,
KeyboardTheme.STYLE_MATERIAL
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_CUSTOM_ICON_NAMES, R.string.customize_icons) { def ->
Setting(context, Settings.PREF_CUSTOM_ICON_NAMES, R.string.customize_icons) { setting ->
var showDialog by remember { mutableStateOf(false) }
Preference(
name = def.title,
name = setting.title,
onClick = { showDialog = true }
)
if (showDialog) {
@ -131,12 +131,12 @@ fun createAppearancePrefs(context: Context) = listOf(
KeyboardSwitcher.getInstance().forceUpdateKeyboardTheme(LocalContext.current)
keyboardNeedsReload = false
}
CustomizeIconsDialog(def.key) { showDialog = false }
CustomizeIconsDialog(setting.key) { showDialog = false }
}
},
PrefDef(context, Settings.PREF_THEME_COLORS, R.string.theme_colors) { def ->
Setting(context, Settings.PREF_THEME_COLORS, R.string.theme_colors) { setting ->
val ctx = LocalContext.current
val b = (ctx.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val currentStyle = ctx.prefs().getString(Settings.PREF_THEME_STYLE, KeyboardTheme.STYLE_MATERIAL)
@ -146,14 +146,14 @@ fun createAppearancePrefs(context: Context) = listOf(
it.getStringResourceOrName("theme_name_", ctx) to it
}
ListPreference(
def,
setting,
items,
KeyboardTheme.THEME_LIGHT
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_THEME_COLORS_NIGHT, R.string.theme_colors_night) { def ->
Setting(context, Settings.PREF_THEME_COLORS_NIGHT, R.string.theme_colors_night) { setting ->
val ctx = LocalContext.current
val b = (ctx.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (ctx.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val currentStyle = ctx.prefs().getString(Settings.PREF_THEME_STYLE, KeyboardTheme.STYLE_MATERIAL)
@ -163,147 +163,147 @@ fun createAppearancePrefs(context: Context) = listOf(
it.getStringResourceOrName("theme_name_", ctx) to it
}
ListPreference(
def,
setting,
items,
KeyboardTheme.THEME_DARK
) { keyboardNeedsReload = true }
},
PrefDef(context, NonSettingsPrefs.ADJUST_COLORS, R.string.select_user_colors, R.string.select_user_colors_summary) { def ->
Setting(context, SettingsWithoutKey.ADJUST_COLORS, R.string.select_user_colors, R.string.select_user_colors_summary) {
val ctx = LocalContext.current
Preference(
name = def.title,
description = def.description,
name = it.title,
description = it.description,
onClick = {
ctx.getActivity()?.switchTo(ColorsSettingsFragment())
//SettingsDestination.navigateTo(SettingsDestination.Colors) todo: later
}
)
},
PrefDef(context, NonSettingsPrefs.ADJUST_COLORS_NIGHT, R.string.select_user_colors_night, R.string.select_user_colors_summary) { def ->
Setting(context, SettingsWithoutKey.ADJUST_COLORS_NIGHT, R.string.select_user_colors_night, R.string.select_user_colors_summary) {
val ctx = LocalContext.current
Preference(
name = def.title,
description = def.description,
name = it.title,
description = it.description,
onClick = {
ctx.getActivity()?.switchTo(ColorsNightSettingsFragment())
//SettingsDestination.navigateTo(SettingsDestination.ColorsNight) todo: later
}
)
},
PrefDef(context, Settings.PREF_THEME_KEY_BORDERS, R.string.key_borders) {
Setting(context, Settings.PREF_THEME_KEY_BORDERS, R.string.key_borders) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_THEME_DAY_NIGHT, R.string.day_night_mode, R.string.day_night_mode_summary) {
Setting(context, Settings.PREF_THEME_DAY_NIGHT, R.string.day_night_mode, R.string.day_night_mode_summary) {
SwitchPreference(it, Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_NAVBAR_COLOR, R.string.theme_navbar, R.string.day_night_mode_summary) {
Setting(context, Settings.PREF_NAVBAR_COLOR, R.string.theme_navbar, R.string.day_night_mode_summary) {
SwitchPreference(it, Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
},
PrefDef(context, NonSettingsPrefs.BACKGROUND_IMAGE, R.string.customize_background_image) {
Setting(context, SettingsWithoutKey.BACKGROUND_IMAGE, R.string.customize_background_image) {
BackgroundImagePref(it, false)
},
PrefDef(context, NonSettingsPrefs.BACKGROUND_IMAGE_LANDSCAPE,
Setting(context, SettingsWithoutKey.BACKGROUND_IMAGE_LANDSCAPE,
R.string.customize_background_image_landscape, R.string.summary_customize_background_image_landscape)
{
BackgroundImagePref(it, true)
},
PrefDef(context, Settings.PREF_ENABLE_SPLIT_KEYBOARD, R.string.enable_split_keyboard) {
Setting(context, Settings.PREF_ENABLE_SPLIT_KEYBOARD, R.string.enable_split_keyboard) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_SPLIT_SPACER_SCALE, R.string.split_spacer_scale) { def ->
Setting(context, Settings.PREF_SPLIT_SPACER_SCALE, R.string.split_spacer_scale) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = SettingsValues.DEFAULT_SIZE_SCALE,
range = 0.5f..2f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_NARROW_KEY_GAPS, R.string.prefs_narrow_key_gaps) {
Setting(context, Settings.PREF_NARROW_KEY_GAPS, R.string.prefs_narrow_key_gaps) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_KEYBOARD_HEIGHT_SCALE, R.string.prefs_keyboard_height_scale) { def ->
Setting(context, Settings.PREF_KEYBOARD_HEIGHT_SCALE, R.string.prefs_keyboard_height_scale) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = SettingsValues.DEFAULT_SIZE_SCALE,
range = 0.5f..1.5f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_BOTTOM_PADDING_SCALE, R.string.prefs_bottom_padding_scale) { def ->
Setting(context, Settings.PREF_BOTTOM_PADDING_SCALE, R.string.prefs_bottom_padding_scale) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = SettingsValues.DEFAULT_SIZE_SCALE,
range = 0f..5f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_BOTTOM_PADDING_SCALE_LANDSCAPE, R.string.prefs_bottom_padding_scale_landscape) { def ->
Setting(context, Settings.PREF_BOTTOM_PADDING_SCALE_LANDSCAPE, R.string.prefs_bottom_padding_scale_landscape) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 0f,
range = 0f..5f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_SIDE_PADDING_SCALE, R.string.prefs_side_padding_scale) { def ->
Setting(context, Settings.PREF_SIDE_PADDING_SCALE, R.string.prefs_side_padding_scale) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 0f,
range = 0f..3f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_SIDE_PADDING_SCALE_LANDSCAPE, R.string.prefs_side_padding_scale_landscape) { def ->
Setting(context, Settings.PREF_SIDE_PADDING_SCALE_LANDSCAPE, R.string.prefs_side_padding_scale_landscape) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 0f,
range = 0f..3f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_SPACE_BAR_TEXT, R.string.prefs_space_bar_text) { def ->
Setting(context, Settings.PREF_SPACE_BAR_TEXT, R.string.prefs_space_bar_text) { setting ->
var showDialog by remember { mutableStateOf(false) }
val prefs = LocalContext.current.prefs()
Preference(
name = def.title,
name = setting.title,
onClick = { showDialog = true },
description = prefs.getString(def.key, "")
description = prefs.getString(setting.key, "")
)
if (showDialog) {
TextInputDialog(
onDismissRequest = { showDialog = false },
onConfirmed = {
prefs.edit().putString(def.key, it).apply()
prefs.edit().putString(setting.key, it).apply()
keyboardNeedsReload = true
},
initialText = prefs.getString(def.key, "") ?: "",
title = { Text(def.title) },
initialText = prefs.getString(setting.key, "") ?: "",
title = { Text(setting.title) },
checkTextValid = { true }
)
}
},
PrefDef(context, NonSettingsPrefs.CUSTOM_FONT, R.string.custom_font) {
Setting(context, SettingsWithoutKey.CUSTOM_FONT, R.string.custom_font) {
CustomFontPreference(it)
},
PrefDef(context, Settings.PREF_FONT_SCALE, R.string.prefs_font_scale) { def ->
Setting(context, Settings.PREF_FONT_SCALE, R.string.prefs_font_scale) { def ->
SliderPreference(
name = def.title,
pref = def.key,
key = def.key,
default = 1f,
range = 0.5f..1.5f,
description = { "${(100 * it).toInt()}%" }
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_EMOJI_FONT_SCALE, R.string.prefs_emoji_font_scale) { def ->
Setting(context, Settings.PREF_EMOJI_FONT_SCALE, R.string.prefs_emoji_font_scale) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 1f,
range = 0.5f..1.5f,
description = { "${(100 * it).toInt()}%" }
@ -314,7 +314,7 @@ fun createAppearancePrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
AppearanceScreen { }

View file

@ -27,7 +27,7 @@ fun ColorsScreen(
}
private class ColorSetting(
val pref: String, // old, this should go away
val key: String, // old, this should go away
val displayName: String,
var auto: Boolean, // not for all
var color: Int

View file

@ -20,11 +20,11 @@ import helium314.keyboard.latin.R
import helium314.keyboard.latin.settings.DebugSettings
import helium314.keyboard.latin.settings.DebugSettingsFragment
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ConfirmationDialog
@ -49,21 +49,18 @@ fun DebugScreen(
)
}
fun createDebugPrefs(context: Context) = listOf(
PrefDef(context, DebugSettings.PREF_SHOW_DEBUG_SETTINGS, R.string.prefs_show_debug_settings) { def ->
fun createDebugSettings(context: Context) = listOf(
Setting(context, DebugSettings.PREF_SHOW_DEBUG_SETTINGS, R.string.prefs_show_debug_settings) { setting ->
val prefs = LocalContext.current.prefs()
SwitchPreference(
name = def.title,
pref = def.key,
default = false,
) { if (!it) prefs.edit().putBoolean(DebugSettings.PREF_DEBUG_MODE, false).apply() }
SwitchPreference(setting, false)
{ if (!it) prefs.edit().putBoolean(DebugSettings.PREF_DEBUG_MODE, false).apply() }
},
PrefDef(context, DebugSettings.PREF_DEBUG_MODE, R.string.prefs_debug_mode) { def ->
Setting(context, DebugSettings.PREF_DEBUG_MODE, R.string.prefs_debug_mode) { setting ->
val prefs = LocalContext.current.prefs()
var showConfirmDialog by remember { mutableStateOf(false) }
SwitchPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
description = stringResource(R.string.version_text, BuildConfig.VERSION_NAME),
default = false,
) {
@ -78,12 +75,12 @@ fun createDebugPrefs(context: Context) = listOf(
)
}
},
PrefDef(context, DebugSettings.PREF_SHOW_SUGGESTION_INFOS, R.string.prefs_show_suggestion_infos) { def ->
SwitchPreference(def, false) { keyboardNeedsReload = true }
Setting(context, DebugSettings.PREF_SHOW_SUGGESTION_INFOS, R.string.prefs_show_suggestion_infos) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH, R.string.prefs_force_non_distinct_multitouch) { def ->
Setting(context, DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH, R.string.prefs_force_non_distinct_multitouch) {
var showConfirmDialog by remember { mutableStateOf(false) }
SwitchPreference(def, false) { showConfirmDialog = true }
SwitchPreference(it, false) { showConfirmDialog = true }
if (showConfirmDialog) {
ConfirmationDialog(
onDismissRequest = { showConfirmDialog = false },
@ -92,17 +89,17 @@ fun createDebugPrefs(context: Context) = listOf(
)
}
},
PrefDef(context, DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, R.string.sliding_key_input_preview, R.string.sliding_key_input_preview_summary) { def ->
Setting(context, DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, R.string.sliding_key_input_preview, R.string.sliding_key_input_preview_summary) { def ->
SwitchPreference(def, false)
},
) + DictionaryFacilitator.DYNAMIC_DICTIONARY_TYPES.map {
PrefDef(context, DebugSettingsFragment.PREF_KEY_DUMP_DICT_PREFIX + it, R.string.button_default) { def ->
) + DictionaryFacilitator.DYNAMIC_DICTIONARY_TYPES.map { type ->
Setting(context, DebugSettingsFragment.PREF_KEY_DUMP_DICT_PREFIX + type, R.string.button_default) {
val ctx = LocalContext.current
Preference(
name = "Dump $it dictionary",
name = "Dump $type dictionary",
onClick = {
val intent = Intent(DictionaryDumpBroadcastReceiver.DICTIONARY_DUMP_INTENT_ACTION)
intent.putExtra(DictionaryDumpBroadcastReceiver.DICTIONARY_NAME_KEY, it)
intent.putExtra(DictionaryDumpBroadcastReceiver.DICTIONARY_NAME_KEY, type)
ctx.sendBroadcast(intent)
}
)
@ -112,7 +109,7 @@ fun createDebugPrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
DebugScreen { }

View file

@ -13,10 +13,10 @@ import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SliderPreference
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
@ -27,7 +27,7 @@ fun GestureTypingScreen(
onClickBack: () -> Unit,
) {
val prefs = LocalContext.current.prefs()
val b = (LocalContext.current.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (LocalContext.current.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val gestureFloatingPreviewEnabled = prefs.getBoolean(Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT, true)
@ -51,19 +51,19 @@ fun GestureTypingScreen(
)
}
fun createGestureTypingPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_GESTURE_INPUT, R.string.gesture_input, R.string.gesture_input_summary) {
fun createGestureTypingSettings(context: Context) = listOf(
Setting(context, Settings.PREF_GESTURE_INPUT, R.string.gesture_input, R.string.gesture_input_summary) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_GESTURE_PREVIEW_TRAIL, R.string.gesture_preview_trail) {
Setting(context, Settings.PREF_GESTURE_PREVIEW_TRAIL, R.string.gesture_preview_trail) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT,
Setting(context, Settings.PREF_GESTURE_FLOATING_PREVIEW_TEXT,
R.string.gesture_floating_preview_static, R.string.gesture_floating_preview_static_summary)
{
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_GESTURE_FLOATING_PREVIEW_DYNAMIC,
Setting(context, Settings.PREF_GESTURE_FLOATING_PREVIEW_DYNAMIC,
R.string.gesture_floating_preview_text, R.string.gesture_floating_preview_dynamic_summary)
{ def ->
val ctx = LocalContext.current
@ -77,13 +77,13 @@ fun createGestureTypingPrefs(context: Context) = listOf(
keyboardNeedsReload = true
}
},
PrefDef(context, Settings.PREF_GESTURE_SPACE_AWARE, R.string.gesture_space_aware, R.string.gesture_space_aware_summary) {
Setting(context, Settings.PREF_GESTURE_SPACE_AWARE, R.string.gesture_space_aware, R.string.gesture_space_aware_summary) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_GESTURE_FAST_TYPING_COOLDOWN, R.string.gesture_fast_typing_cooldown) { def ->
Setting(context, Settings.PREF_GESTURE_FAST_TYPING_COOLDOWN, R.string.gesture_fast_typing_cooldown) { def ->
SliderPreference(
name = def.title,
pref = def.key,
key = def.key,
default = 500,
range = 0f..500f,
description = {
@ -92,10 +92,10 @@ fun createGestureTypingPrefs(context: Context) = listOf(
}
)
},
PrefDef(context, Settings.PREF_GESTURE_TRAIL_FADEOUT_DURATION, R.string.gesture_trail_fadeout_duration) { def ->
Setting(context, Settings.PREF_GESTURE_TRAIL_FADEOUT_DURATION, R.string.gesture_trail_fadeout_duration) { def ->
SliderPreference(
name = def.title,
pref = def.key,
key = def.key,
default = 800,
range = 100f..1900f,
description = { stringResource(R.string.abbreviation_unit_milliseconds, (it + 100).toString()) },
@ -107,7 +107,7 @@ fun createGestureTypingPrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
GestureTypingScreen { }

View file

@ -22,12 +22,12 @@ import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.getEnabledSubtypes
import helium314.keyboard.latin.utils.locale
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.ReorderSwitchPreference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SliderPreference
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
@ -38,7 +38,7 @@ fun PreferencesScreen(
onClickBack: () -> Unit,
) {
val prefs = LocalContext.current.prefs()
val b = (LocalContext.current.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (LocalContext.current.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val items = listOfNotNull(
@ -79,51 +79,51 @@ fun PreferencesScreen(
)
}
fun createPreferencesPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_SHOW_HINTS, R.string.show_hints, R.string.show_hints_summary) {
fun createPreferencesSettings(context: Context) = listOf(
Setting(context, Settings.PREF_SHOW_HINTS, R.string.show_hints, R.string.show_hints_summary) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_POPUP_KEYS_LABELS_ORDER, R.string.hint_source) {
Setting(context, Settings.PREF_POPUP_KEYS_LABELS_ORDER, R.string.hint_source) {
ReorderSwitchPreference(it, POPUP_KEYS_LABEL_DEFAULT)
},
PrefDef(context, Settings.PREF_POPUP_KEYS_ORDER, R.string.popup_order) {
Setting(context, Settings.PREF_POPUP_KEYS_ORDER, R.string.popup_order) {
ReorderSwitchPreference(it, POPUP_KEYS_ORDER_DEFAULT)
},
PrefDef(context, Settings.PREF_SHOW_POPUP_HINTS, R.string.show_popup_hints, R.string.show_popup_hints_summary) {
Setting(context, Settings.PREF_SHOW_POPUP_HINTS, R.string.show_popup_hints, R.string.show_popup_hints_summary) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_POPUP_ON, R.string.popup_on_keypress) {
Setting(context, Settings.PREF_POPUP_ON, R.string.popup_on_keypress) {
val dm = LocalContext.current.resources.displayMetrics
val px600 = with(LocalDensity.current) { 600.dp.toPx() }
SwitchPreference(it, dm.widthPixels >= px600 || dm.heightPixels >= px600)
},
PrefDef(context, Settings.PREF_VIBRATE_ON, R.string.vibrate_on_keypress) {
Setting(context, Settings.PREF_VIBRATE_ON, R.string.vibrate_on_keypress) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_VIBRATE_IN_DND_MODE, R.string.vibrate_in_dnd_mode) {
Setting(context, Settings.PREF_VIBRATE_IN_DND_MODE, R.string.vibrate_in_dnd_mode) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_SOUND_ON, R.string.sound_on_keypress) {
Setting(context, Settings.PREF_SOUND_ON, R.string.sound_on_keypress) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_ENABLE_CLIPBOARD_HISTORY,
Setting(context, Settings.PREF_ENABLE_CLIPBOARD_HISTORY,
R.string.enable_clipboard_history, R.string.enable_clipboard_history_summary)
{
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_SHOW_NUMBER_ROW, R.string.number_row, R.string.number_row_summary) {
Setting(context, Settings.PREF_SHOW_NUMBER_ROW, R.string.number_row, R.string.number_row_summary) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_LOCALIZED_NUMBER_ROW, R.string.localized_number_row, R.string.localized_number_row_summary) {
Setting(context, Settings.PREF_LOCALIZED_NUMBER_ROW, R.string.localized_number_row, R.string.localized_number_row_summary) {
SwitchPreference(it, true) { KeyboardLayoutSet.onSystemLocaleChanged() }
},
PrefDef(context, Settings.PREF_SHOW_NUMBER_ROW_HINTS, R.string.number_row_hints) {
Setting(context, Settings.PREF_SHOW_NUMBER_ROW_HINTS, R.string.number_row_hints) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY, R.string.show_language_switch_key) {
Setting(context, Settings.PREF_SHOW_LANGUAGE_SWITCH_KEY, R.string.show_language_switch_key) {
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_LANGUAGE_SWITCH_KEY, R.string.language_switch_key_behavior) {
Setting(context, Settings.PREF_LANGUAGE_SWITCH_KEY, R.string.language_switch_key_behavior) {
ListPreference(
it,
listOf(
@ -134,18 +134,18 @@ fun createPreferencesPrefs(context: Context) = listOf(
"internal"
) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_SHOW_EMOJI_KEY, R.string.show_emoji_key) {
Setting(context, Settings.PREF_SHOW_EMOJI_KEY, R.string.show_emoji_key) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_REMOVE_REDUNDANT_POPUPS,
Setting(context, Settings.PREF_REMOVE_REDUNDANT_POPUPS,
R.string.remove_redundant_popups, R.string.remove_redundant_popups_summary)
{
SwitchPreference(it, false) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_CLIPBOARD_HISTORY_RETENTION_TIME, R.string.clipboard_history_retention_time) { def ->
Setting(context, Settings.PREF_CLIPBOARD_HISTORY_RETENTION_TIME, R.string.clipboard_history_retention_time) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = 10,
description = {
if (it < 0) stringResource(R.string.settings_no_limit)
@ -154,10 +154,10 @@ fun createPreferencesPrefs(context: Context) = listOf(
range = -1f..120f,
)
},
PrefDef(context, Settings.PREF_VIBRATION_DURATION_SETTINGS, R.string.prefs_keypress_vibration_duration_settings) { def ->
Setting(context, Settings.PREF_VIBRATION_DURATION_SETTINGS, R.string.prefs_keypress_vibration_duration_settings) { setting ->
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = -1,
description = {
if (it < 0) stringResource(R.string.settings_system_default)
@ -167,11 +167,11 @@ fun createPreferencesPrefs(context: Context) = listOf(
onValueChanged = { AudioAndHapticFeedbackManager.getInstance().vibrate(it.toLong()) }
)
},
PrefDef(context, Settings.PREF_KEYPRESS_SOUND_VOLUME, R.string.prefs_keypress_sound_volume_settings) { def ->
Setting(context, Settings.PREF_KEYPRESS_SOUND_VOLUME, R.string.prefs_keypress_sound_volume_settings) { setting ->
val audioManager = LocalContext.current.getSystemService(Context.AUDIO_SERVICE) as AudioManager
SliderPreference(
name = def.title,
pref = def.key,
name = setting.title,
key = setting.key,
default = -0.01f,
description = {
if (it < 0) stringResource(R.string.settings_system_default)
@ -189,7 +189,7 @@ private val localesWithLocalizedNumberRow = listOf("ar", "bn", "fa", "gu", "hi",
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
PreferencesScreen { }

View file

@ -28,13 +28,13 @@ import helium314.keyboard.latin.utils.Log
import helium314.keyboard.latin.utils.getActivity
import helium314.keyboard.latin.utils.prefs
import helium314.keyboard.latin.utils.switchTo
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.preferences.ListPreference
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ConfirmationDialog
@ -45,13 +45,13 @@ fun TextCorrectionScreen(
onClickBack: () -> Unit,
) {
val prefs = LocalContext.current.prefs()
val b = (LocalContext.current.getActivity() as? SettingsActivity2)?.prefChanged?.collectAsState()
val b = (LocalContext.current.getActivity() as? SettingsActivity)?.prefChanged?.collectAsState()
if ((b?.value ?: 0) < 0)
Log.v("irrelevant", "stupid way to trigger recomposition on preference change")
val autocorrectEnabled = prefs.getBoolean(Settings.PREF_AUTO_CORRECTION, true)
val suggestionsEnabled = prefs.getBoolean(Settings.PREF_SHOW_SUGGESTIONS, true)
val items = listOfNotNull(
NonSettingsPrefs.EDIT_PERSONAL_DICTIONARY,
SettingsWithoutKey.EDIT_PERSONAL_DICTIONARY,
R.string.settings_category_correction,
Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE,
Settings.PREF_AUTO_CORRECTION,
@ -79,8 +79,8 @@ fun TextCorrectionScreen(
)
}
fun createCorrectionPrefs(context: Context) = listOf(
PrefDef(context, NonSettingsPrefs.EDIT_PERSONAL_DICTIONARY, R.string.edit_personal_dictionary) {
fun createCorrectionSettings(context: Context) = listOf(
Setting(context, SettingsWithoutKey.EDIT_PERSONAL_DICTIONARY, R.string.edit_personal_dictionary) {
val ctx = LocalContext.current
Preference(
name = stringResource(R.string.edit_personal_dictionary),
@ -93,27 +93,27 @@ fun createCorrectionPrefs(context: Context) = listOf(
)
}
},
PrefDef(context, Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE,
Setting(context, Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE,
R.string.prefs_block_potentially_offensive_title, R.string.prefs_block_potentially_offensive_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_AUTO_CORRECTION,
Setting(context, Settings.PREF_AUTO_CORRECTION,
R.string.autocorrect, R.string.auto_correction_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_MORE_AUTO_CORRECTION,
Setting(context, Settings.PREF_MORE_AUTO_CORRECTION,
R.string.more_autocorrect, R.string.more_autocorrect_summary
) {
SwitchPreference(it, true) // todo (later): shouldn't it better be false?
},
PrefDef(context, Settings.PREF_AUTOCORRECT_SHORTCUTS,
Setting(context, Settings.PREF_AUTOCORRECT_SHORTCUTS,
R.string.auto_correct_shortcuts, R.string.auto_correct_shortcuts_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_AUTO_CORRECTION_CONFIDENCE, R.string.auto_correction_confidence) {
Setting(context, Settings.PREF_AUTO_CORRECTION_CONFIDENCE, R.string.auto_correction_confidence) {
val items = listOf(
stringResource(R.string.auto_correction_threshold_mode_modest) to "0",
stringResource(R.string.auto_correction_threshold_mode_aggressive) to "1",
@ -121,38 +121,36 @@ fun createCorrectionPrefs(context: Context) = listOf(
)
ListPreference(it, items, "0")
},
PrefDef(context, Settings.PREF_AUTO_CAP,
Setting(context, Settings.PREF_AUTO_CAP,
R.string.auto_cap, R.string.auto_cap_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD,
Setting(context, Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD,
R.string.use_double_space_period, R.string.use_double_space_period_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION,
Setting(context, Settings.PREF_AUTOSPACE_AFTER_PUNCTUATION,
R.string.autospace_after_punctuation, R.string.autospace_after_punctuation_summary
) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_SHOW_SUGGESTIONS,
Setting(context, Settings.PREF_SHOW_SUGGESTIONS,
R.string.prefs_show_suggestions, R.string.prefs_show_suggestions_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_ALWAYS_SHOW_SUGGESTIONS,
Setting(context, Settings.PREF_ALWAYS_SHOW_SUGGESTIONS,
R.string.prefs_always_show_suggestions, R.string.prefs_always_show_suggestions_summary
) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_KEY_USE_PERSONALIZED_DICTS,
Setting(context, Settings.PREF_KEY_USE_PERSONALIZED_DICTS,
R.string.use_personalized_dicts, R.string.use_personalized_dicts_summary
) { prefDef ->
) { setting ->
var showConfirmDialog by remember { mutableStateOf(false) }
SwitchPreference(
prefDef,
true,
SwitchPreference(setting, true,
allowCheckedChange = {
showConfirmDialog = !it
it
@ -163,41 +161,39 @@ fun createCorrectionPrefs(context: Context) = listOf(
ConfirmationDialog(
onDismissRequest = { showConfirmDialog = false },
onConfirmed = {
prefs.edit().putBoolean(prefDef.key, false).apply()
prefs.edit().putBoolean(setting.key, false).apply()
},
text = { Text(stringResource(R.string.disable_personalized_dicts_message)) }
)
}
},
PrefDef(context, Settings.PREF_BIGRAM_PREDICTIONS,
Setting(context, Settings.PREF_BIGRAM_PREDICTIONS,
R.string.bigram_prediction, R.string.bigram_prediction_summary
) {
SwitchPreference(it, true) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_CENTER_SUGGESTION_TEXT_TO_ENTER,
Setting(context, Settings.PREF_CENTER_SUGGESTION_TEXT_TO_ENTER,
R.string.center_suggestion_text_to_enter, R.string.center_suggestion_text_to_enter_summary
) {
SwitchPreference(it, false)
},
PrefDef(context, Settings.PREF_SUGGEST_CLIPBOARD_CONTENT,
Setting(context, Settings.PREF_SUGGEST_CLIPBOARD_CONTENT,
R.string.suggest_clipboard_content, R.string.suggest_clipboard_content_summary
) {
SwitchPreference(it, true)
},
PrefDef(context, Settings.PREF_USE_CONTACTS,
Setting(context, Settings.PREF_USE_CONTACTS,
R.string.use_contacts_dict, R.string.use_contacts_dict_summary
) { def ->
val activity = LocalContext.current.getActivity() ?: return@PrefDef
) { setting ->
val activity = LocalContext.current.getActivity() ?: return@Setting
var granted by remember { mutableStateOf(PermissionsUtil.checkAllPermissionsGranted(activity, Manifest.permission.READ_CONTACTS)) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) {
granted = it
if (granted)
activity.prefs().edit().putBoolean(def.key, true).apply()
activity.prefs().edit().putBoolean(setting.key, true).apply()
}
SwitchPreference(
def,
false,
SwitchPreference(setting, false,
allowCheckedChange = {
if (it && !granted) {
launcher.launch(Manifest.permission.READ_CONTACTS)
@ -206,7 +202,7 @@ fun createCorrectionPrefs(context: Context) = listOf(
}
)
},
PrefDef(context, Settings.PREF_ADD_TO_PERSONAL_DICTIONARY,
Setting(context, Settings.PREF_ADD_TO_PERSONAL_DICTIONARY,
R.string.add_to_personal_dictionary, R.string.add_to_personal_dictionary_summary
) {
SwitchPreference(it, false)
@ -216,7 +212,7 @@ fun createCorrectionPrefs(context: Context) = listOf(
@Preview
@Composable
private fun PreferencePreview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
Theme(true) {
Surface {
TextCorrectionScreen { }

View file

@ -29,13 +29,13 @@ import helium314.keyboard.latin.settings.Settings
import helium314.keyboard.latin.utils.defaultClipboardToolbarPref
import helium314.keyboard.latin.utils.defaultPinnedToolbarPref
import helium314.keyboard.latin.utils.defaultToolbarPref
import helium314.keyboard.settings.AllPrefs
import helium314.keyboard.settings.NonSettingsPrefs
import helium314.keyboard.settings.PrefDef
import helium314.keyboard.settings.SettingsContainer
import helium314.keyboard.settings.SettingsWithoutKey
import helium314.keyboard.settings.Setting
import helium314.keyboard.settings.preferences.Preference
import helium314.keyboard.settings.preferences.ReorderSwitchPreference
import helium314.keyboard.settings.SearchPrefScreen
import helium314.keyboard.settings.SettingsActivity2
import helium314.keyboard.settings.SettingsActivity
import helium314.keyboard.settings.preferences.SwitchPreference
import helium314.keyboard.settings.Theme
import helium314.keyboard.settings.dialogs.ToolbarKeysCustomizer
@ -49,7 +49,7 @@ fun ToolbarScreen(
Settings.PREF_TOOLBAR_KEYS,
Settings.PREF_PINNED_TOOLBAR_KEYS,
Settings.PREF_CLIPBOARD_TOOLBAR_KEYS,
NonSettingsPrefs.CUSTOM_KEY_CODES,
SettingsWithoutKey.CUSTOM_KEY_CODES,
Settings.PREF_QUICK_PIN_TOOLBAR_KEYS,
Settings.PREF_AUTO_SHOW_TOOLBAR,
Settings.PREF_AUTO_HIDE_TOOLBAR,
@ -62,17 +62,17 @@ fun ToolbarScreen(
)
}
fun createToolbarPrefs(context: Context) = listOf(
PrefDef(context, Settings.PREF_TOOLBAR_KEYS, R.string.toolbar_keys) {
fun createToolbarSettingss(context: Context) = listOf(
Setting(context, Settings.PREF_TOOLBAR_KEYS, R.string.toolbar_keys) {
ReorderSwitchPreference(it, defaultToolbarPref)
},
PrefDef(context, Settings.PREF_PINNED_TOOLBAR_KEYS, R.string.pinned_toolbar_keys) {
Setting(context, Settings.PREF_PINNED_TOOLBAR_KEYS, R.string.pinned_toolbar_keys) {
ReorderSwitchPreference(it, defaultPinnedToolbarPref)
},
PrefDef(context, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, R.string.clipboard_toolbar_keys) {
Setting(context, Settings.PREF_CLIPBOARD_TOOLBAR_KEYS, R.string.clipboard_toolbar_keys) {
ReorderSwitchPreference(it, defaultClipboardToolbarPref)
},
PrefDef(context, NonSettingsPrefs.CUSTOM_KEY_CODES, R.string.customize_toolbar_key_codes) {
Setting(context, SettingsWithoutKey.CUSTOM_KEY_CODES, R.string.customize_toolbar_key_codes) {
var showDialog by remember { mutableStateOf(false) }
Preference(
name = it.title,
@ -84,20 +84,20 @@ fun createToolbarPrefs(context: Context) = listOf(
onDismissRequest = { showDialog = false }
)
},
PrefDef(context, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS,
Setting(context, Settings.PREF_QUICK_PIN_TOOLBAR_KEYS,
R.string.quick_pin_toolbar_keys, R.string.quick_pin_toolbar_keys_summary)
{
SwitchPreference(it, false,) { keyboardNeedsReload = true }
},
PrefDef(context, Settings.PREF_AUTO_SHOW_TOOLBAR, R.string.auto_show_toolbar, R.string.auto_show_toolbar_summary)
Setting(context, Settings.PREF_AUTO_SHOW_TOOLBAR, R.string.auto_show_toolbar, R.string.auto_show_toolbar_summary)
{
SwitchPreference(it, false,)
},
PrefDef(context, Settings.PREF_AUTO_HIDE_TOOLBAR, R.string.auto_hide_toolbar, R.string.auto_hide_toolbar_summary)
Setting(context, Settings.PREF_AUTO_HIDE_TOOLBAR, R.string.auto_hide_toolbar, R.string.auto_hide_toolbar_summary)
{
SwitchPreference(it, false,)
},
PrefDef(context, Settings.PREF_VARIABLE_TOOLBAR_DIRECTION,
Setting(context, Settings.PREF_VARIABLE_TOOLBAR_DIRECTION,
R.string.var_toolbar_direction, R.string.var_toolbar_direction_summary)
{
SwitchPreference(it, true,)
@ -107,7 +107,7 @@ fun createToolbarPrefs(context: Context) = listOf(
@Preview
@Composable
private fun Preview() {
SettingsActivity2.allPrefs = AllPrefs(LocalContext.current)
SettingsActivity.settingsContainer = SettingsContainer(LocalContext.current)
KeyboardIconsSet.instance.loadIcons(LocalContext.current)
Theme(true) {
Surface {