Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ class SettingsScreenTest {
PyrycodeMobileTheme {
SettingsScreen(
themeMode = ThemeMode.SYSTEM,
useWallpaperColors = false,
onSelectTheme = {},
onToggleUseWallpaperColors = {},
onBack = {},
onOpenLicense = {},
onOpenArchivedDiscussions = {},
Expand All @@ -44,7 +46,9 @@ class SettingsScreenTest {
PyrycodeMobileTheme {
SettingsScreen(
themeMode = ThemeMode.SYSTEM,
useWallpaperColors = false,
onSelectTheme = {},
onToggleUseWallpaperColors = {},
onBack = {},
onOpenLicense = {},
onOpenArchivedDiscussions = {},
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/de/pyryco/mobile/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,12 @@ private fun PyryNavHost(
composable(Routes.SETTINGS) {
val vm = koinViewModel<SettingsViewModel>()
val themeMode by vm.themeMode.collectAsStateWithLifecycle()
val useWallpaperColors by vm.useWallpaperColors.collectAsStateWithLifecycle()
SettingsScreen(
themeMode = themeMode,
useWallpaperColors = useWallpaperColors,
onSelectTheme = vm::onSelectTheme,
onToggleUseWallpaperColors = vm::onToggleUseWallpaperColors,
onBack = { navController.popBackStack() },
onOpenLicense = { navController.navigate(Routes.LICENSE) },
onOpenArchivedDiscussions = { navController.navigate(Routes.ARCHIVED_DISCUSSIONS) },
Expand Down
23 changes: 20 additions & 3 deletions app/src/main/java/de/pyryco/mobile/ui/settings/SettingsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.pyryco.mobile.ui.settings

import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
Expand Down Expand Up @@ -47,12 +48,15 @@ import de.pyryco.mobile.ui.theme.PyrycodeMobileTheme
@Composable
fun SettingsScreen(
themeMode: ThemeMode,
useWallpaperColors: Boolean,
onSelectTheme: (ThemeMode) -> Unit,
onToggleUseWallpaperColors: (Boolean) -> Unit,
onBack: () -> Unit,
onOpenLicense: () -> Unit,
onOpenArchivedDiscussions: () -> Unit,
modifier: Modifier = Modifier,
) {
val dynamicColorSupported = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
Scaffold(
modifier = modifier,
topBar = {
Expand All @@ -70,7 +74,6 @@ fun SettingsScreen(
},
) { inner ->
val context = LocalContext.current
var materialYou by remember { mutableStateOf(true) }
var defaultYolo by remember { mutableStateOf(false) }
var pushNotifications by remember { mutableStateOf(true) }
var showThemeDialog by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -115,9 +118,19 @@ fun SettingsScreen(
onClick = { showThemeDialog = true },
)
SettingsRow(
headline = "Use Material You dynamic color",
headline = stringResource(R.string.settings_use_wallpaper_colors),
supporting =
if (!dynamicColorSupported) {
stringResource(R.string.settings_use_wallpaper_colors_unsupported)
} else {
null
},
trailing = {
Switch(checked = materialYou, onCheckedChange = { materialYou = it })
Switch(
checked = useWallpaperColors,
onCheckedChange = onToggleUseWallpaperColors,
enabled = dynamicColorSupported,
)
},
)

Expand Down Expand Up @@ -291,7 +304,9 @@ private fun SettingsScreenLightPreview() {
PyrycodeMobileTheme(darkTheme = false) {
SettingsScreen(
themeMode = ThemeMode.SYSTEM,
useWallpaperColors = false,
onSelectTheme = {},
onToggleUseWallpaperColors = {},
onBack = {},
onOpenLicense = {},
onOpenArchivedDiscussions = {},
Expand All @@ -305,7 +320,9 @@ private fun SettingsScreenDarkPreview() {
PyrycodeMobileTheme(darkTheme = true) {
SettingsScreen(
themeMode = ThemeMode.SYSTEM,
useWallpaperColors = false,
onSelectTheme = {},
onToggleUseWallpaperColors = {},
onBack = {},
onOpenLicense = {},
onOpenArchivedDiscussions = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@ class SettingsViewModel(
initialValue = ThemeMode.SYSTEM,
)

val useWallpaperColors: StateFlow<Boolean> =
appPreferences.useWallpaperColors.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(STOP_TIMEOUT_MILLIS),
initialValue = false,
)

fun onSelectTheme(mode: ThemeMode) {
viewModelScope.launch { appPreferences.setThemeMode(mode) }
}

fun onToggleUseWallpaperColors(enabled: Boolean) {
viewModelScope.launch { appPreferences.setUseWallpaperColors(enabled) }
}

private companion object {
const val STOP_TIMEOUT_MILLIS = 5_000L
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<string name="promote_dialog_confirm">Save as channel</string>
<string name="promote_dialog_cancel">Cancel</string>
<string name="settings_theme_dialog_title">Theme</string>
<string name="settings_use_wallpaper_colors">Use wallpaper colors</string>
<string name="settings_use_wallpaper_colors_unsupported">Requires Android 12 or newer</string>
<string name="archived_discussions_title">Archived discussions</string>
<string name="archived_discussions_empty">No archived discussions</string>
<string name="archived_discussions_settings_row">Archived discussions</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,46 @@ class SettingsViewModelTest {
assertEquals(ThemeMode.LIGHT, vm.themeMode.value)
collector.cancel()
}

@Test
fun useWallpaperColors_initialState_emitsFalse_whenNoStoredValue() =
runTest(dispatcher) {
val prefs = AppPreferences(newDataStore())
val vm = SettingsViewModel(prefs)
val collector = launch { vm.useWallpaperColors.collect { } }
advanceUntilIdle()
assertEquals(false, vm.useWallpaperColors.value)
collector.cancel()
}

@Test
fun useWallpaperColors_initialState_mirrorsPersistedTrue() =
runTest(dispatcher) {
val prefs = AppPreferences(newDataStore())
prefs.setUseWallpaperColors(true)
advanceUntilIdle()
val vm = SettingsViewModel(prefs)
val collector = launch { vm.useWallpaperColors.collect { } }
advanceUntilIdle()
assertEquals(true, vm.useWallpaperColors.value)
collector.cancel()
}

@Test
fun onToggleUseWallpaperColors_persistsAndFlowReEmits() =
runTest(dispatcher) {
val prefs = AppPreferences(newDataStore())
val vm = SettingsViewModel(prefs)
val collector = launch { vm.useWallpaperColors.collect { } }
advanceUntilIdle()
vm.onToggleUseWallpaperColors(true)
advanceUntilIdle()
assertEquals(true, prefs.useWallpaperColors.first())
assertEquals(true, vm.useWallpaperColors.value)
vm.onToggleUseWallpaperColors(false)
advanceUntilIdle()
assertEquals(false, prefs.useWallpaperColors.first())
assertEquals(false, vm.useWallpaperColors.value)
collector.cancel()
}
}
Loading
Loading