Added smooth fade when theme is changed

This commit is contained in:
Prathamesh More 2021-09-22 17:30:02 +05:30
parent 6a5cc51db4
commit 381e112dac
5 changed files with 44 additions and 10 deletions

View file

@ -14,6 +14,7 @@
*/ */
package code.name.monkey.retromusic.activities package code.name.monkey.retromusic.activities
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import androidx.navigation.NavController import androidx.navigation.NavController
@ -25,16 +26,18 @@ import code.name.monkey.retromusic.activities.base.AbsBaseActivity
import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager
import code.name.monkey.retromusic.databinding.ActivitySettingsBinding import code.name.monkey.retromusic.databinding.ActivitySettingsBinding
import code.name.monkey.retromusic.extensions.applyToolbar import code.name.monkey.retromusic.extensions.applyToolbar
import code.name.monkey.retromusic.extensions.extra
import code.name.monkey.retromusic.extensions.findNavController import code.name.monkey.retromusic.extensions.findNavController
import code.name.monkey.retromusic.extensions.surfaceColor import code.name.monkey.retromusic.extensions.surfaceColor
import com.afollestad.materialdialogs.MaterialDialog import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.color.ColorCallback import com.afollestad.materialdialogs.color.ColorCallback
class SettingsActivity : AbsBaseActivity(), ColorCallback { class SettingsActivity : AbsBaseActivity(), ColorCallback, OnThemeChangedListener {
private lateinit var binding: ActivitySettingsBinding private lateinit var binding: ActivitySettingsBinding
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
setDrawUnderStatusBar() setDrawUnderStatusBar()
super.onCreate(savedInstanceState) val mSavedInstanceState = extra<Bundle>(TAG).value ?: savedInstanceState
super.onCreate(mSavedInstanceState)
setLightStatusbarAuto(surfaceColor()) setLightStatusbarAuto(surfaceColor())
binding = ActivitySettingsBinding.inflate(layoutInflater) binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root) setContentView(binding.root)
@ -83,6 +86,28 @@ class SettingsActivity : AbsBaseActivity(), ColorCallback {
if (VersionUtils.hasNougatMR()) if (VersionUtils.hasNougatMR())
DynamicShortcutManager(this).updateDynamicShortcuts() DynamicShortcutManager(this).updateDynamicShortcuts()
recreate() restart()
}
override fun onThemeValuesChanged() {
restart()
}
private fun restart() {
val savedInstanceState = Bundle().apply {
onSaveInstanceState(this)
}
finish()
val intent = Intent(this, this::class.java).putExtra(TAG, savedInstanceState)
startActivity(intent)
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
}
companion object {
val TAG: String = SettingsActivity::class.java.simpleName
} }
} }
interface OnThemeChangedListener {
fun onThemeValuesChanged()
}

View file

@ -23,6 +23,7 @@ import androidx.preference.ListPreference
import androidx.preference.Preference import androidx.preference.Preference
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEPreferenceFragmentCompat import code.name.monkey.appthemehelper.common.prefs.supportv7.ATEPreferenceFragmentCompat
import code.name.monkey.retromusic.activities.OnThemeChangedListener
import code.name.monkey.retromusic.preferences.* import code.name.monkey.retromusic.preferences.*
import code.name.monkey.retromusic.util.NavigationUtil import code.name.monkey.retromusic.util.NavigationUtil
@ -94,4 +95,12 @@ abstract class AbsSettingsFragment : ATEPreferenceFragmentCompat() {
else -> super.onDisplayPreferenceDialog(preference) else -> super.onDisplayPreferenceDialog(preference)
} }
} }
fun restartActivity() {
if (activity is OnThemeChangedListener) {
(activity as OnThemeChangedListener).onThemeValuesChanged()
} else {
activity?.recreate()
}
}
} }

View file

@ -39,7 +39,7 @@ class OtherSettingsFragment : AbsSettingsFragment() {
val languagePreference: ATEListPreference? = findPreference(LANGUAGE_NAME) val languagePreference: ATEListPreference? = findPreference(LANGUAGE_NAME)
languagePreference?.setOnPreferenceChangeListener { _, _ -> languagePreference?.setOnPreferenceChangeListener { _, _ ->
println("Invalidated") println("Invalidated")
requireActivity().recreate() restartActivity()
return@setOnPreferenceChangeListener true return@setOnPreferenceChangeListener true
} }
} }
@ -69,7 +69,7 @@ class OtherSettingsFragment : AbsSettingsFragment() {
manager.startInstall(request) manager.startInstall(request)
// Recreate the activity on download complete // Recreate the activity on download complete
.addOnCompleteListener { .addOnCompleteListener {
activity?.recreate() restartActivity()
} }
} else { } else {
requireActivity().recreate() requireActivity().recreate()

View file

@ -25,7 +25,7 @@ class PersonalizeSettingsFragment : AbsSettingsFragment() {
override fun invalidateSettings() { override fun invalidateSettings() {
val toggleFullScreen: TwoStatePreference? = findPreference(TOGGLE_FULL_SCREEN) val toggleFullScreen: TwoStatePreference? = findPreference(TOGGLE_FULL_SCREEN)
toggleFullScreen?.setOnPreferenceChangeListener { _, _ -> toggleFullScreen?.setOnPreferenceChangeListener { _, _ ->
requireActivity().recreate() restartActivity()
true true
} }
} }

View file

@ -49,7 +49,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue(theme)) requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue(theme))
DynamicShortcutManager(requireContext()).updateDynamicShortcuts() DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
} }
requireActivity().recreate() restartActivity()
true true
} }
} }
@ -68,7 +68,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
ThemeStore.editTheme(requireContext()).accentColor(color).commit() ThemeStore.editTheme(requireContext()).accentColor(color).commit()
if (VersionUtils.hasNougatMR()) if (VersionUtils.hasNougatMR())
DynamicShortcutManager(requireContext()).updateDynamicShortcuts() DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
requireActivity().recreate() restartActivity()
} }
} }
return@setOnPreferenceClickListener true return@setOnPreferenceClickListener true
@ -84,7 +84,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue("black")) requireActivity().setTheme(PreferenceUtil.themeResFromPrefValue("black"))
DynamicShortcutManager(requireContext()).updateDynamicShortcuts() DynamicShortcutManager(requireContext()).updateDynamicShortcuts()
} }
requireActivity().recreate() restartActivity()
true true
} }
@ -96,7 +96,7 @@ class ThemeSettingsFragment : AbsSettingsFragment() {
.putBoolean("desaturated_color", desaturated) .putBoolean("desaturated_color", desaturated)
.apply() .apply()
PreferenceUtil.isDesaturatedColor = desaturated PreferenceUtil.isDesaturatedColor = desaturated
requireActivity().recreate() restartActivity()
true true
} }