Skip to content

Commit 4edb29a

Browse files
authored
Refactor Other preferences registration logic (#1379)
Made sure the logic for displaying experimental settings runs regardless of the visibility of the toggle
1 parent ac09bfe commit 4edb29a

File tree

2 files changed

+51
-44
lines changed

2 files changed

+51
-44
lines changed

app/src/processing/app/ui/PDEPreferences.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class PDEPreferences {
9999
Interface.register()
100100
Coding.register()
101101
Sketches.register()
102-
Other.register(panes)
102+
Other.register()
103103
}
104104

105105
/**
@@ -111,6 +111,8 @@ class PDEPreferences {
111111
val locale = LocalLocale.current
112112
var preferencesQuery by remember { mutableStateOf("") }
113113

114+
Other.handleOtherPreferences(panes)
115+
114116
/**
115117
* Filter panes based on the search query.
116118
*/

app/src/processing/app/ui/preferences/Other.kt

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.material.icons.filled.Lightbulb
66
import androidx.compose.material3.Icon
77
import androidx.compose.material3.OutlinedTextField
88
import androidx.compose.material3.Switch
9+
import androidx.compose.runtime.Composable
910
import androidx.compose.runtime.DisposableEffect
1011
import androidx.compose.ui.Modifier
1112
import androidx.compose.ui.unit.dp
@@ -27,7 +28,7 @@ class Other {
2728
after = sketches
2829
)
2930

30-
fun register(panes: PDEPreferencePanes) {
31+
fun register() {
3132
PDEPreferences.register(
3233
PDEPreference(
3334
key = "preferences.show_other",
@@ -41,57 +42,61 @@ class Other {
4142
setPreference(it.toString())
4243
}
4344
)
44-
if (!showOther) {
45-
return@PDEPreference
46-
}
47-
val prefs = LocalPreferences.current
48-
val locale = LocalLocale.current
49-
DisposableEffect(Unit) {
50-
// add all the other options to the same group as the current one
51-
val group =
52-
panes[other]?.find { group -> group.any { preference -> preference.key == "preferences.show_other" } } as? MutableList<PDEPreference>
45+
}
46+
)
47+
)
48+
}
5349

54-
val existing = panes.values.flatten().flatten().map { preference -> preference.key }
55-
val keys = prefs.keys.mapNotNull { it as? String }.filter { it !in existing }.sorted()
50+
@Composable
51+
fun handleOtherPreferences(panes: PDEPreferencePanes) {
52+
// This function can be used to handle any specific logic related to other preferences if needed
53+
val prefs = LocalPreferences.current
54+
val locale = LocalLocale.current
55+
if (prefs["preferences.show_other"]?.toBoolean() != true) {
56+
return
57+
}
58+
DisposableEffect(panes) {
59+
// add all the other options to the same group as the current one
60+
val group =
61+
panes[other]?.find { group -> group.any { preference -> preference.key == "preferences.show_other" } } as? MutableList<PDEPreference>
5662

57-
for (prefKey in keys) {
58-
val descriptionKey = "preferences.$prefKey"
59-
val preference = PDEPreference(
60-
key = prefKey,
61-
descriptionKey = if (locale.containsKey(descriptionKey)) descriptionKey else prefKey,
62-
pane = other,
63-
control = { preference, updatePreference ->
64-
if (preference?.toBooleanStrictOrNull() != null) {
65-
Switch(
66-
checked = preference.toBoolean(),
67-
onCheckedChange = {
68-
updatePreference(it.toString())
69-
}
70-
)
71-
return@PDEPreference
72-
}
63+
val existing = panes.values.flatten().flatten().map { preference -> preference.key }
64+
val keys = prefs.keys.mapNotNull { it as? String }.filter { it !in existing }.sorted()
7365

74-
OutlinedTextField(
75-
modifier = Modifier.widthIn(max = 300.dp),
76-
value = preference ?: "",
77-
singleLine = true,
78-
onValueChange = {
79-
updatePreference(it)
80-
}
81-
)
66+
for (prefKey in keys) {
67+
val descriptionKey = "preferences.$prefKey"
68+
val preference = PDEPreference(
69+
key = prefKey,
70+
descriptionKey = if (locale.containsKey(descriptionKey)) descriptionKey else prefKey,
71+
pane = other,
72+
control = { preference, updatePreference ->
73+
if (preference?.toBooleanStrictOrNull() != null) {
74+
Switch(
75+
checked = preference.toBoolean(),
76+
onCheckedChange = {
77+
updatePreference(it.toString())
8278
}
8379
)
84-
group?.add(preference)
80+
return@PDEPreference
8581
}
86-
onDispose {
87-
group?.apply {
88-
removeIf { it.key != "preferences.show_other" }
82+
83+
OutlinedTextField(
84+
modifier = Modifier.widthIn(max = 300.dp),
85+
value = preference ?: "",
86+
onValueChange = {
87+
updatePreference(it)
8988
}
90-
}
89+
)
9190
}
91+
)
92+
group?.add(preference)
93+
}
94+
onDispose {
95+
group?.apply {
96+
removeIf { it.key != "preferences.show_other" }
9297
}
93-
)
94-
)
98+
}
99+
}
95100
}
96101
}
97102
}

0 commit comments

Comments
 (0)