-
Notifications
You must be signed in to change notification settings - Fork 0
Add in-memory theme picker in Settings #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -30,7 +30,9 @@ fun SettingsScreen( | |||||||||
| viewModel: SettingsViewModel = viewModel { SettingsViewModel() } | ||||||||||
| ) { | ||||||||||
| val uiState by viewModel.uiState.collectAsState() | ||||||||||
| val themePreference by viewModel.themePreference.collectAsState() | ||||||||||
| var showAccountsSheet by remember { mutableStateOf(false) } | ||||||||||
| var showThemeDialog by remember { mutableStateOf(false) } | ||||||||||
|
|
||||||||||
| // Refresh token state when returning to this screen | ||||||||||
| val lifecycle = LocalLifecycleOwner.current.lifecycle | ||||||||||
|
|
@@ -92,7 +94,7 @@ fun SettingsScreen( | |||||||||
| SettingsRow( | ||||||||||
| title = stringResource(Res.string.settings_theme), | ||||||||||
| subtitle = uiState.themeValue, | ||||||||||
| onClick = { /* TODO */ } | ||||||||||
| onClick = { showThemeDialog = true } | ||||||||||
| ) | ||||||||||
| SettingsRow( | ||||||||||
| title = stringResource(Res.string.settings_code_options), | ||||||||||
|
|
@@ -159,6 +161,51 @@ fun SettingsScreen( | |||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (showThemeDialog) { | ||||||||||
| AlertDialog( | ||||||||||
| onDismissRequest = { showThemeDialog = false }, | ||||||||||
| title = { Text(text = stringResource(Res.string.settings_theme)) }, | ||||||||||
| text = { | ||||||||||
| Column { | ||||||||||
| ThemePreference.values().forEach { preference -> | ||||||||||
| Row( | ||||||||||
| Modifier | ||||||||||
| .fillMaxWidth() | ||||||||||
| .clickable { | ||||||||||
| viewModel.setTheme(preference) | ||||||||||
| showThemeDialog = false | ||||||||||
| } | ||||||||||
| .padding(vertical = 12.dp), | ||||||||||
| verticalAlignment = Alignment.CenterVertically | ||||||||||
| ) { | ||||||||||
| RadioButton( | ||||||||||
| selected = preference == themePreference, | ||||||||||
| onClick = { | ||||||||||
| viewModel.setTheme(preference) | ||||||||||
| showThemeDialog = false | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| Spacer(modifier = Modifier.width(8.dp)) | ||||||||||
| Text( | ||||||||||
| text = when (preference) { | ||||||||||
| ThemePreference.SYSTEM -> stringResource(Res.string.settings_follow_system) | ||||||||||
| ThemePreference.LIGHT -> "Light" | ||||||||||
| ThemePreference.DARK -> "Dark" | ||||||||||
|
Comment on lines
+193
to
+194
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded strings "Light" and "Dark" should be replaced with localized string resources to support internationalization, ensuring consistency with other localized strings like
Suggested change
|
||||||||||
| }, | ||||||||||
| style = MaterialTheme.typography.bodyLarge | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
| }, | ||||||||||
| confirmButton = { | ||||||||||
| TextButton(onClick = { showThemeDialog = false }) { | ||||||||||
| Text(stringResource(Res.string.content_description_close)) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @Composable | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,11 +19,28 @@ class SettingsViewModel : ViewModel() { | |
| private val _uiState = MutableStateFlow(SettingsUiState()) | ||
| val uiState: StateFlow<SettingsUiState> = _uiState.asStateFlow() | ||
|
|
||
| val themePreference: StateFlow<ThemePreference> = ThemeStateHolder.themePreference.asStateFlow() | ||
|
|
||
| private val _signOutEvent = MutableSharedFlow<Unit>() | ||
| val signOutEvent: SharedFlow<Unit> = _signOutEvent.asSharedFlow() | ||
|
|
||
| init { | ||
| refresh() | ||
|
|
||
| viewModelScope.launch { | ||
| themePreference.collect { pref -> | ||
| val displayString = when (pref) { | ||
| ThemePreference.SYSTEM -> "Follow system" | ||
| ThemePreference.LIGHT -> "Light" | ||
| ThemePreference.DARK -> "Dark" | ||
| } | ||
| _uiState.value = _uiState.value.copy(themeValue = displayString) | ||
|
Comment on lines
+32
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mapping logic duplicates the string resolution found in the UI and uses hardcoded English strings. Additionally, since this is set in the |
||
| } | ||
| } | ||
| } | ||
|
|
||
| fun setTheme(preference: ThemePreference) { | ||
| ThemeStateHolder.themePreference.value = preference | ||
| } | ||
|
|
||
| fun refresh() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package dev.therealashik.github.settings | ||
|
|
||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
|
|
||
| enum class ThemePreference { | ||
| SYSTEM, | ||
| LIGHT, | ||
| DARK | ||
| } | ||
|
|
||
| object ThemeStateHolder { | ||
| val themePreference = MutableStateFlow(ThemePreference.SYSTEM) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
entriesinstead ofvalues()for Enum classes. In Kotlin 1.9.0+,entriesis preferred as it returns a pre-allocated immutable list, avoiding the array allocation overhead ofvalues()on every call.