Skip to content

Commit

Permalink
Fix: Make pref lists more robust (reduce unneeded recompositions)
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Jul 23, 2024
1 parent 7946cc6 commit ee09e77
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ import com.machiav3lli.backup.ui.item.Pref
import com.machiav3lli.backup.ui.item.StringPref
import com.machiav3lli.backup.utils.SystemUtils.numCores
import com.machiav3lli.backup.utils.sortFilterModel
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList


@Composable
fun DevPrefGroups() {
val devUserOptions = Pref.prefGroups["dev-adv"] ?: listOf()
val devFileOptions = Pref.prefGroups["dev-file"] ?: listOf()
val devLogOptions = Pref.prefGroups["dev-log"] ?: listOf()
val devTraceOptions = Pref.prefGroups["dev-trace"] ?: listOf()
val devHackOptions = Pref.prefGroups["dev-hack"] ?: listOf()
val devAltOptions = Pref.prefGroups["dev-alt"] ?: listOf()
val devNewOptions = Pref.prefGroups["dev-new"] ?: listOf()
val devFakeOptions = Pref.prefGroups["dev-fake"] ?: listOf()
val devUserOptions = Pref.prefGroups["dev-adv"]?.toPersistentList() ?: persistentListOf()
val devFileOptions = Pref.prefGroups["dev-file"]?.toPersistentList() ?: persistentListOf()
val devLogOptions = Pref.prefGroups["dev-log"]?.toPersistentList() ?: persistentListOf()
val devTraceOptions = Pref.prefGroups["dev-trace"]?.toPersistentList() ?: persistentListOf()
val devHackOptions = Pref.prefGroups["dev-hack"]?.toPersistentList() ?: persistentListOf()
val devAltOptions = Pref.prefGroups["dev-alt"]?.toPersistentList() ?: persistentListOf()
val devNewOptions = Pref.prefGroups["dev-new"]?.toPersistentList() ?: persistentListOf()
val devFakeOptions = Pref.prefGroups["dev-fake"]?.toPersistentList() ?: persistentListOf()

Column(
verticalArrangement = Arrangement.spacedBy(4.dp)
Expand All @@ -80,7 +82,7 @@ fun AdvancedPrefsPage() {
val context = LocalContext.current
val (expanded, expand) = remember { mutableStateOf(false) }

val prefs = Pref.prefGroups["adv"] ?: listOf()
val prefs = Pref.prefGroups["adv"]?.toPersistentList() ?: persistentListOf()

BusyBackground(
modifier = Modifier.fillMaxSize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ import com.machiav3lli.backup.ui.item.ListPref
import com.machiav3lli.backup.ui.item.PasswordPref
import com.machiav3lli.backup.ui.item.Pref
import com.machiav3lli.backup.ui.item.StringPref
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList

@Composable
fun ServicePrefsPage() {
Expand All @@ -78,12 +80,12 @@ fun ServicePrefsPage() {
BaseDialog(openDialogCustom = openDialog) {
when (dialogsPref) { //TODO hg42 encapsulate in pref

is ListPref -> ListPrefDialogUI(
is ListPref -> ListPrefDialogUI(
pref = dialogsPref as ListPref,
openDialogCustom = openDialog,
)

is EnumPref -> EnumPrefDialogUI(
is EnumPref -> EnumPrefDialogUI(
pref = dialogsPref as EnumPref,
openDialogCustom = openDialog
)
Expand All @@ -105,9 +107,9 @@ fun ServicePrefsPage() {
}

fun LazyListScope.ServicePrefGroups(onPrefDialog: (Pref) -> Unit) {
val generalServicePrefs = Pref.prefGroups["srv"] ?: listOf()
val backupServicePrefs = Pref.prefGroups["srv-bkp"] ?: listOf()
val restoreServicePrefs = Pref.prefGroups["srv-rst"] ?: listOf()
val generalServicePrefs = Pref.prefGroups["srv"]?.toPersistentList() ?: persistentListOf()
val backupServicePrefs = Pref.prefGroups["srv-bkp"]?.toPersistentList() ?: persistentListOf()
val restoreServicePrefs = Pref.prefGroups["srv-rst"]?.toPersistentList() ?: persistentListOf()

item {
PrefsGroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ import com.machiav3lli.backup.utils.recreateActivities
import com.machiav3lli.backup.utils.restartApp
import com.machiav3lli.backup.utils.setBackupDir
import com.machiav3lli.backup.utils.setCustomTheme
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.PersistentList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import timber.log.Timber

@Composable
Expand All @@ -77,7 +81,7 @@ fun UserPrefsPage() {
var dialogsPref by remember { mutableStateOf<Pref?>(null) }
var backupDir by remember { mutableStateOf(backupDirConfigured) } //TODO hg42 remember ???

val prefs = Pref.prefGroups["user"] ?: listOf()
val prefs = Pref.prefGroups["user"]?.toPersistentList() ?: persistentListOf()

val launcher =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import com.machiav3lli.backup.ICON_SIZE_MEDIUM
import com.machiav3lli.backup.traceDebug
import com.machiav3lli.backup.ui.compose.item.ExpandableBlock
import com.machiav3lli.backup.ui.item.Pref
import kotlinx.collections.immutable.ImmutableList

@Composable
fun PrefsGroupCollapsed(prefs: List<Pref>, heading: String) {
fun PrefsGroupCollapsed(prefs: ImmutableList<Pref>, heading: String) {
if (prefs.isNotEmpty())
ExpandableBlock(
heading = heading,
Expand Down Expand Up @@ -66,7 +67,7 @@ fun PrefsGroup(
fun PrefsGroup(
modifier: Modifier = Modifier,
heading: String? = null,
prefs: List<Pref>,
prefs: ImmutableList<Pref>,
onPrefDialog: (Pref) -> Unit = {},
) {
val size = prefs.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.machiav3lli.backup.preferences.ui.PrefsGroup
import com.machiav3lli.backup.ui.item.Pref
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList

@Composable
fun BatchPrefsSheet(backupBoolean: Boolean) {
val prefs = Pref.prefGroups[if (backupBoolean) "srv-bkp" else "srv-rst"] ?: listOf()
val prefs = Pref.prefGroups[if (backupBoolean) "srv-bkp" else "srv-rst"]?.toPersistentList()
?: persistentListOf()

LazyColumn(
modifier = Modifier.fillMaxSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ import com.machiav3lli.backup.utils.TraceUtils.trace
import com.machiav3lli.backup.utils.getBackupRoot
import com.machiav3lli.backup.utils.recreateActivities
import com.machiav3lli.backup.viewmodels.LogViewModel
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -204,7 +206,7 @@ fun DevSettingsTab() {
.filter {
it.key.contains(search, ignoreCase = true)
&& it.group !in listOf("persist", "kill")
}
}.toPersistentList()
)
}
}
Expand Down Expand Up @@ -412,7 +414,7 @@ fun DevToolsTab() {

val scroll = rememberScrollState(0)

val prefs = Pref.prefGroups["dev-tool"] ?: listOf()
val prefs = Pref.prefGroups["dev-tool"]?.toPersistentList() ?: persistentListOf()

Column(
modifier = Modifier
Expand Down Expand Up @@ -469,7 +471,7 @@ val pref_afterSupport = LaunchPref(
fun DevSupportTab() {
val scroll = rememberScrollState(0)

val prefs = Pref.prefGroups["dev-support"] ?: listOf()
val prefs = Pref.prefGroups["dev-support"]?.toPersistentList() ?: persistentListOf()

Column(
modifier = Modifier
Expand Down

0 comments on commit ee09e77

Please sign in to comment.