Skip to content

Commit bf67c20

Browse files
committed
Fix background setting
1 parent 0654451 commit bf67c20

4 files changed

Lines changed: 186 additions & 146 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ android {
2020
applicationId = "io.bloco.snowflake"
2121
minSdk = 26
2222
targetSdk = 37
23-
versionCode = 25 // Increment by 5 to account for ABI split
24-
versionName = "1.5"
23+
versionCode = 30 // Increment by 5 to account for ABI split
24+
versionName = "1.6"
2525

2626
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2727
androidResources.localeFilters += listOf("en", "fr", "ja", "pt")

app/src/main/java/io/bloco/snowflake/Dependencies.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class Dependencies(
125125
SettingsViewModel(
126126
getAppConfig = appDataStore::appConfig,
127127
getCapacity = appDataStore::capacity,
128+
setBackground = appDataStore::setBackground,
128129
setUnmeteredOnly = appDataStore::setUnmeteredOnly,
129130
setChargingOnly = appDataStore::setChargingOnly,
130131
setCapacity = appDataStore::setCapacity,

app/src/main/java/io/bloco/snowflake/ui/settings/SettingsScreen.kt

Lines changed: 173 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -62,154 +62,183 @@ fun SettingsScreen(
6262
.padding(WindowInsets.navigationBars.asPaddingValues())
6363
.padding(top = 16.dp, bottom = 32.dp),
6464
) {
65-
val background = state.config?.background == true
66-
Row(
67-
verticalAlignment = Alignment.CenterVertically,
68-
modifier =
69-
Modifier
70-
.selectable(
71-
selected = background,
72-
role = Role.Switch,
73-
onClick = { onEvent(SettingsViewModel.Event.UnmeteredOnlyChange(!background)) },
74-
).padding(horizontal = 16.dp, vertical = 8.dp),
75-
) {
76-
Text(
77-
text = stringResource(R.string.settings_background),
78-
style = MaterialTheme.typography.titleMediumEmphasized,
79-
modifier = Modifier.weight(1f),
80-
)
81-
Switch(
82-
checked = background,
83-
onCheckedChange = null,
84-
)
85-
}
86-
Text(
87-
text = stringResource(R.string.settings_background_context),
88-
style = MaterialTheme.typography.labelLarge,
89-
modifier =
90-
Modifier
91-
.fillMaxWidth()
92-
.padding(horizontal = 16.dp)
93-
.padding(bottom = 28.dp),
94-
)
65+
BackgroundSetting(state, onEvent)
66+
UnmeteredSetting(state, onEvent)
67+
ChargingSetting(state, onEvent)
68+
CapacitySetting(state, onEvent)
69+
}
70+
}
71+
}
9572

96-
val unmeteredOnly = state.config?.unmeteredOnly == true
97-
Row(
98-
verticalAlignment = Alignment.CenterVertically,
99-
modifier =
100-
Modifier
101-
.selectable(
102-
selected = unmeteredOnly,
103-
role = Role.Switch,
104-
onClick = { onEvent(SettingsViewModel.Event.UnmeteredOnlyChange(!unmeteredOnly)) },
105-
).padding(horizontal = 16.dp, vertical = 8.dp),
106-
) {
107-
Text(
108-
text = stringResource(R.string.settings_unmetered),
109-
style = MaterialTheme.typography.titleMediumEmphasized,
110-
modifier = Modifier.weight(1f),
111-
)
112-
Switch(
113-
checked = unmeteredOnly,
114-
onCheckedChange = null,
115-
)
116-
}
117-
Text(
118-
text = stringResource(R.string.settings_unmetered_context),
119-
style = MaterialTheme.typography.labelLarge,
120-
modifier =
121-
Modifier
122-
.fillMaxWidth()
123-
.padding(horizontal = 16.dp)
124-
.padding(bottom = 28.dp),
125-
)
73+
@Composable
74+
private fun BackgroundSetting(
75+
state: SettingsViewModel.State,
76+
onEvent: (SettingsViewModel.Event) -> Unit,
77+
) {
78+
val background = state.config?.background == true
79+
Row(
80+
verticalAlignment = Alignment.CenterVertically,
81+
modifier =
82+
Modifier
83+
.selectable(
84+
selected = background,
85+
role = Role.Switch,
86+
onClick = { onEvent(SettingsViewModel.Event.BackgroundChange(!background)) },
87+
).padding(horizontal = 16.dp, vertical = 8.dp),
88+
) {
89+
Text(
90+
text = stringResource(R.string.settings_background),
91+
style = MaterialTheme.typography.titleMediumEmphasized,
92+
modifier = Modifier.weight(1f),
93+
)
94+
Switch(
95+
checked = background,
96+
onCheckedChange = null,
97+
)
98+
}
99+
Text(
100+
text = stringResource(R.string.settings_background_context),
101+
style = MaterialTheme.typography.labelLarge,
102+
modifier =
103+
Modifier
104+
.fillMaxWidth()
105+
.padding(horizontal = 16.dp)
106+
.padding(bottom = 28.dp),
107+
)
108+
}
109+
110+
@Composable
111+
private fun UnmeteredSetting(
112+
state: SettingsViewModel.State,
113+
onEvent: (SettingsViewModel.Event) -> Unit,
114+
) {
115+
val unmeteredOnly = state.config?.unmeteredOnly == true
116+
Row(
117+
verticalAlignment = Alignment.CenterVertically,
118+
modifier =
119+
Modifier
120+
.selectable(
121+
selected = unmeteredOnly,
122+
role = Role.Switch,
123+
onClick = { onEvent(SettingsViewModel.Event.UnmeteredOnlyChange(!unmeteredOnly)) },
124+
).padding(horizontal = 16.dp, vertical = 8.dp),
125+
) {
126+
Text(
127+
text = stringResource(R.string.settings_unmetered),
128+
style = MaterialTheme.typography.titleMediumEmphasized,
129+
modifier = Modifier.weight(1f),
130+
)
131+
Switch(
132+
checked = unmeteredOnly,
133+
onCheckedChange = null,
134+
)
135+
}
136+
Text(
137+
text = stringResource(R.string.settings_unmetered_context),
138+
style = MaterialTheme.typography.labelLarge,
139+
modifier =
140+
Modifier
141+
.fillMaxWidth()
142+
.padding(horizontal = 16.dp)
143+
.padding(bottom = 28.dp),
144+
)
145+
}
126146

127-
val chargingOnly = state.config?.chargingOnly == true
128-
Row(
129-
verticalAlignment = Alignment.CenterVertically,
130-
modifier =
131-
Modifier
132-
.selectable(
133-
selected = chargingOnly,
134-
role = Role.Switch,
135-
onClick = { onEvent(SettingsViewModel.Event.ChargingOnlyChange(!chargingOnly)) },
136-
).padding(horizontal = 16.dp, vertical = 8.dp),
137-
) {
138-
Text(
139-
text = stringResource(R.string.settings_charging),
140-
style = MaterialTheme.typography.titleMediumEmphasized,
141-
modifier = Modifier.weight(1f),
142-
)
143-
Switch(
144-
checked = chargingOnly,
145-
onCheckedChange = null,
146-
)
147-
}
148-
Text(
149-
text = stringResource(R.string.settings_charging_context),
150-
style = MaterialTheme.typography.labelLarge,
151-
modifier =
152-
Modifier
153-
.fillMaxWidth()
154-
.padding(horizontal = 16.dp)
155-
.padding(bottom = 28.dp),
156-
)
147+
@Composable
148+
private fun ChargingSetting(
149+
state: SettingsViewModel.State,
150+
onEvent: (SettingsViewModel.Event) -> Unit,
151+
) {
152+
val chargingOnly = state.config?.chargingOnly == true
153+
Row(
154+
verticalAlignment = Alignment.CenterVertically,
155+
modifier =
156+
Modifier
157+
.selectable(
158+
selected = chargingOnly,
159+
role = Role.Switch,
160+
onClick = { onEvent(SettingsViewModel.Event.ChargingOnlyChange(!chargingOnly)) },
161+
).padding(horizontal = 16.dp, vertical = 8.dp),
162+
) {
163+
Text(
164+
text = stringResource(R.string.settings_charging),
165+
style = MaterialTheme.typography.titleMediumEmphasized,
166+
modifier = Modifier.weight(1f),
167+
)
168+
Switch(
169+
checked = chargingOnly,
170+
onCheckedChange = null,
171+
)
172+
}
173+
Text(
174+
text = stringResource(R.string.settings_charging_context),
175+
style = MaterialTheme.typography.labelLarge,
176+
modifier =
177+
Modifier
178+
.fillMaxWidth()
179+
.padding(horizontal = 16.dp)
180+
.padding(bottom = 28.dp),
181+
)
182+
}
157183

158-
Text(
159-
text = stringResource(R.string.settings_capacity),
160-
style = MaterialTheme.typography.titleMediumEmphasized,
161-
modifier =
162-
Modifier
163-
.padding(horizontal = 16.dp, vertical = 8.dp),
164-
)
165-
Slider(
166-
value =
167-
CAPACITY_RANGE_VALUES.entries
168-
.firstOrNull { it.value == state.capacity }
169-
?.key
170-
?: CAPACITY_RANGE_VALUES.keys.first(),
171-
onValueChange = {
172-
val cap = CAPACITY_RANGE_VALUES[it] ?: CAPACITY_RANGE_VALUES.values.first()
173-
onEvent(SettingsViewModel.Event.CapacityChange(cap))
174-
},
175-
valueRange = CAPACITY_RANGE_VALUES.keys.let { it.first()..it.last() },
176-
steps = CAPACITY_RANGE_VALUES.size - 2,
177-
modifier =
178-
Modifier
179-
.padding(horizontal = 16.dp)
180-
.padding(bottom = 8.dp),
181-
)
182-
Text(
183-
text =
184-
when (state.capacity) {
185-
is Capacity.Specific ->
186-
pluralStringResource(
187-
R.plurals.settings_capacity_specific,
188-
state.capacity.value.toInt(),
189-
state.capacity.value,
190-
)
184+
@Composable
185+
private fun CapacitySetting(
186+
state: SettingsViewModel.State,
187+
onEvent: (SettingsViewModel.Event) -> Unit,
188+
) {
189+
Text(
190+
text = stringResource(R.string.settings_capacity),
191+
style = MaterialTheme.typography.titleMediumEmphasized,
192+
modifier =
193+
Modifier
194+
.padding(horizontal = 16.dp, vertical = 8.dp),
195+
)
196+
Slider(
197+
value =
198+
CAPACITY_RANGE_VALUES.entries
199+
.firstOrNull { it.value == state.capacity }
200+
?.key
201+
?: CAPACITY_RANGE_VALUES.keys.first(),
202+
onValueChange = {
203+
val cap = CAPACITY_RANGE_VALUES[it] ?: CAPACITY_RANGE_VALUES.values.first()
204+
onEvent(SettingsViewModel.Event.CapacityChange(cap))
205+
},
206+
valueRange = CAPACITY_RANGE_VALUES.keys.let { it.first()..it.last() },
207+
steps = CAPACITY_RANGE_VALUES.size - 2,
208+
modifier =
209+
Modifier
210+
.padding(horizontal = 16.dp)
211+
.padding(bottom = 8.dp),
212+
)
213+
Text(
214+
text =
215+
when (state.capacity) {
216+
is Capacity.Specific ->
217+
pluralStringResource(
218+
R.plurals.settings_capacity_specific,
219+
state.capacity.value.toInt(),
220+
state.capacity.value,
221+
)
191222

192-
Capacity.Unlimited -> stringResource(R.string.settings_capacity_unlimited)
193-
null -> ""
194-
},
195-
textAlign = TextAlign.End,
196-
style = MaterialTheme.typography.labelLarge,
197-
modifier =
198-
Modifier
199-
.fillMaxWidth()
200-
.padding(horizontal = 16.dp),
201-
)
202-
if (state.capacityWasChanged) {
203-
Text(
204-
text = stringResource(R.string.settings_capacity_warning),
205-
style = MaterialTheme.typography.labelMedium,
206-
textAlign = TextAlign.End,
207-
modifier = Modifier
208-
.fillMaxWidth()
209-
.padding(vertical = 4.dp, horizontal = 16.dp),
210-
)
211-
}
212-
}
223+
Capacity.Unlimited -> stringResource(R.string.settings_capacity_unlimited)
224+
null -> ""
225+
},
226+
textAlign = TextAlign.End,
227+
style = MaterialTheme.typography.labelLarge,
228+
modifier =
229+
Modifier
230+
.fillMaxWidth()
231+
.padding(horizontal = 16.dp),
232+
)
233+
if (state.capacityWasChanged) {
234+
Text(
235+
text = stringResource(R.string.settings_capacity_warning),
236+
style = MaterialTheme.typography.labelMedium,
237+
textAlign = TextAlign.End,
238+
modifier = Modifier
239+
.fillMaxWidth()
240+
.padding(vertical = 4.dp, horizontal = 16.dp),
241+
)
213242
}
214243
}
215244

