Skip to content

Commit

Permalink
Fix splash screen theme
Browse files Browse the repository at this point in the history
  • Loading branch information
massivemadness committed Feb 5, 2025
1 parent 8ba74f4 commit 6b86e0d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 22 deletions.
5 changes: 4 additions & 1 deletion app/src/main/kotlin/com/blacksquircle/ui/SquircleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.work.Configuration
import com.blacksquircle.ui.core.logger.AndroidTree
import com.blacksquircle.ui.core.storage.keyvalue.SettingsManager
import com.blacksquircle.ui.core.theme.Theme
import com.blacksquircle.ui.core.theme.ThemeManager
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -40,7 +41,9 @@ class SquircleApp : Application(), Configuration.Provider {

override fun attachBaseContext(base: Context) {
val settingsManager = SettingsManager(base)
Theme.of(settingsManager.theme).apply()
val themeManager = ThemeManager(base)
val theme = Theme.of(settingsManager.theme)
themeManager.apply(theme)
super.attachBaseContext(base)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.blacksquircle.ui.core.storage.database.AppDatabase
import com.blacksquircle.ui.core.storage.database.AppDatabaseImpl
import com.blacksquircle.ui.core.storage.database.utils.Migrations
import com.blacksquircle.ui.core.storage.keyvalue.SettingsManager
import com.blacksquircle.ui.core.theme.ThemeManager
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -33,6 +34,12 @@ import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
object CoreModule {

@Provides
@Singleton
fun provideThemeManager(@ApplicationContext context: Context): ThemeManager {
return ThemeManager(context)
}

@Provides
@Singleton
fun provideSettingsManager(@ApplicationContext context: Context): SettingsManager {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,11 @@

package com.blacksquircle.ui.core.theme

import android.os.Build
import androidx.appcompat.app.AppCompatDelegate

enum class Theme(val value: String) {
LIGHT("light"),
DARK("dark"),
SYSTEM_DEFAULT("system_default");

fun apply() {
val mode = when (this) {
LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
DARK -> AppCompatDelegate.MODE_NIGHT_YES
SYSTEM_DEFAULT -> if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
} else {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
}
AppCompatDelegate.setDefaultNightMode(mode)
}

companion object {

fun of(value: String): Theme {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.core.theme

import android.app.UiModeManager
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate

class ThemeManager(private val context: Context) {

fun apply(theme: Theme) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
val mode = when (theme) {
Theme.LIGHT -> UiModeManager.MODE_NIGHT_NO
Theme.DARK -> UiModeManager.MODE_NIGHT_YES
Theme.SYSTEM_DEFAULT -> UiModeManager.MODE_NIGHT_AUTO
}
val uiModeManager = context.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
uiModeManager.setApplicationNightMode(mode)
} else {
val mode = when (theme) {
Theme.LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Theme.DARK -> AppCompatDelegate.MODE_NIGHT_YES
Theme.SYSTEM_DEFAULT -> if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
} else {
AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
}
}
AppCompatDelegate.setDefaultNightMode(mode)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ private fun AppHeaderScreen(
entries = stringArrayResource(R.array.theme_entries),
entryValues = stringArrayResource(R.array.theme_values),
selectedValue = viewState.appTheme,
onValueSelected = { value ->
onThemeChanged(value)
Theme.of(value).apply()
},
onValueSelected = onThemeChanged,
)
Preference(
title = stringResource(R.string.pref_color_scheme_title),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import androidx.lifecycle.viewModelScope
import com.blacksquircle.ui.core.mvi.ViewEvent
import com.blacksquircle.ui.core.navigation.Screen
import com.blacksquircle.ui.core.storage.keyvalue.SettingsManager
import com.blacksquircle.ui.core.theme.Theme
import com.blacksquircle.ui.core.theme.ThemeManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
Expand All @@ -33,7 +35,8 @@ import javax.inject.Inject

@HiltViewModel
internal class AppHeaderViewModel @Inject constructor(
private val settingsManager: SettingsManager
private val settingsManager: SettingsManager,
private val themeManager: ThemeManager,
) : ViewModel() {

private val _viewState = MutableStateFlow(updateViewState())
Expand All @@ -50,7 +53,9 @@ internal class AppHeaderViewModel @Inject constructor(

fun onThemeChanged(value: String) {
viewModelScope.launch {
val theme = Theme.of(value)
settingsManager.theme = value
themeManager.apply(theme)
_viewState.value = updateViewState()
}
}
Expand Down

0 comments on commit 6b86e0d

Please sign in to comment.