Migrate “pref_enableIconSelection” to Preferences DataStore

This commit is contained in:
Patryk
2022-03-11 16:56:20 +01:00
parent 1eed597b48
commit 5eabcfa645
5 changed files with 27 additions and 18 deletions
@@ -106,6 +106,7 @@ fun CustomizeAppDialog(
val prefs = preferenceManager()
val preferenceManager2 = preferenceManager2()
val coroutineScope = rememberCoroutineScope()
val enableIconSelection by preferenceManager2.enableIconSelection.state()
val context = LocalContext.current
var title by remember { mutableStateOf("") }
@@ -141,7 +142,7 @@ fun CustomizeAppDialog(
title = title,
onTitleChange = { title = it },
defaultTitle = defaultTitle,
launchSelectIcon = if (prefs.enableIconSelection.get()) openIconPicker else null,
launchSelectIcon = if (enableIconSelection == true) openIconPicker else null,
) {
PreferenceGroup(
description = componentKey.componentName.flattenToString(),
@@ -74,7 +74,6 @@ class PreferenceManager private constructor(private val context: Context) : Base
val searchResultPixelTips = BoolPref("pref_searchResultPixelTips", false)
val searchResultSettings = BoolPref("pref_searchResultSettings", false)
val enableIconSelection = BoolPref("pref_enableIconSelection", false)
val showComponentName = BoolPref("pref_showComponentName", false)
val themedIcons = BoolPref("themed_icons", false)
val hotseatQsbCornerRadius = FloatPref("pref_hotseatQsbCornerRadius", 1F, recreate)
@@ -24,6 +24,7 @@ import androidx.datastore.preferences.core.floatPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import app.lawnchair.data.iconoverride.IconOverrideRepository
import app.lawnchair.font.FontCache
import app.lawnchair.icons.shape.IconShape
import app.lawnchair.icons.shape.IconShapeManager
@@ -32,6 +33,9 @@ import app.lawnchair.theme.color.ColorOption
import com.android.launcher3.Utilities
import com.android.launcher3.util.MainThreadInitializedObject
import com.patrykmichalik.preferencemanager.PreferenceManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import app.lawnchair.preferences.PreferenceManager as LawnchairPreferenceManager
class PreferenceManager2(private val context: Context) : PreferenceManager {
@@ -199,6 +203,19 @@ class PreferenceManager2(private val context: Context) : PreferenceManager {
onSet = { reloadHelper.recreate() },
)
val enableIconSelection = preference(
key = booleanPreferencesKey(name = "enable_icon_selection"),
defaultValue = false,
onSet = {
if (!it) {
val iconOverrideRepository = IconOverrideRepository.INSTANCE.get(context)
CoroutineScope(Dispatchers.IO).launch {
iconOverrideRepository.deleteAll()
}
}
}
)
companion object {
private val Context.preferencesDataStore by preferencesDataStore(
name = "preferences",
@@ -38,7 +38,7 @@ class SharedPreferencesMigration(private val context: Context) {
"pref_allAppsIconLabels" to "show_icon_labels_in_drawer", "pref_textSizeFactor" to "home_icon_label_size_factor",
"pref_allAppsTextSizeFactor" to "drawer_icon_label_size_factor", "pref_allAppsCellHeightMultiplier" to "drawer_cell_height_factor",
"pref_useFuzzySearch" to "enable_fuzzy_search", "pref_smartSpaceEnable" to "enable_smartspace",
"pref_enableMinusOne" to "enable_feed",
"pref_enableMinusOne" to "enable_feed", "pref_enableIconSelection" to "enable_icon_selection",
)
fun produceMigration() = androidx.datastore.migrations.SharedPreferencesMigration(
@@ -30,15 +30,18 @@ fun NavGraphBuilder.experimentalFeaturesGraph(route: String) {
interface ExperimentalPreferenceCollectorScope : PreferenceCollectorScope {
val enableFontSelection: Boolean
val enableIconSelection: Boolean
}
@Composable
fun ExperimentalPreferenceCollector(content: @Composable ExperimentalPreferenceCollectorScope.() -> Unit) {
val preferenceManager = preferenceManager2()
val enableFontSelection by preferenceManager.enableFontSelection.state()
ifNotNull(enableFontSelection) {
val enableIconSelection by preferenceManager.enableIconSelection.state()
ifNotNull(enableFontSelection, enableIconSelection) {
object : ExperimentalPreferenceCollectorScope {
override val enableFontSelection = it[0] as Boolean
override val enableIconSelection = it[1] as Boolean
override val coroutineScope = rememberCoroutineScope()
override val preferenceManager = preferenceManager
}.content()
@@ -48,9 +51,6 @@ fun ExperimentalPreferenceCollector(content: @Composable ExperimentalPreferenceC
@Composable
@OptIn(ExperimentalAnimationApi::class)
fun ExperimentalFeaturesPreferences() {
val preferenceManager = preferenceManager()
val context = LocalContext.current
ExperimentalPreferenceCollector {
PreferenceLayout(label = stringResource(id = R.string.experimental_features_label)) {
PreferenceGroup(isFirstChild = true) {
@@ -60,19 +60,11 @@ fun ExperimentalFeaturesPreferences() {
description = stringResource(id = R.string.font_picker_description),
edit = { enableFontSelection.set(value = it) },
)
SwitchPreference(
adapter = preferenceManager.enableIconSelection.getAdapter(),
SwitchPreference2(
checked = enableIconSelection,
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()
}
}
}
edit = { enableIconSelection.set(value = it) },
)
}
}