Implement theme customization

Implement the ability to change the theme to auto/light/dark.
This commit is contained in:
OxygenCobalt
2020-11-28 16:17:54 -07:00
parent 76c1fe1d75
commit 2dc7ba3420
16 changed files with 297 additions and 79 deletions
@@ -5,18 +5,22 @@ import android.content.Intent
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import org.oxycblt.auxio.playback.PlaybackService
import org.oxycblt.auxio.prefs.PrefsManager
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.accent
// FIXME: Fix bug where fast navigation will break the animations and
// lead to nothing being displayed [Possibly Un-fixable]
// TODO: Landscape UI layouts
// FIXME: Compat issue with Versions 5 that leads to progress bar looking off
class MainActivity : AppCompatActivity(R.layout.activity_main) {
class MainActivity : AppCompatActivity(R.layout.activity_main), SettingsManager.Callback {
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
val prefsManager = PrefsManager.init(this)
val settingsManager = SettingsManager.init(applicationContext)
AppCompatDelegate.setDefaultNightMode(
settingsManager.getTheme()
)
// Apply the theme
setTheme(accent.second)
@@ -31,4 +35,22 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
startService(it)
}
}
override fun onResume() {
super.onResume()
// Perform callback additions/removals in onPause/onResume so that they are always
// ran when the activity is recreated.
SettingsManager.getInstance().addCallback(this)
}
override fun onPause() {
super.onPause()
SettingsManager.getInstance().removeCallback(this)
}
override fun onThemeUpdate(value: Int) {
AppCompatDelegate.setDefaultNightMode(value)
}
}
@@ -13,9 +13,9 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.music.BaseModel
import org.oxycblt.auxio.music.Header
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.prefs.PrefsManager
import org.oxycblt.auxio.recycler.ShowMode
import org.oxycblt.auxio.recycler.SortMode
import org.oxycblt.auxio.settings.SettingsManager
/**
* A [ViewModel] that manages what [LibraryFragment] is currently showing, and also the search
@@ -40,7 +40,7 @@ class LibraryViewModel : ViewModel() {
val searchHasFocus: Boolean get() = mSearchHasFocus
init {
val prefsManager = PrefsManager.getInstance()
val prefsManager = SettingsManager.getInstance()
viewModelScope.launch {
mSortMode.value = withContext(Dispatchers.IO) {
@@ -135,7 +135,7 @@ class LibraryViewModel : ViewModel() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
val prefsManager = PrefsManager.getInstance()
val prefsManager = SettingsManager.getInstance()
prefsManager.setLibrarySortMode(mSortMode.value!!)
}
@@ -1,71 +0,0 @@
package org.oxycblt.auxio.prefs
import android.content.Context
import android.content.SharedPreferences
import org.oxycblt.auxio.recycler.SortMode
/**
* Wrapper around the [SharedPreferences] class that writes & reads values.
* Please run any getter/setter in a coroutine. Its not required, but it prevents slowdowns
* on older devices.
* @author OxygenCobalt
*/
class PrefsManager private constructor(context: Context) {
private val sharedPrefs = context.getSharedPreferences(
"auxio_prefs", Context.MODE_PRIVATE
)
private lateinit var mLibrarySortMode: SortMode
fun setLibrarySortMode(sortMode: SortMode) {
mLibrarySortMode = sortMode
sharedPrefs.edit()
.putInt(Keys.KEY_LIBRARY_SORT_MODE, sortMode.toConstant())
.apply()
}
fun getLibrarySortMode(): SortMode {
if (!::mLibrarySortMode.isInitialized) {
mLibrarySortMode = SortMode.fromConstant(
sharedPrefs.getInt(
Keys.KEY_LIBRARY_SORT_MODE,
SortMode.CONSTANT_ALPHA_DOWN
)
) ?: SortMode.ALPHA_DOWN
}
return mLibrarySortMode
}
companion object {
@Volatile
private lateinit var INSTANCE: PrefsManager
/**
* Init the single instance of [PrefsManager]. Done so that every object
* can have access to it regardless of if it has a context.
*/
fun init(context: Context): PrefsManager {
synchronized(this) {
INSTANCE = PrefsManager(context)
return getInstance()
}
}
/**
* Get the single instance of [PrefsManager].
*/
fun getInstance(): PrefsManager {
check(::INSTANCE.isInitialized) {
"PrefsManager must be initialized with init() before getting its instance."
}
return INSTANCE
}
}
object Keys {
const val KEY_LIBRARY_SORT_MODE = "KEY_LIBRARY_SORT_MODE"
}
}
@@ -0,0 +1,11 @@
package org.oxycblt.auxio.settings
import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat
import org.oxycblt.auxio.R
class SettingListFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.prefs_main, rootKey)
}
}
@@ -0,0 +1,20 @@
package org.oxycblt.auxio.settings
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import org.oxycblt.auxio.databinding.FragmentSettingsBinding
class SettingsFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding = FragmentSettingsBinding.inflate(inflater)
return binding.root
}
}
@@ -0,0 +1,118 @@
package org.oxycblt.auxio.settings
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.PreferenceManager
import org.oxycblt.auxio.recycler.SortMode
/**
* Wrapper around the [SharedPreferences] class that writes & reads values without a context.
*
* **Note:** Run any getter in a IO coroutine if possible, as SharedPrefs will read from disk
* the first time it occurs.
* @author OxygenCobalt
*/
class SettingsManager private constructor(context: Context) : SharedPreferences.OnSharedPreferenceChangeListener {
private val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context)
init {
sharedPrefs.registerOnSharedPreferenceChangeListener(this)
}
private val callbacks = mutableListOf<Callback>()
fun addCallback(callback: Callback) {
callbacks.add(callback)
}
fun removeCallback(callback: Callback) {
callbacks.remove(callback)
}
fun setLibrarySortMode(sortMode: SortMode) {
sharedPrefs.edit()
.putInt(Keys.KEY_LIBRARY_SORT_MODE, sortMode.toConstant())
.apply()
}
fun getLibrarySortMode(): SortMode {
return SortMode.fromConstant(
sharedPrefs.getInt(
Keys.KEY_LIBRARY_SORT_MODE,
SortMode.CONSTANT_ALPHA_DOWN
)
) ?: SortMode.ALPHA_DOWN
}
fun getTheme(): Int {
// Turn the string from SharedPreferences into an actual theme value that can
// be used, as apparently the preference system provided by androidx doesn't like integers
// for some...reason.
return when (sharedPrefs.getString(Keys.KEY_THEME, Theme.THEME_AUTO)) {
Theme.THEME_AUTO -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
Theme.THEME_LIGHT -> AppCompatDelegate.MODE_NIGHT_NO
Theme.THEME_DARK -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
Keys.KEY_THEME -> {
callbacks.forEach {
it.onThemeUpdate(getTheme())
}
}
}
}
companion object {
@Volatile
private lateinit var INSTANCE: SettingsManager
/**
* Init the single instance of [SettingsManager]. Done so that every object
* can have access to it regardless of if it has a context.
*/
fun init(context: Context): SettingsManager {
if (!::INSTANCE.isInitialized) {
synchronized(this) {
INSTANCE = SettingsManager(context)
}
}
return getInstance()
}
/**
* Get the single instance of [SettingsManager].
*/
fun getInstance(): SettingsManager {
check(::INSTANCE.isInitialized) {
"PrefsManager must be initialized with init() before getting its instance."
}
return INSTANCE
}
}
object Keys {
const val KEY_LIBRARY_SORT_MODE = "KEY_LIBRARY_SORT_MODE"
const val KEY_THEME = "KEY_THEME"
}
private object Theme {
const val THEME_AUTO = "AUTO"
const val THEME_LIGHT = "LIGHT"
const val THEME_DARK = "DARK"
}
/**
* A safe interface for receiving preference updates, use this instead of
* [SharedPreferences.OnSharedPreferenceChangeListener].
*/
interface Callback {
fun onThemeUpdate(value: Int) {}
}
}