Restore listener after device rotation

This commit is contained in:
Benoit Marty 2020-09-11 10:12:56 +02:00
parent db977b8109
commit c2be97741e
2 changed files with 28 additions and 26 deletions

View File

@ -25,7 +25,7 @@ import im.vector.app.R
class BackgroundSyncModeChooserDialog : DialogFragment() { class BackgroundSyncModeChooserDialog : DialogFragment() {
private var interactionListener: InteractionListener? = null var interactionListener: InteractionListener? = null
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val initialMode = BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE)) val initialMode = BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE))
@ -65,9 +65,8 @@ class BackgroundSyncModeChooserDialog : DialogFragment() {
companion object { companion object {
private const val ARG_INITIAL_MODE = "ARG_INITIAL_MODE" private const val ARG_INITIAL_MODE = "ARG_INITIAL_MODE"
fun newInstance(selectedMode: BackgroundSyncMode, interactionListener: InteractionListener): BackgroundSyncModeChooserDialog { fun newInstance(selectedMode: BackgroundSyncMode): BackgroundSyncModeChooserDialog {
val frag = BackgroundSyncModeChooserDialog() val frag = BackgroundSyncModeChooserDialog()
frag.interactionListener = interactionListener
val args = Bundle() val args = Bundle()
args.putString(ARG_INITIAL_MODE, selectedMode.name) args.putString(ARG_INITIAL_MODE, selectedMode.name)
frag.arguments = args frag.arguments = args

View File

@ -47,7 +47,8 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
private val pushManager: PushersManager, private val pushManager: PushersManager,
private val activeSessionHolder: ActiveSessionHolder, private val activeSessionHolder: ActiveSessionHolder,
private val vectorPreferences: VectorPreferences private val vectorPreferences: VectorPreferences
) : VectorSettingsBaseFragment() { ) : VectorSettingsBaseFragment(),
BackgroundSyncModeChooserDialog.InteractionListener {
override var titleRes: Int = R.string.settings_notifications override var titleRes: Int = R.string.settings_notifications
override val preferenceXmlRes = R.xml.vector_settings_notifications override val preferenceXmlRes = R.xml.vector_settings_notifications
@ -73,28 +74,10 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let { findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let {
it.onPreferenceClickListener = Preference.OnPreferenceClickListener { it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
val initialMode = vectorPreferences.getFdroidSyncBackgroundMode() val initialMode = vectorPreferences.getFdroidSyncBackgroundMode()
val dialogFragment = BackgroundSyncModeChooserDialog.newInstance( val dialogFragment = BackgroundSyncModeChooserDialog.newInstance(initialMode)
initialMode, dialogFragment.interactionListener = this
object : BackgroundSyncModeChooserDialog.InteractionListener { activity?.supportFragmentManager?.let {fm ->
override fun onOptionSelected(mode: BackgroundSyncMode) { dialogFragment.show(fm, "syncDialog")
// 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")
} }
true true
} }
@ -131,6 +114,23 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
handleSystemPreference() 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() { private fun refreshBackgroundSyncPrefs() {
findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let { findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let {
it.summary = when (vectorPreferences.getFdroidSyncBackgroundMode()) { it.summary = when (vectorPreferences.getFdroidSyncBackgroundMode()) {
@ -251,6 +251,9 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
if (context is VectorSettingsFragmentInteractionListener) { if (context is VectorSettingsFragmentInteractionListener) {
interactionListener = context interactionListener = context
} }
(activity?.supportFragmentManager
?.findFragmentByTag("syncDialog") as BackgroundSyncModeChooserDialog?)
?.interactionListener = this
} }
override fun onDetach() { override fun onDetach() {