-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Update SettingsViewModel.kt #1897
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
base: main
Are you sure you want to change the base?
Changes from all commits
9efe95d
7d6c98e
0ae715a
7c9c983
8bacb1c
bc1007d
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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -16,14 +16,23 @@ | |||||||
|
||||||||
package com.google.samples.apps.nowinandroid.feature.settings | ||||||||
|
||||||||
import android.app.UiModeManager | ||||||||
import android.content.Context | ||||||||
import android.os.Build | ||||||||
import android.os.Build.VERSION_CODES | ||||||||
import androidx.appcompat.app.AppCompatDelegate | ||||||||
import androidx.lifecycle.ViewModel | ||||||||
import androidx.lifecycle.viewModelScope | ||||||||
import com.google.samples.apps.nowinandroid.core.data.repository.UserDataRepository | ||||||||
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig | ||||||||
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.DARK | ||||||||
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.FOLLOW_SYSTEM | ||||||||
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig.LIGHT | ||||||||
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand | ||||||||
import com.google.samples.apps.nowinandroid.feature.settings.SettingsUiState.Loading | ||||||||
import com.google.samples.apps.nowinandroid.feature.settings.SettingsUiState.Success | ||||||||
import dagger.hilt.android.lifecycle.HiltViewModel | ||||||||
import dagger.hilt.android.qualifiers.ApplicationContext | ||||||||
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed | ||||||||
import kotlinx.coroutines.flow.StateFlow | ||||||||
import kotlinx.coroutines.flow.map | ||||||||
|
@@ -35,6 +44,7 @@ import kotlin.time.Duration.Companion.seconds | |||||||
@HiltViewModel | ||||||||
class SettingsViewModel @Inject constructor( | ||||||||
private val userDataRepository: UserDataRepository, | ||||||||
@ApplicationContext private val context: Context, | ||||||||
) : ViewModel() { | ||||||||
val settingsUiState: StateFlow<SettingsUiState> = | ||||||||
userDataRepository.userData | ||||||||
|
@@ -62,6 +72,21 @@ class SettingsViewModel @Inject constructor( | |||||||
fun updateDarkThemeConfig(darkThemeConfig: DarkThemeConfig) { | ||||||||
viewModelScope.launch { | ||||||||
userDataRepository.setDarkThemeConfig(darkThemeConfig) | ||||||||
val splashMode = when (darkThemeConfig) { | ||||||||
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 variable name is quite misleading. Could we change it to |
||||||||
FOLLOW_SYSTEM -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM | ||||||||
LIGHT -> AppCompatDelegate.MODE_NIGHT_NO | ||||||||
DARK -> AppCompatDelegate.MODE_NIGHT_YES | ||||||||
} | ||||||||
AppCompatDelegate.setDefaultNightMode(splashMode) | ||||||||
if (Build.VERSION.SDK_INT >= VERSION_CODES.S) { | ||||||||
val uiModeMode = when (darkThemeConfig) { | ||||||||
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.
|
||||||||
FOLLOW_SYSTEM -> UiModeManager.MODE_NIGHT_AUTO | ||||||||
LIGHT -> UiModeManager.MODE_NIGHT_NO | ||||||||
DARK -> UiModeManager.MODE_NIGHT_YES | ||||||||
} | ||||||||
(context.getSystemService(Context.UI_MODE_SERVICE) as? UiModeManager) | ||||||||
?.setApplicationNightMode(uiModeMode) | ||||||||
Comment on lines
+87
to
+88
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.
Suggested change
|
||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
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.
Exposing
Context
instances inViewModel
s is generally a bad practice as it might lead to memory leak (here since we use@ApplicationContext
it does not really leak anything), and facilitate accessingContext
resources that might change through configuration changes that would not be reflected in the ViewModel scope.Could we instead request a
UiModeManager
and@Provide
it in a Hilt module? (or even extract this logic of applying aDarkThemeConfig
altogether in a dedicated class?)