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
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
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.databinding.ActivitySettingsBinding
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.surfaceColor
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.color.ColorCallback
class SettingsActivity : AbsBaseActivity(), ColorCallback {
class SettingsActivity : AbsBaseActivity(), ColorCallback, OnThemeChangedListener {
private lateinit var binding: ActivitySettingsBinding
override fun onCreate(savedInstanceState: Bundle?) {
setDrawUnderStatusBar()
super.onCreate(savedInstanceState)
val mSavedInstanceState = extra<Bundle>(TAG).value ?: savedInstanceState
super.onCreate(mSavedInstanceState)
setLightStatusbarAuto(surfaceColor())
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
@ -83,6 +86,28 @@ class SettingsActivity : AbsBaseActivity(), ColorCallback {
if (VersionUtils.hasNougatMR())
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.PreferenceManager
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.util.NavigationUtil
@ -94,4 +95,12 @@ abstract class AbsSettingsFragment : ATEPreferenceFragmentCompat() {
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)
languagePreference?.setOnPreferenceChangeListener { _, _ ->
println("Invalidated")
requireActivity().recreate()
restartActivity()
return@setOnPreferenceChangeListener true
}
}
@ -69,7 +69,7 @@ class OtherSettingsFragment : AbsSettingsFragment() {
manager.startInstall(request)
// Recreate the activity on download complete
.addOnCompleteListener {
activity?.recreate()
restartActivity()
}
} else {
requireActivity().recreate()

View file

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

View file

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