48 lines
1.6 KiB
Kotlin
48 lines
1.6 KiB
Kotlin
package code.name.monkey.retromusic.util.theme
|
|
|
|
import android.content.Context
|
|
import android.content.res.Configuration
|
|
import android.os.PowerManager
|
|
import androidx.annotation.StyleRes
|
|
import androidx.appcompat.app.AppCompatDelegate
|
|
import code.name.monkey.retromusic.R
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
|
import code.name.monkey.retromusic.util.theme.ThemeManager.isSystemDarkModeEnabled
|
|
import code.name.monkey.retromusic.util.theme.ThemeMode.*
|
|
|
|
object ThemeManager {
|
|
|
|
@StyleRes
|
|
fun getThemeResValue(
|
|
context: Context
|
|
): Int = when (context.generalThemeValue) {
|
|
LIGHT -> R.style.Theme_RetroMusic_Light
|
|
DARK -> R.style.Theme_RetroMusic_Base
|
|
BLACK -> R.style.Theme_RetroMusic_Black
|
|
AUTO -> R.style.Theme_RetroMusic_FollowSystem
|
|
}
|
|
|
|
fun getNightMode(
|
|
context: Context
|
|
): Int = when (context.generalThemeValue) {
|
|
LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
|
|
DARK,
|
|
BLACK -> AppCompatDelegate.MODE_NIGHT_YES
|
|
AUTO -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
|
}
|
|
|
|
fun isSystemDarkModeEnabled(context: Context): Boolean {
|
|
val isBatterySaverEnabled =
|
|
(context.getSystemService(Context.POWER_SERVICE) as PowerManager?)?.isPowerSaveMode
|
|
?: false
|
|
val isDarkModeEnabled =
|
|
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
|
|
|
|
return isBatterySaverEnabled or isDarkModeEnabled
|
|
}
|
|
}
|
|
|
|
val Context.generalThemeValue: ThemeMode
|
|
get() {
|
|
return PreferenceUtil.getInstance(this).getGeneralThemeValue(isSystemDarkModeEnabled(this))
|
|
} |