Fix crash and color theme selection for none pro users

This commit is contained in:
h4h13 2019-07-08 20:02:23 +05:30
parent f40a8e2d3e
commit e4a4874edc
3 changed files with 59 additions and 53 deletions

View file

@ -23,7 +23,6 @@ import androidx.preference.Preference
import androidx.preference.TwoStatePreference import androidx.preference.TwoStatePreference
import code.name.monkey.appthemehelper.* import code.name.monkey.appthemehelper.*
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEColorPreference import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEColorPreference
import code.name.monkey.appthemehelper.util.ATHUtil
import code.name.monkey.appthemehelper.util.ColorUtil import code.name.monkey.appthemehelper.util.ColorUtil
import code.name.monkey.appthemehelper.util.VersionUtils import code.name.monkey.appthemehelper.util.VersionUtils
import code.name.monkey.retromusic.App import code.name.monkey.retromusic.App
@ -42,15 +41,20 @@ import com.afollestad.materialdialogs.color.colorChooser
class ThemeSettingsFragment : AbsSettingsFragment() { class ThemeSettingsFragment : AbsSettingsFragment() {
override fun invalidateSettings() { override fun invalidateSettings() {
val primaryColorPref: ATEColorPreference = findPreference("primary_color")!! val primaryColorPref: ATEColorPreference? = findPreference("primary_color")
primaryColorPref.isVisible = PreferenceUtil.getInstance().generalTheme == R.style.Theme_RetroMusic_Color primaryColorPref?.let {
it.isVisible = PreferenceUtil.getInstance().generalTheme == R.style.Theme_RetroMusic_Color
val primaryColor = ThemeStore.primaryColor(activity!!) val primaryColor = ThemeStore.primaryColor(activity!!)
primaryColorPref.setColor(primaryColor, ColorUtil.darkenColor(primaryColor)) it.setColor(primaryColor, ColorUtil.darkenColor(primaryColor))
primaryColorPref.setOnPreferenceClickListener { it.setOnPreferenceClickListener {
MaterialDialog(activity!!, BottomSheet()).show { MaterialDialog(activity!!, BottomSheet()).show {
title(code.name.monkey.retromusic.R.string.primary_color) title(R.string.primary_color)
positiveButton(R.string.set) positiveButton(R.string.set)
colorChooser(initialSelection = BLUE, allowCustomArgb = true, colors = PRIMARY_COLORS, subColors = PRIMARY_COLORS_SUB) { _, color -> colorChooser(initialSelection = BLUE,
allowCustomArgb = true,
colors = PRIMARY_COLORS,
subColors = PRIMARY_COLORS_SUB) { _, color ->
val theme = if (ColorUtil.isColorLight(color)) val theme = if (ColorUtil.isColorLight(color))
PreferenceUtil.getThemeResFromPrefValue("light") PreferenceUtil.getThemeResFromPrefValue("light")
else else
@ -65,31 +69,30 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
} }
true true
} }
}
val generalTheme: Preference = findPreference("general_theme")!! val generalTheme: Preference? = findPreference("general_theme")
setSummary(generalTheme)
generalTheme.setOnPreferenceChangeListener { _, newValue -> generalTheme?.let {
setSummary(it)
it.setOnPreferenceChangeListener { _, newValue ->
val theme = newValue as String val theme = newValue as String
println(newValue)
if (theme == "color" && !App.isProVersion) { if (theme == "color" && !App.isProVersion) {
primaryColorPref.isVisible = false
showProToastAndNavigate("Color theme") showProToastAndNavigate("Color theme")
return@setOnPreferenceChangeListener false return@setOnPreferenceChangeListener false
} else { }
primaryColorPref.isVisible = true
if (theme == "color") {
primaryColorPref?.isVisible = true
} }
setSummary(generalTheme, newValue) setSummary(generalTheme, newValue)
when (theme) { when (theme) {
"light" -> ThemeStore.editTheme(context!!).primaryColor(Color.WHITE).commit() "light" -> ThemeStore.editTheme(context!!).primaryColor(Color.WHITE).commit()
"black" -> ThemeStore.editTheme(context!!).primaryColor(Color.BLACK).commit() "black" -> ThemeStore.editTheme(context!!).primaryColor(Color.BLACK).commit()
"dark" -> ThemeStore.editTheme(context!!).primaryColor(ContextCompat.getColor(context!!, R.color.md_grey_900)).commit() "dark" -> ThemeStore.editTheme(context!!).primaryColor(ContextCompat.getColor(context!!, R.color.md_grey_900)).commit()
"daynight" -> {
val color = ATHUtil.resolveColor(context!!, android.R.attr.colorPrimary)
ThemeStore.editTheme(context!!).primaryColor(color).commit()
}
"color" -> ThemeStore.editTheme(context!!).primaryColor(ContextCompat.getColor(context!!, R.color.md_blue_grey_800)).commit() "color" -> ThemeStore.editTheme(context!!).primaryColor(ContextCompat.getColor(context!!, R.color.md_blue_grey_800)).commit()
} }
@ -98,13 +101,13 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
.commit() .commit()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
activity!!.setTheme(PreferenceUtil.getThemeResFromPrefValue(theme)) activity?.setTheme(PreferenceUtil.getThemeResFromPrefValue(theme))
DynamicShortcutManager(activity!!).updateDynamicShortcuts() DynamicShortcutManager(activity!!).updateDynamicShortcuts()
} }
activity!!.recreate() activity?.recreate()
//invalidateSettings();
true true
} }
}
val accentColorPref: ATEColorPreference = findPreference("accent_color")!! val accentColorPref: ATEColorPreference = findPreference("accent_color")!!
val accentColor = ThemeStore.accentColor(activity!!) val accentColor = ThemeStore.accentColor(activity!!)

View file

@ -2,7 +2,7 @@
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<code.name.monkey.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory android:title="@string/pref_header_general"> <code.name.monkey.appthemehelper.common.prefs.supportv7.ATEPreferenceCategory android:title="@string/pref_header_general">
<code.name.monkey.retromusic.preferences.MaterialListPreference <code.name.monkey.appthemehelper.common.prefs.supportv7.ATEListPreference
android:defaultValue="dark" android:defaultValue="dark"
android:entries="@array/pref_general_theme_list_titles" android:entries="@array/pref_general_theme_list_titles"
android:entryValues="@array/pref_general_theme_list_values" android:entryValues="@array/pref_general_theme_list_values"

View file

@ -1,9 +1,11 @@
package code.name.monkey.appthemehelper.common.prefs.supportv7 package code.name.monkey.appthemehelper.common.prefs.supportv7
import android.content.Context import android.content.Context
import android.graphics.PorterDuff
import android.util.AttributeSet import android.util.AttributeSet
import androidx.preference.ListPreference import androidx.preference.ListPreference
import code.name.monkey.appthemehelper.R import code.name.monkey.appthemehelper.R
import code.name.monkey.appthemehelper.ThemeStore
/** /**
* @author Aidan Follestad (afollestad) * @author Aidan Follestad (afollestad)
@ -27,6 +29,7 @@ class ATEListPreference : ListPreference {
} }
private fun init() { private fun init() {
icon?.setColorFilter(ThemeStore.textColorSecondary(context), PorterDuff.Mode.SRC_IN)
layoutResource = R.layout.ate_preference_custom_support layoutResource = R.layout.ate_preference_custom_support
if (summary == null || summary.toString().trim { it <= ' ' }.isEmpty()) if (summary == null || summary.toString().trim { it <= ' ' }.isEmpty())
summary = "%s" summary = "%s"