2025-01-25 22:07:56 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
package helium314.keyboard.settings
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.ContextWrapper
|
2025-01-28 22:17:23 +01:00
|
|
|
import android.content.SharedPreferences
|
2025-01-25 22:07:56 +01:00
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
import androidx.annotation.StringRes
|
|
|
|
import androidx.compose.runtime.Composable
|
2025-01-28 22:17:23 +01:00
|
|
|
import helium314.keyboard.latin.utils.DeviceProtectedUtils
|
2025-01-26 18:25:43 +01:00
|
|
|
import helium314.keyboard.settings.screens.createAboutPrefs
|
|
|
|
import helium314.keyboard.settings.screens.createCorrectionPrefs
|
2025-01-26 21:16:26 +01:00
|
|
|
import helium314.keyboard.settings.screens.createPreferencesPrefs
|
2025-01-28 16:14:42 +01:00
|
|
|
import helium314.keyboard.settings.screens.createToolbarPrefs
|
2025-01-25 22:07:56 +01:00
|
|
|
|
|
|
|
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 { if (it.title.lowercase().startsWith(term)) results.add(it) }
|
|
|
|
list.forEach { if (it.title.lowercase().split(' ').any { it.startsWith(term) }) results.add(it) }
|
|
|
|
list.forEach {
|
|
|
|
if (it.description?.lowercase()?.split(' ')?.any { it.startsWith(term) } == true)
|
|
|
|
results.add(it)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-26 21:16:26 +01:00
|
|
|
private fun createPrefDefs(context: Context) = createAboutPrefs(context) +
|
2025-01-28 16:14:42 +01:00
|
|
|
createCorrectionPrefs(context) + createPreferencesPrefs(context) + createToolbarPrefs(context)
|
2025-01-25 22:07:56 +01:00
|
|
|
|
|
|
|
// todo: move somewhere else
|
|
|
|
fun Context.getActivity(): ComponentActivity? {
|
|
|
|
val componentActivity = when (this) {
|
|
|
|
is ComponentActivity -> this
|
|
|
|
is ContextWrapper -> baseContext.getActivity()
|
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
return componentActivity
|
|
|
|
}
|
|
|
|
|
2025-01-28 22:17:23 +01:00
|
|
|
fun Context.prefs(): SharedPreferences = DeviceProtectedUtils.getSharedPreferences(this)
|
|
|
|
|
2025-01-25 22:07:56 +01:00
|
|
|
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"
|
2025-01-29 05:51:41 +01:00
|
|
|
const val CUSTOM_KEY_CODES = "customize_key_codes"
|
2025-01-25 22:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@JvmField
|
|
|
|
var themeChanged = false
|