2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-03 23:15:31 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/22851: Adjust theme based on the selected wallpaper.

This commit is contained in:
Arturo Mejia 2021-12-21 17:41:41 -05:00 committed by mergify[bot]
parent d6cca53ac2
commit 0b88222bf5
3 changed files with 40 additions and 5 deletions

View File

@ -490,7 +490,7 @@ class Settings(private val appContext: Context) : PreferencesHolder {
}
}
val shouldUseDarkTheme by booleanPreference(
var shouldUseDarkTheme by booleanPreference(
appContext.getPreferenceKey(R.string.pref_key_dark_theme),
default = false
)

View File

@ -9,8 +9,8 @@ import org.mozilla.fenix.R
/**
* A enum that represents the available wallpapers and their states.
*/
enum class Wallpaper(val drawable: Int) {
FIRST(R.drawable.wallpaper_1),
SECOND(R.drawable.wallpaper_2),
NONE(R.attr.homeBackground);
enum class Wallpaper(val drawable: Int, val isDark: Boolean) {
FIRST(drawable = R.drawable.wallpaper_1, isDark = true),
SECOND(drawable = R.drawable.wallpaper_2, isDark = false),
NONE(drawable = R.attr.homeBackground, isDark = false);
}

View File

@ -4,8 +4,11 @@
package org.mozilla.fenix.wallpapers
import android.content.Context
import android.view.View
import androidx.appcompat.app.AppCompatDelegate
import mozilla.components.support.ktx.android.content.getColorFromAttr
import org.mozilla.fenix.ext.asActivity
import org.mozilla.fenix.utils.Settings
/**
@ -30,6 +33,38 @@ class WallpaperManager(private val settings: Settings) {
wallpaperContainer.setBackgroundResource(newWallpaper.drawable)
}
currentWallpaper = newWallpaper
adjustTheme(wallpaperContainer.context)
}
private fun adjustTheme(context: Context) {
val mode = if (currentWallpaper != Wallpaper.NONE) {
if (currentWallpaper.isDark) {
updateThemePreference(useDarkTheme = true)
AppCompatDelegate.MODE_NIGHT_YES
} else {
updateThemePreference(useLightTheme = true)
AppCompatDelegate.MODE_NIGHT_NO
}
} else {
updateThemePreference(followDeviceTheme = true)
AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
if (AppCompatDelegate.getDefaultNightMode() != mode) {
AppCompatDelegate.setDefaultNightMode(mode)
context.asActivity()?.recreate()
}
}
private fun updateThemePreference(
useDarkTheme: Boolean = false,
useLightTheme: Boolean = false,
followDeviceTheme: Boolean = false
) {
settings.shouldUseDarkTheme = useDarkTheme
settings.shouldUseLightTheme = useLightTheme
settings.shouldFollowDeviceTheme = followDeviceTheme
}
/**