PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/fragments/settings/ThemeSettingsFragment.kt

130 lines
5.4 KiB
Kotlin
Raw Normal View History

2019-05-11 13:53:20 +00:00
/*
2020-10-06 08:46:04 +00:00
* Copyright (c) 2020 Hemanth Savarla.
2019-05-11 13:53:20 +00:00
*
* Licensed under the GNU General Public License v3
*
2020-10-06 08:46:04 +00:00
* This is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
2019-05-11 13:53:20 +00:00
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
2020-10-06 08:46:04 +00:00
*
2019-05-11 13:53:20 +00:00
*/
2019-04-20 05:29:45 +00:00
package code.name.monkey.retromusic.fragments.settings
2018-12-06 08:52:57 +00:00
import android.os.Build
import android.os.Bundle
2019-03-25 12:43:43 +00:00
import androidx.preference.Preference
2018-12-06 08:52:57 +00:00
import androidx.preference.TwoStatePreference
import code.name.monkey.appthemehelper.ACCENT_COLORS
import code.name.monkey.appthemehelper.ACCENT_COLORS_SUB
2019-09-03 17:54:14 +00:00
import code.name.monkey.appthemehelper.ThemeStore
2018-12-06 08:52:57 +00:00
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEColorPreference
2019-09-28 17:52:49 +00:00
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATESwitchPreference
2018-12-06 08:52:57 +00:00
import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.*
2018-12-08 03:33:02 +00:00
import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager
2020-06-06 18:57:28 +00:00
import code.name.monkey.retromusic.util.PreferenceUtil
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.color.colorChooser
import com.google.android.material.color.DynamicColors
2018-12-06 08:52:57 +00:00
/**
* @author Hemanth S (h4h13).
*/
class ThemeSettingsFragment : AbsSettingsFragment() {
override fun invalidateSettings() {
val generalTheme: Preference? = findPreference(GENERAL_THEME)
generalTheme?.let {
setSummary(it)
it.setOnPreferenceChangeListener { _, newValue ->
val theme = newValue as String
setSummary(it, newValue)
2019-09-03 17:54:14 +00:00
ThemeStore.markChanged(requireContext())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
2020-06-06 18:57:28 +00:00
requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue(theme))
2019-08-01 14:13:00 +00:00
DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
}
restartActivity()
true
2018-12-06 08:52:57 +00:00
}
}
val accentColorPref: ATEColorPreference? = findPreference(ACCENT_COLOR)
2019-08-01 14:13:00 +00:00
val accentColor = ThemeStore.accentColor(requireContext())
accentColorPref?.setColor(accentColor, ColorUtil.darkenColor(accentColor))
accentColorPref?.setOnPreferenceClickListener {
MaterialDialog(requireContext()).show {
colorChooser(
initialSelection = accentColor,
showAlphaSelector = false,
colors = ACCENT_COLORS,
subColors = ACCENT_COLORS_SUB, allowCustomArgb = true
) { _, color ->
ThemeStore.editTheme(requireContext()).accentColor(color).commit()
if (VersionUtils.hasNougatMR())
DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
restartActivity()
}
}
2019-02-19 10:38:51 +00:00
return@setOnPreferenceClickListener true
2018-12-06 08:52:57 +00:00
}
val blackTheme: ATESwitchPreference? = findPreference(BLACK_THEME)
2019-09-28 17:52:49 +00:00
blackTheme?.setOnPreferenceChangeListener { _, _ ->
2019-12-26 15:09:28 +00:00
if (!App.isProVersion()) {
showProToastAndNavigate("Just Black theme")
return@setOnPreferenceChangeListener false
}
2019-09-28 17:52:49 +00:00
ThemeStore.markChanged(requireContext())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
2020-06-06 18:57:28 +00:00
requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue("black"))
2019-09-28 17:52:49 +00:00
DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
}
restartActivity()
2019-09-28 17:52:49 +00:00
true
}
2018-12-06 08:52:57 +00:00
val desaturatedColor: ATESwitchPreference? = findPreference(DESATURATED_COLOR)
2019-10-12 06:41:48 +00:00
desaturatedColor?.setOnPreferenceChangeListener { _, value ->
val desaturated = value as Boolean
ThemeStore.prefs(requireContext())
.edit()
.putBoolean("desaturated_color", desaturated)
.apply()
2020-06-06 18:57:28 +00:00
PreferenceUtil.isDesaturatedColor = desaturated
restartActivity()
2019-10-12 06:41:48 +00:00
true
}
val colorAppShortcuts: TwoStatePreference? = findPreference(SHOULD_COLOR_APP_SHORTCUTS)
2018-12-06 08:52:57 +00:00
if (!VersionUtils.hasNougatMR()) {
colorAppShortcuts?.isVisible = false
2018-12-06 08:52:57 +00:00
} else {
colorAppShortcuts?.isChecked = PreferenceUtil.isColoredAppShortcuts
colorAppShortcuts?.setOnPreferenceChangeListener { _, newValue ->
2020-06-06 18:57:28 +00:00
PreferenceUtil.isColoredAppShortcuts = newValue as Boolean
2019-08-01 14:13:00 +00:00
DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
2018-12-06 08:52:57 +00:00
true
}
}
val materialYou: ATESwitchPreference? = findPreference(MATERIAL_YOU)
materialYou?.setOnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
DynamicColors.applyToActivitiesIfAvailable(App.getContext())
}
restartActivity()
true
}
2018-12-06 08:52:57 +00:00
}
2018-12-06 10:23:03 +00:00
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.pref_general)
2018-12-06 08:52:57 +00:00
}
}