From c2be97741e78780339dfe399abd862596d5500e3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 11 Sep 2020 10:12:56 +0200 Subject: [PATCH] Restore listener after device rotation --- .../BackgroundSyncModeChooserDialog.kt | 5 +- ...rSettingsNotificationPreferenceFragment.kt | 49 ++++++++++--------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt b/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt index 8b45a06f23..59b8569c1e 100644 --- a/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt +++ b/vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt @@ -25,7 +25,7 @@ import im.vector.app.R class BackgroundSyncModeChooserDialog : DialogFragment() { - private var interactionListener: InteractionListener? = null + var interactionListener: InteractionListener? = null override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val initialMode = BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE)) @@ -65,9 +65,8 @@ class BackgroundSyncModeChooserDialog : DialogFragment() { companion object { private const val ARG_INITIAL_MODE = "ARG_INITIAL_MODE" - fun newInstance(selectedMode: BackgroundSyncMode, interactionListener: InteractionListener): BackgroundSyncModeChooserDialog { + fun newInstance(selectedMode: BackgroundSyncMode): BackgroundSyncModeChooserDialog { val frag = BackgroundSyncModeChooserDialog() - frag.interactionListener = interactionListener val args = Bundle() args.putString(ARG_INITIAL_MODE, selectedMode.name) frag.arguments = args diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt index 81036e64e1..62ecc606c4 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt @@ -47,7 +47,8 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( private val pushManager: PushersManager, private val activeSessionHolder: ActiveSessionHolder, private val vectorPreferences: VectorPreferences -) : VectorSettingsBaseFragment() { +) : VectorSettingsBaseFragment(), + BackgroundSyncModeChooserDialog.InteractionListener { override var titleRes: Int = R.string.settings_notifications override val preferenceXmlRes = R.xml.vector_settings_notifications @@ -73,28 +74,10 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( findPreference(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let { it.onPreferenceClickListener = Preference.OnPreferenceClickListener { val initialMode = vectorPreferences.getFdroidSyncBackgroundMode() - val dialogFragment = BackgroundSyncModeChooserDialog.newInstance( - initialMode, - object : BackgroundSyncModeChooserDialog.InteractionListener { - override fun onOptionSelected(mode: BackgroundSyncMode) { - // option has change, need to act - if (mode == BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME) { - // Important, Battery optim white listing is needed in this mode; - // Even if using foreground service with foreground notif, it stops to work - // in doze mode for certain devices :/ - if (!isIgnoringBatteryOptimizations(requireContext())) { - requestDisablingBatteryOptimization(requireActivity(), - this@VectorSettingsNotificationPreferenceFragment, - REQUEST_BATTERY_OPTIMIZATION) - } - } - vectorPreferences.setFdroidSyncBackgroundMode(mode) - refreshBackgroundSyncPrefs() - } - } - ) - activity?.supportFragmentManager?.let { - dialogFragment.show(it, "syncDialog") + val dialogFragment = BackgroundSyncModeChooserDialog.newInstance(initialMode) + dialogFragment.interactionListener = this + activity?.supportFragmentManager?.let {fm -> + dialogFragment.show(fm, "syncDialog") } true } @@ -131,6 +114,23 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( handleSystemPreference() } + // BackgroundSyncModeChooserDialog.InteractionListener + override fun onOptionSelected(mode: BackgroundSyncMode) { + // option has change, need to act + if (mode == BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME) { + // Important, Battery optim white listing is needed in this mode; + // Even if using foreground service with foreground notif, it stops to work + // in doze mode for certain devices :/ + if (!isIgnoringBatteryOptimizations(requireContext())) { + requestDisablingBatteryOptimization(requireActivity(), + this@VectorSettingsNotificationPreferenceFragment, + REQUEST_BATTERY_OPTIMIZATION) + } + } + vectorPreferences.setFdroidSyncBackgroundMode(mode) + refreshBackgroundSyncPrefs() + } + private fun refreshBackgroundSyncPrefs() { findPreference(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let { it.summary = when (vectorPreferences.getFdroidSyncBackgroundMode()) { @@ -251,6 +251,9 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor( if (context is VectorSettingsFragmentInteractionListener) { interactionListener = context } + (activity?.supportFragmentManager + ?.findFragmentByTag("syncDialog") as BackgroundSyncModeChooserDialog?) + ?.interactionListener = this } override fun onDetach() {