app/src/main/java/io/bloco/snowflake/ui/settings/SettingsViewModel.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import kotlinx.coroutines.flow.update
1616
class SettingsViewModel(
1717
getAppConfig: () -> Flow<AppConfig>,
1818
getCapacity: () -> Flow<Capacity>,
19+
setBackground: suspend (Boolean) -> Unit,
1920
setUnmeteredOnly: suspend (Boolean) -> Unit,
2021
setChargingOnly: suspend (Boolean) -> Unit,
2122
setCapacity: suspend (Capacity) -> Unit,
@@ -36,6 +37,11 @@ class SettingsViewModel(
3637
.onEach { _state.update { state -> state.copy(capacity = it) } }
3738
.launchIn(viewModelScope)
3839

40+
events
41+
.filterIsInstance<Event.BackgroundChange>()
42+
.onEach { setBackground(it.value) }
43+
.launchIn(viewModelScope)
44+
3945
events
4046
.filterIsInstance<Event.UnmeteredOnlyChange>()
4147
.onEach { setUnmeteredOnly(it.value) }
@@ -61,6 +67,10 @@ class SettingsViewModel(
6167
)
6268

6369
sealed interface Event {
70+
data class BackgroundChange(
71+
val value: Boolean,
72+
) : Event
73+
6474
data class UnmeteredOnlyChange(
6575
val value: Boolean,
6676
) : Event

0 commit comments

Comments
 (0)