Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

yannigoyal
Copy link

What I have done and why

  • Used setApplicationNightMode via UiModeManager for API 31+ (Android 12+).
  • Used AppCompatDelegate.setDefaultNightMode for below API 31.
  • Ensures consistent dark theme behaviour across all Android versions.

Copy link

google-cla bot commented Jul 11, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@yannigoyal
Copy link
Author

@dturner My CLA certificate is not verifying I have done the same as per the instructions. Please help!

@dturner
Copy link
Collaborator

dturner commented Jul 11, 2025

It looks like this PR contains commits from multiple authors. All authors must sign the CLA.

@dturner
Copy link
Collaborator

dturner commented Jul 11, 2025

Specifically, this commit 9efe95d is from an author that hasn't signed the CLA

@yannigoyal
Copy link
Author

I have made all the commits myself since I read somewhere that I should verify the CLA by committing again. Should I fork again and make a new PR?

@yannigoyal
Copy link
Author

@dturner I have successfully signed the CLA. Can you please tell me if there is anything else I need to do?

@dturner
Copy link
Collaborator

dturner commented Jul 11, 2025

You need to update the unit tests so that they pass

@yannigoyal
Copy link
Author

Okay

Comment on lines +87 to +88
(context.getSystemService(Context.UI_MODE_SERVICE) as? UiModeManager)
?.setApplicationNightMode(uiModeMode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(context.getSystemService(Context.UI_MODE_SERVICE) as? UiModeManager)
?.setApplicationNightMode(uiModeMode)
context.getSystemService<UiModeManager>()?.setApplicationNightMode(uiModeMode)

@@ -62,6 +72,21 @@ class SettingsViewModel @Inject constructor(
fun updateDarkThemeConfig(darkThemeConfig: DarkThemeConfig) {
viewModelScope.launch {
userDataRepository.setDarkThemeConfig(darkThemeConfig)
val splashMode = when (darkThemeConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable name is quite misleading. Could we change it to nightMode?

}
AppCompatDelegate.setDefaultNightMode(splashMode)
if (Build.VERSION.SDK_INT >= VERSION_CODES.S) {
val uiModeMode = when (darkThemeConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uiModeMode should be renamed, why not simply uiMode or nightMode?

@@ -35,6 +44,7 @@ import kotlin.time.Duration.Companion.seconds
@HiltViewModel
class SettingsViewModel @Inject constructor(
private val userDataRepository: UserDataRepository,
@ApplicationContext private val context: Context,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exposing Context instances in ViewModels 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 accessing Context 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 a DarkThemeConfig altogether in a dedicated class?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy