mirror of
https://github.com/Michatec/Radio.git
synced 2026-04-01 07:56:27 +02:00
Initial commit
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* AppThemeHelper.kt
|
||||
* Implements the AppThemeHelper object
|
||||
* A AppThemeHelper can set the different app themes: Light Mode, Dark Mode, Follow System
|
||||
*
|
||||
* This file is part of
|
||||
* TRANSISTOR - Radio App for Android
|
||||
*
|
||||
* Copyright (c) 2015-22 - Y20K.org
|
||||
* Licensed under the MIT-License
|
||||
* http://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
|
||||
package com.michatec.radio.helpers
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.TypedArray
|
||||
import android.util.Log
|
||||
import android.util.TypedValue
|
||||
import androidx.annotation.AttrRes
|
||||
import androidx.annotation.ColorInt
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import com.michatec.radio.Keys
|
||||
import com.michatec.radio.R
|
||||
|
||||
|
||||
/*
|
||||
* AppThemeHelper object
|
||||
*/
|
||||
object AppThemeHelper {
|
||||
|
||||
/* Define log tag */
|
||||
private val TAG: String = AppThemeHelper::class.java.simpleName
|
||||
|
||||
private val sTypedValue = TypedValue()
|
||||
|
||||
/* Sets app theme */
|
||||
fun setTheme(nightModeState: String) {
|
||||
when (nightModeState) {
|
||||
Keys.STATE_THEME_DARK_MODE -> {
|
||||
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_YES) {
|
||||
// turn on dark mode
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
||||
Log.i(TAG, "Dark Mode activated.")
|
||||
}
|
||||
}
|
||||
Keys.STATE_THEME_LIGHT_MODE -> {
|
||||
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_NO) {
|
||||
// turn on light mode
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
||||
Log.i(TAG, "Theme: Light Mode activated.")
|
||||
}
|
||||
}
|
||||
Keys.STATE_THEME_FOLLOW_SYSTEM -> {
|
||||
if (AppCompatDelegate.getDefaultNightMode() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) {
|
||||
// turn on mode "follow system"
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||
Log.i(TAG, "Theme: Follow System Mode activated.")
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
// turn on mode "follow system"
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
||||
Log.i(TAG, "Theme: Follow System Mode activated.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Returns a readable String for currently selected App Theme */
|
||||
fun getCurrentTheme(context: Context): String {
|
||||
return when (PreferencesHelper.loadThemeSelection()) {
|
||||
Keys.STATE_THEME_LIGHT_MODE -> context.getString(R.string.pref_theme_selection_mode_light)
|
||||
Keys.STATE_THEME_DARK_MODE -> context.getString(R.string.pref_theme_selection_mode_dark)
|
||||
else -> context.getString(R.string.pref_theme_selection_mode_device_default)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ColorInt
|
||||
fun getColor(context: Context, @AttrRes resource: Int): Int {
|
||||
val a: TypedArray = context.obtainStyledAttributes(sTypedValue.data, intArrayOf(resource))
|
||||
val color = a.getColor(0, 0)
|
||||
a.recycle()
|
||||
return color
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user