Update: Manage dependencies decentralized

This commit is contained in:
machiav3lli 2022-09-17 22:08:35 +02:00
parent 528bed9dec
commit 30fb0d2dd2
3 changed files with 46 additions and 27 deletions

View File

@ -34,6 +34,7 @@ import androidx.compose.ui.unit.sp
import com.machiav3lli.fdroid.content.BooleanPrefsMeta import com.machiav3lli.fdroid.content.BooleanPrefsMeta
import com.machiav3lli.fdroid.content.NonBooleanPrefsMeta import com.machiav3lli.fdroid.content.NonBooleanPrefsMeta
import com.machiav3lli.fdroid.content.Preferences import com.machiav3lli.fdroid.content.Preferences
import com.machiav3lli.fdroid.content.PrefsDependencies
import com.machiav3lli.fdroid.content.PrefsEntries import com.machiav3lli.fdroid.content.PrefsEntries
import com.machiav3lli.fdroid.ui.compose.utils.addIf import com.machiav3lli.fdroid.ui.compose.utils.addIf
import com.machiav3lli.fdroid.utility.Utils import com.machiav3lli.fdroid.utility.Utils
@ -124,11 +125,25 @@ fun SwitchPreference(
prefKey: Preferences.Key<Boolean>, prefKey: Preferences.Key<Boolean>,
index: Int = 0, index: Int = 0,
groupSize: Int = 1, groupSize: Int = 1,
enabled: Boolean = true,
onCheckedChange: ((Boolean) -> Unit) = {}, onCheckedChange: ((Boolean) -> Unit) = {},
) { ) {
val context = LocalContext.current val context = LocalContext.current
val (checked, check) = remember(Preferences[prefKey]) { mutableStateOf(Preferences[prefKey]) } val (checked, check) = remember(Preferences[prefKey]) { mutableStateOf(Preferences[prefKey]) }
val dependency = PrefsDependencies[prefKey]
var isEnabled by remember {
mutableStateOf(dependency?.let { Preferences[dependency] != it.default.value } ?: true)
}
SideEffect {
CoroutineScope(Dispatchers.Default).launch {
Preferences.subject.collect {
when (it) {
dependency -> isEnabled = Preferences[it] != it.default.value
else -> {}
}
}
}
}
BasePreference( BasePreference(
modifier = modifier, modifier = modifier,
@ -136,7 +151,7 @@ fun SwitchPreference(
summaryId = BooleanPrefsMeta[prefKey]?.second ?: -1, summaryId = BooleanPrefsMeta[prefKey]?.second ?: -1,
index = index, index = index,
groupSize = groupSize, groupSize = groupSize,
isEnabled = enabled, isEnabled = isEnabled,
onClick = { onClick = {
onCheckedChange(!checked) onCheckedChange(!checked)
Preferences[prefKey] = !checked Preferences[prefKey] = !checked
@ -152,22 +167,36 @@ fun SwitchPreference(
Preferences[prefKey] = it Preferences[prefKey] = it
check(it) check(it)
}, },
enabled = enabled, enabled = isEnabled,
) )
} }
) )
} }
@Composable @Composable
fun StringPreference( fun LanguagePreference(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
prefKey: Preferences.Key<String>, prefKey: Preferences.Key<String>,
index: Int = 1, index: Int = 1,
groupSize: Int = 1, groupSize: Int = 1,
isEnabled: Boolean = true,
onClick: (() -> Unit) = {}, onClick: (() -> Unit) = {},
) { ) {
val context = LocalContext.current val context = LocalContext.current
val dependency = PrefsDependencies[prefKey]
var isEnabled by remember {
mutableStateOf(dependency?.let { Preferences[dependency] != it.default.value } ?: true)
}
SideEffect {
CoroutineScope(Dispatchers.Default).launch {
Preferences.subject.collect {
when (it) {
dependency -> isEnabled = Preferences[it] != it.default.value
else -> {}
}
}
}
}
BasePreference( BasePreference(
modifier = modifier, modifier = modifier,
@ -186,9 +215,12 @@ fun EnumPreference(
prefKey: Preferences.Key<Preferences.Enumeration<*>>, prefKey: Preferences.Key<Preferences.Enumeration<*>>,
index: Int = 1, index: Int = 1,
groupSize: Int = 1, groupSize: Int = 1,
isEnabled: Boolean = true,
onClick: (() -> Unit) = {}, onClick: (() -> Unit) = {},
) { ) {
val dependency = PrefsDependencies[prefKey]
var isEnabled by remember {
mutableStateOf(dependency?.let { Preferences[dependency] != it.default.value } ?: true)
}
var prefValue by remember { var prefValue by remember {
mutableStateOf(Preferences[prefKey]) mutableStateOf(Preferences[prefKey])
} }
@ -197,6 +229,7 @@ fun EnumPreference(
Preferences.subject.collect { Preferences.subject.collect {
when (it) { when (it) {
prefKey -> prefValue = Preferences[prefKey] prefKey -> prefValue = Preferences[prefKey]
dependency -> isEnabled = Preferences[it] != it.default.value
else -> {} else -> {}
} }
} }
@ -221,9 +254,12 @@ fun IntPreference(
prefKey: Preferences.Key<Int>, prefKey: Preferences.Key<Int>,
index: Int = 1, index: Int = 1,
groupSize: Int = 1, groupSize: Int = 1,
isEnabled: Boolean = true,
onClick: (() -> Unit) = {}, onClick: (() -> Unit) = {},
) { ) {
val dependency = PrefsDependencies[prefKey]
var isEnabled by remember {
mutableStateOf(dependency?.let { Preferences[dependency] != it.default.value } ?: true)
}
var prefValue by remember { var prefValue by remember {
mutableStateOf(Preferences[prefKey]) mutableStateOf(Preferences[prefKey])
} }
@ -232,6 +268,7 @@ fun IntPreference(
Preferences.subject.collect { Preferences.subject.collect {
when (it) { when (it) {
prefKey -> prefValue = Preferences[prefKey] prefKey -> prefValue = Preferences[prefKey]
dependency -> isEnabled = Preferences[it] != it.default.value
else -> {} else -> {}
} }
} }

View File

@ -1,15 +1,12 @@
package com.machiav3lli.fdroid.ui.compose.components.prefs package com.machiav3lli.fdroid.ui.compose.components.prefs
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.machiav3lli.fdroid.content.Preferences import com.machiav3lli.fdroid.content.Preferences
import com.machiav3lli.fdroid.content.PrefsDependencies
@Composable @Composable
fun PrefsBuilder( fun PrefsBuilder(
prefKey: Preferences.Key<*>, prefKey: Preferences.Key<*>,
onDialogPref: (Preferences.Key<*>) -> Unit, onDialogPref: (Preferences.Key<*>) -> Unit,
enabledSetState: SnapshotStateList<Preferences.Key<*>>,
index: Int, index: Int,
size: Int size: Int
) { ) {
@ -18,14 +15,8 @@ fun PrefsBuilder(
prefKey = prefKey as Preferences.Key<Boolean>, prefKey = prefKey as Preferences.Key<Boolean>,
index = index, index = index,
groupSize = size, groupSize = size,
enabled = enabledSetState.contains(prefKey) )
) { prefKey.default is Preferences.Value.StringValue -> LanguagePreference(
val dependents =
PrefsDependencies.entries.filter { it.value == prefKey }.map { it.key }.toSet()
if (it) enabledSetState.addAll(dependents)
else enabledSetState.removeAll(dependents)
}
prefKey.default is Preferences.Value.StringValue -> StringPreference(
prefKey = prefKey as Preferences.Key<String>, prefKey = prefKey as Preferences.Key<String>,
index = index, index = index,
groupSize = size, groupSize = size,

View File

@ -13,15 +13,12 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import com.machiav3lli.fdroid.content.Preferences import com.machiav3lli.fdroid.content.Preferences
import com.machiav3lli.fdroid.content.PrefsDependencies
@Composable @Composable
fun PreferenceGroup( fun PreferenceGroup(
@ -49,11 +46,6 @@ fun PreferenceGroup(
onPrefDialog: (Preferences.Key<*>) -> Unit, onPrefDialog: (Preferences.Key<*>) -> Unit,
) { ) {
val size = keys.size val size = keys.size
val enabledList = remember() {
mutableStateListOf(
*keys.filter { PrefsDependencies[it]?.let { Preferences[it] } ?: true }.toTypedArray()
)
}
PreferenceGroup( PreferenceGroup(
modifier = modifier, modifier = modifier,
@ -63,7 +55,6 @@ fun PreferenceGroup(
PrefsBuilder( PrefsBuilder(
item, item,
onPrefDialog, onPrefDialog,
enabledList,
index, index,
size size
) )