Apply theme preferences to LauncherActivity
This commit is contained in:
parent
f701e90c47
commit
8799c15682
@ -1,12 +1,49 @@
|
|||||||
package de.mm20.launcher2.ui.base
|
package de.mm20.launcher2.ui.base
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import de.mm20.launcher2.permissions.PermissionsManager
|
import de.mm20.launcher2.permissions.PermissionsManager
|
||||||
|
import de.mm20.launcher2.preferences.Settings
|
||||||
|
import de.mm20.launcher2.ui.R
|
||||||
import org.koin.android.ext.android.inject
|
import org.koin.android.ext.android.inject
|
||||||
|
|
||||||
abstract class BaseActivity : AppCompatActivity() {
|
abstract class BaseActivity : AppCompatActivity() {
|
||||||
private val permissionsManager: PermissionsManager by inject()
|
private val permissionsManager: PermissionsManager by inject()
|
||||||
|
|
||||||
|
private val viewModel: BaseActivityVM by viewModels()
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
val theme = viewModel.getTheme()
|
||||||
|
when (theme) {
|
||||||
|
Settings.AppearanceSettings.Theme.Light -> AppCompatDelegate.setDefaultNightMode(
|
||||||
|
AppCompatDelegate.MODE_NIGHT_NO
|
||||||
|
)
|
||||||
|
Settings.AppearanceSettings.Theme.Dark -> AppCompatDelegate.setDefaultNightMode(
|
||||||
|
AppCompatDelegate.MODE_NIGHT_YES
|
||||||
|
)
|
||||||
|
else -> AppCompatDelegate.setDefaultNightMode(
|
||||||
|
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||||
|
)
|
||||||
|
}
|
||||||
|
viewModel.theme.observe(this) {
|
||||||
|
if (it != theme && it != null) recreate()
|
||||||
|
}
|
||||||
|
|
||||||
|
val colorScheme = viewModel.getColorScheme()
|
||||||
|
val colorSchemeThemeId = when (colorScheme) {
|
||||||
|
Settings.AppearanceSettings.ColorScheme.BlackAndWhite -> R.style.BlackWhiteColors
|
||||||
|
else -> R.style.DefaultColors
|
||||||
|
}
|
||||||
|
this.theme.applyStyle(colorSchemeThemeId, true)
|
||||||
|
|
||||||
|
viewModel.colorScheme.observe(this) {
|
||||||
|
if (it != colorScheme && it != null) recreate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onRequestPermissionsResult(
|
override fun onRequestPermissionsResult(
|
||||||
requestCode: Int,
|
requestCode: Int,
|
||||||
permissions: Array<out String>,
|
permissions: Array<out String>,
|
||||||
|
|||||||
27
ui/src/main/java/de/mm20/launcher2/ui/base/BaseActivityVM.kt
Normal file
27
ui/src/main/java/de/mm20/launcher2/ui/base/BaseActivityVM.kt
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package de.mm20.launcher2.ui.base
|
||||||
|
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.asLiveData
|
||||||
|
import de.mm20.launcher2.preferences.LauncherDataStore
|
||||||
|
import de.mm20.launcher2.preferences.Settings
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
|
||||||
|
class BaseActivityVM : ViewModel(), KoinComponent {
|
||||||
|
private val dataStore: LauncherDataStore by inject()
|
||||||
|
|
||||||
|
val theme = dataStore.data.map { it.appearance.theme }.asLiveData()
|
||||||
|
|
||||||
|
fun getTheme(): Settings.AppearanceSettings.Theme = runBlocking {
|
||||||
|
dataStore.data.map { it.appearance.theme }.first()
|
||||||
|
}
|
||||||
|
|
||||||
|
val colorScheme = dataStore.data.map { it.appearance.colorScheme }.asLiveData()
|
||||||
|
|
||||||
|
fun getColorScheme(): Settings.AppearanceSettings.ColorScheme = runBlocking {
|
||||||
|
dataStore.data.map { it.appearance.colorScheme }.first()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -34,41 +34,12 @@ class LauncherActivity : BaseActivity() {
|
|||||||
|
|
||||||
private val viewModel: LauncherActivityVM by viewModels()
|
private val viewModel: LauncherActivityVM by viewModels()
|
||||||
|
|
||||||
private val preferences = LauncherPreferences.instance
|
|
||||||
|
|
||||||
private var windowBackgroundBlur: Boolean = false
|
|
||||||
set(value) {
|
|
||||||
if (field == value) return
|
|
||||||
field = value
|
|
||||||
if (!isAtLeastApiLevel(31)) return
|
|
||||||
window.attributes = window.attributes.also {
|
|
||||||
if (value) {
|
|
||||||
it.blurBehindRadius = (32 * dp).toInt()
|
|
||||||
it.flags = it.flags or WindowManager.LayoutParams.FLAG_BLUR_BEHIND
|
|
||||||
} else {
|
|
||||||
it.blurBehindRadius = 0
|
|
||||||
it.flags = it.flags and WindowManager.LayoutParams.FLAG_BLUR_BEHIND.inv()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun updateSystemBarAppearance() {
|
|
||||||
val allowLightSystemBars = allowsLightSystemBars()
|
|
||||||
val insetsController = WindowInsetsControllerCompat(window, window.decorView)
|
|
||||||
insetsController.isAppearanceLightNavigationBars =
|
|
||||||
allowLightSystemBars && preferences.lightNavBar
|
|
||||||
insetsController.isAppearanceLightStatusBars =
|
|
||||||
allowLightSystemBars && preferences.lightStatusBar
|
|
||||||
}
|
|
||||||
|
|
||||||
private lateinit var binding: ActivityLauncherBinding
|
private lateinit var binding: ActivityLauncherBinding
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
val iconRepository: IconRepository by inject()
|
val iconRepository: IconRepository by inject()
|
||||||
iconRepository.recreate()
|
iconRepository.recreate()
|
||||||
ThemeHelper.applyTheme(theme)
|
|
||||||
|
|
||||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||||
|
|
||||||
@ -132,19 +103,11 @@ class LauncherActivity : BaseActivity() {
|
|||||||
ActivityStarter.create(binding.rootView)
|
ActivityStarter.create(binding.rootView)
|
||||||
binding.activityStartOverlay.visibility = View.INVISIBLE
|
binding.activityStartOverlay.visibility = View.INVISIBLE
|
||||||
|
|
||||||
updateSystemBarAppearance()
|
|
||||||
|
|
||||||
binding.container.doOnNextLayout {
|
binding.container.doOnNextLayout {
|
||||||
WallpaperManager.getInstance(this).setWallpaperOffsets(it.windowToken, 0.5f, 0.5f)
|
WallpaperManager.getInstance(this).setWallpaperOffsets(it.windowToken, 0.5f, 0.5f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun allowsLightSystemBars(): Boolean {
|
|
||||||
val dimWallpaper = LauncherPreferences.instance.dimWallpaper
|
|
||||||
val isDarkTheme = resources.getBoolean(R.bool.is_dark_theme)
|
|
||||||
return !(isDarkTheme && dimWallpaper)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
super.onPause()
|
super.onPause()
|
||||||
ActivityStarter.pause()
|
ActivityStarter.pause()
|
||||||
|
|||||||
@ -41,40 +41,10 @@ import de.mm20.launcher2.ui.settings.wikipedia.WikipediaSettingsScreen
|
|||||||
|
|
||||||
class SettingsActivity : BaseActivity() {
|
class SettingsActivity : BaseActivity() {
|
||||||
|
|
||||||
private val viewModel: SettingsActivityVM by viewModels()
|
|
||||||
|
|
||||||
@OptIn(ExperimentalAnimationApi::class)
|
@OptIn(ExperimentalAnimationApi::class)
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
val theme = viewModel.getTheme()
|
|
||||||
when (theme) {
|
|
||||||
Settings.AppearanceSettings.Theme.Light -> AppCompatDelegate.setDefaultNightMode(
|
|
||||||
AppCompatDelegate.MODE_NIGHT_NO
|
|
||||||
)
|
|
||||||
Settings.AppearanceSettings.Theme.Dark -> AppCompatDelegate.setDefaultNightMode(
|
|
||||||
AppCompatDelegate.MODE_NIGHT_YES
|
|
||||||
)
|
|
||||||
else -> AppCompatDelegate.setDefaultNightMode(
|
|
||||||
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
||||||
)
|
|
||||||
}
|
|
||||||
viewModel.theme.observe(this) {
|
|
||||||
if (it != theme && it != null) recreate()
|
|
||||||
}
|
|
||||||
|
|
||||||
val colorScheme = viewModel.getColorScheme()
|
|
||||||
val colorSchemeThemeId = when(colorScheme) {
|
|
||||||
Settings.AppearanceSettings.ColorScheme.BlackAndWhite -> R.style.BlackWhiteColors
|
|
||||||
else -> R.style.DefaultColors
|
|
||||||
}
|
|
||||||
this.theme.applyStyle(colorSchemeThemeId, true)
|
|
||||||
|
|
||||||
viewModel.colorScheme.observe(this) {
|
|
||||||
if (it != colorScheme && it != null) recreate()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
val navController = rememberAnimatedNavController()
|
val navController = rememberAnimatedNavController()
|
||||||
CompositionLocalProvider(LocalNavController provides navController) {
|
CompositionLocalProvider(LocalNavController provides navController) {
|
||||||
|
|||||||
@ -12,17 +12,4 @@ import org.koin.core.component.KoinComponent
|
|||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
|
|
||||||
class SettingsActivityVM: ViewModel(), KoinComponent {
|
class SettingsActivityVM: ViewModel(), KoinComponent {
|
||||||
private val dataStore: LauncherDataStore by inject()
|
|
||||||
|
|
||||||
val theme = dataStore.data.map { it.appearance.theme }.asLiveData()
|
|
||||||
|
|
||||||
fun getTheme(): Settings.AppearanceSettings.Theme = runBlocking {
|
|
||||||
dataStore.data.map { it.appearance.theme }.first()
|
|
||||||
}
|
|
||||||
|
|
||||||
val colorScheme = dataStore.data.map { it.appearance.colorScheme }.asLiveData()
|
|
||||||
|
|
||||||
fun getColorScheme(): Settings.AppearanceSettings.ColorScheme = runBlocking {
|
|
||||||
dataStore.data.map { it.appearance.colorScheme }.first()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user