0
0
mirror of https://github.com/Ishan09811/pine.git synced 2025-04-24 08:55:10 +00:00

Implement proper theme changing logic (#35)

recreate the activity when turning on or off material colors instead of restarting the entire application
This commit is contained in:
Ishan09811 2024-12-20 13:17:26 +05:30 committed by GitHub
parent 4cc4981d08
commit 3d9df8b827
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions

View File

@ -29,6 +29,12 @@ class SkylineApplication : Application() {
private set
val context : Context get() = instance.applicationContext
fun setTheme(newValue: Boolean) {
val dynamicColorsOptions = DynamicColorsOptions.Builder().setPrecondition { _, _ -> newValue }.build()
DynamicColors.applyToActivitiesIfAvailable(instance, dynamicColorsOptions)
if (newValue == false) { instance.setTheme(R.style.AppTheme) }
}
}
override fun onCreate() {

View File

@ -20,6 +20,7 @@ import emu.skyline.MainActivity
import emu.skyline.R
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.SkylineApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -45,9 +46,10 @@ class GlobalSettingsFragment : PreferenceFragmentCompat() {
addPreferencesFromResource(R.xml.credits_preferences)
// Re-launch the app if Material You is toggled
findPreference<Preference>("use_material_you")?.setOnPreferenceChangeListener { _, _ ->
requireActivity().finishAffinity()
startActivity(Intent(requireContext(), MainActivity::class.java))
findPreference<Preference>("use_material_you")?.setOnPreferenceChangeListener { _, newValue ->
val isMaterialYouEnabled = newValue as Boolean
SkylineApplication.setTheme(isMaterialYouEnabled)
requireActivity().recreate()
true
}