Migrate “pref_enableFontSelection” to Preferences DataStore

This commit is contained in:
Patryk Michalik
2022-02-22 10:50:51 +01:00
parent 5cfd1b0bbb
commit acecefd12e
6 changed files with 68 additions and 33 deletions
+1 -1
View File
@@ -336,7 +336,7 @@ dependencies {
implementation 'dev.kdrag0n:colorkt:1.0.3'
implementation "io.coil-kt:coil-compose:1.4.0"
implementation 'me.xdrop:fuzzywuzzy:1.3.1'
implementation "com.patrykmichalik:preference-manager:1.0.1"
implementation "com.patrykmichalik:preference-manager:1.0.3"
implementation "androidx.datastore:datastore-preferences:1.0.0"
implementation "androidx.room:room-runtime:$room_version"
@@ -104,7 +104,6 @@ class PreferenceManager private constructor(private val context: Context) : Base
val recentsActionShare = BoolPref("pref_recentsActionShare", isOnePlusStock)
val recentsActionLens = BoolPref("pref_recentsActionLens", true)
val recentsActionClearAll = BoolPref("pref_clearAllAsAction", false)
val enableFontSelection = BoolPref("pref_enableFontSelection", true)
init {
sp.registerOnSharedPreferenceChangeListener(this)
@@ -23,12 +23,14 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import app.lawnchair.font.FontCache
import app.lawnchair.icons.shape.IconShape
import app.lawnchair.icons.shape.IconShapeManager
import app.lawnchair.theme.color.ColorOption
import com.android.launcher3.Utilities
import com.android.launcher3.util.MainThreadInitializedObject
import com.patrykmichalik.preferencemanager.PreferenceManager
import app.lawnchair.preferences.PreferenceManager as LawnchairPreferenceManager
class PreferenceManager2(private val context: Context) : PreferenceManager {
@@ -43,7 +45,7 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
val hotseatQsb = preference(
key = booleanPreferencesKey(name = "dock_search_bar"),
defaultValue = true,
onSet = reloadHelper::restart,
onSet = { reloadHelper.restart() },
)
val iconShape = preference(
@@ -62,7 +64,7 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
key = stringPreferencesKey(name = "accent_color"),
parse = ColorOption::fromString,
save = ColorOption::toString,
onSet = reloadHelper::recreate,
onSet = { reloadHelper.recreate() },
defaultValue = when {
Utilities.ATLEAST_S -> ColorOption.SystemAccent
Utilities.ATLEAST_O_MR1 -> ColorOption.WallpaperPrimary
@@ -78,7 +80,7 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
val roundedWidgets = preference(
key = booleanPreferencesKey(name = "rounded_widgets"),
defaultValue = true,
onSet = reloadHelper::reloadGrid,
onSet = { reloadHelper.reloadGrid() },
)
val showStatusBar = preference(
@@ -96,6 +98,17 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
defaultValue = false,
)
val enableFontSelection = preference(
key = booleanPreferencesKey(name = "enable_font_selection"),
defaultValue = true,
onSet = { newValue ->
if (!newValue) {
val fontCache = FontCache.INSTANCE.get(context)
LawnchairPreferenceManager.getInstance(context).workspaceFont.set(newValue = fontCache.uiText)
}
},
)
companion object {
private val Context.preferencesDataStore by preferencesDataStore(
name = "preferences",
@@ -31,7 +31,7 @@ class SharedPreferencesMigration(private val context: Context) {
"pref_iconShape" to "icon_shape", "pref_themedHotseatQsb" to "themed_hotseat_qsb",
"pref_accentColor2" to "accent_color", "hidden-app-set" to "hidden_apps",
"pref_showStatusBar" to "show_status_bar", "pref_showSysUiScrim" to "show_top_shadow",
"pref_hideAppSearchBar" to "hide_app_drawer_search_bar",
"pref_hideAppSearchBar" to "hide_app_drawer_search_bar", "pref_enableFontSelection" to "enable_font_selection",
)
fun produceMigration() = androidx.datastore.migrations.SharedPreferencesMigration(
@@ -2,17 +2,23 @@ package app.lawnchair.ui.preferences
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavGraphBuilder
import app.lawnchair.data.iconoverride.IconOverrideRepository
import app.lawnchair.font.FontCache
import app.lawnchair.preferences.getAdapter
import app.lawnchair.preferences.preferenceManager
import app.lawnchair.preferences2.PreferenceCollectorScope
import app.lawnchair.preferences2.preferenceManager2
import app.lawnchair.ui.preferences.components.PreferenceGroup
import app.lawnchair.ui.preferences.components.PreferenceLayout
import app.lawnchair.ui.preferences.components.SwitchPreference
import app.lawnchair.ui.preferences.components.SwitchPreference2
import app.lawnchair.util.ifNotNull
import com.android.launcher3.R
import com.patrykmichalik.preferencemanager.state
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -22,39 +28,53 @@ fun NavGraphBuilder.experimentalFeaturesGraph(route: String) {
preferenceGraph(route, { ExperimentalFeaturesPreferences() })
}
interface ExperimentalPreferenceCollectorScope : PreferenceCollectorScope {
val enableFontSelection: Boolean
}
@Composable
fun ExperimentalPreferenceCollector(content: @Composable ExperimentalPreferenceCollectorScope.() -> Unit) {
val preferenceManager = preferenceManager2()
val enableFontSelection by preferenceManager.enableFontSelection.state()
ifNotNull(enableFontSelection) {
object : ExperimentalPreferenceCollectorScope {
override val enableFontSelection = it[0] as Boolean
override val coroutineScope = rememberCoroutineScope()
override val preferenceManager = preferenceManager
}.content()
}
}
@Composable
@OptIn(ExperimentalAnimationApi::class)
fun ExperimentalFeaturesPreferences() {
val preferenceManager = preferenceManager()
val context = LocalContext.current
PreferenceLayout(label = stringResource(id = R.string.experimental_features_label)) {
PreferenceGroup(isFirstChild = true) {
SwitchPreference(
adapter = preferenceManager.enableFontSelection.getAdapter(),
label = stringResource(id = R.string.font_picker_label),
description = stringResource(id = R.string.font_picker_description),
onChange = {
if (!it) {
val fontCache = FontCache.INSTANCE.get(context)
preferenceManager.workspaceFont.set(newValue = fontCache.uiText)
}
}
)
SwitchPreference(
adapter = preferenceManager.enableIconSelection.getAdapter(),
label = stringResource(id = R.string.icon_picker_label),
description = stringResource(id = R.string.icon_picker_description),
onChange = {
if (!it) {
val iconOverrideRepository = IconOverrideRepository.INSTANCE.get(context)
ExperimentalPreferenceCollector {
PreferenceLayout(label = stringResource(id = R.string.experimental_features_label)) {
PreferenceGroup(isFirstChild = true) {
SwitchPreference2(
checked = enableFontSelection,
label = stringResource(id = R.string.font_picker_label),
description = stringResource(id = R.string.font_picker_description),
edit = { enableFontSelection.set(value = it) },
)
SwitchPreference(
adapter = preferenceManager.enableIconSelection.getAdapter(),
label = stringResource(id = R.string.icon_picker_label),
description = stringResource(id = R.string.icon_picker_description),
onChange = {
if (!it) {
val iconOverrideRepository = IconOverrideRepository.INSTANCE.get(context)
CoroutineScope(Dispatchers.IO).launch {
iconOverrideRepository.deleteAll()
CoroutineScope(Dispatchers.IO).launch {
iconOverrideRepository.deleteAll()
}
}
}
}
)
)
}
}
}
}
@@ -53,6 +53,7 @@ fun NavGraphBuilder.generalGraph(route: String) {
interface GeneralPreferenceCollectorScope : PreferenceCollectorScope {
val iconShape: IconShape
val accentColor: ColorOption
val enableFontSelection: Boolean
}
@Composable
@@ -60,10 +61,12 @@ fun GeneralPreferenceCollector(content: @Composable GeneralPreferenceCollectorSc
val preferenceManager = preferenceManager2()
val iconShape by preferenceManager.iconShape.state()
val accentColor by preferenceManager.accentColor.state()
ifNotNull(iconShape, accentColor) {
val enableFontSelection by preferenceManager.enableFontSelection.state()
ifNotNull(iconShape, accentColor, enableFontSelection) {
object : GeneralPreferenceCollectorScope {
override val iconShape = it[0] as IconShape
override val accentColor = it[1] as ColorOption
override val enableFontSelection = it[2] as Boolean
override val coroutineScope = rememberCoroutineScope()
override val preferenceManager = preferenceManager
}.content()
@@ -101,7 +104,7 @@ fun GeneralPreferences() {
value = iconShape,
edit = { iconShape.set(value = it) },
)
if (prefs.enableFontSelection.get()) {
if (enableFontSelection) {
FontPreference(
adapter = prefs.workspaceFont.getAdapter(),
label = stringResource(id = R.string.font_label),