Skip to content

Commit c2be977

Browse files
committed
Restore listener after device rotation
1 parent db977b8 commit c2be977

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

vector/src/main/java/im/vector/app/features/settings/BackgroundSyncModeChooserDialog.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import im.vector.app.R
2525

2626
class BackgroundSyncModeChooserDialog : DialogFragment() {
2727

28-
private var interactionListener: InteractionListener? = null
28+
var interactionListener: InteractionListener? = null
2929

3030
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
3131
val initialMode = BackgroundSyncMode.fromString(arguments?.getString(ARG_INITIAL_MODE))
@@ -65,9 +65,8 @@ class BackgroundSyncModeChooserDialog : DialogFragment() {
6565
companion object {
6666
private const val ARG_INITIAL_MODE = "ARG_INITIAL_MODE"
6767

68-
fun newInstance(selectedMode: BackgroundSyncMode, interactionListener: InteractionListener): BackgroundSyncModeChooserDialog {
68+
fun newInstance(selectedMode: BackgroundSyncMode): BackgroundSyncModeChooserDialog {
6969
val frag = BackgroundSyncModeChooserDialog()
70-
frag.interactionListener = interactionListener
7170
val args = Bundle()
7271
args.putString(ARG_INITIAL_MODE, selectedMode.name)
7372
frag.arguments = args

vector/src/main/java/im/vector/app/features/settings/VectorSettingsNotificationPreferenceFragment.kt

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
4747
private val pushManager: PushersManager,
4848
private val activeSessionHolder: ActiveSessionHolder,
4949
private val vectorPreferences: VectorPreferences
50-
) : VectorSettingsBaseFragment() {
50+
) : VectorSettingsBaseFragment(),
51+
BackgroundSyncModeChooserDialog.InteractionListener {
5152

5253
override var titleRes: Int = R.string.settings_notifications
5354
override val preferenceXmlRes = R.xml.vector_settings_notifications
@@ -73,28 +74,10 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
7374
findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let {
7475
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
7576
val initialMode = vectorPreferences.getFdroidSyncBackgroundMode()
76-
val dialogFragment = BackgroundSyncModeChooserDialog.newInstance(
77-
initialMode,
78-
object : BackgroundSyncModeChooserDialog.InteractionListener {
79-
override fun onOptionSelected(mode: BackgroundSyncMode) {
80-
// option has change, need to act
81-
if (mode == BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME) {
82-
// Important, Battery optim white listing is needed in this mode;
83-
// Even if using foreground service with foreground notif, it stops to work
84-
// in doze mode for certain devices :/
85-
if (!isIgnoringBatteryOptimizations(requireContext())) {
86-
requestDisablingBatteryOptimization(requireActivity(),
87-
this@VectorSettingsNotificationPreferenceFragment,
88-
REQUEST_BATTERY_OPTIMIZATION)
89-
}
90-
}
91-
vectorPreferences.setFdroidSyncBackgroundMode(mode)
92-
refreshBackgroundSyncPrefs()
93-
}
94-
}
95-
)
96-
activity?.supportFragmentManager?.let {
97-
dialogFragment.show(it, "syncDialog")
77+
val dialogFragment = BackgroundSyncModeChooserDialog.newInstance(initialMode)
78+
dialogFragment.interactionListener = this
79+
activity?.supportFragmentManager?.let {fm ->
80+
dialogFragment.show(fm, "syncDialog")
9881
}
9982
true
10083
}
@@ -131,6 +114,23 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
131114
handleSystemPreference()
132115
}
133116

117+
// BackgroundSyncModeChooserDialog.InteractionListener
118+
override fun onOptionSelected(mode: BackgroundSyncMode) {
119+
// option has change, need to act
120+
if (mode == BackgroundSyncMode.FDROID_BACKGROUND_SYNC_MODE_FOR_REALTIME) {
121+
// Important, Battery optim white listing is needed in this mode;
122+
// Even if using foreground service with foreground notif, it stops to work
123+
// in doze mode for certain devices :/
124+
if (!isIgnoringBatteryOptimizations(requireContext())) {
125+
requestDisablingBatteryOptimization(requireActivity(),
126+
this@VectorSettingsNotificationPreferenceFragment,
127+
REQUEST_BATTERY_OPTIMIZATION)
128+
}
129+
}
130+
vectorPreferences.setFdroidSyncBackgroundMode(mode)
131+
refreshBackgroundSyncPrefs()
132+
}
133+
134134
private fun refreshBackgroundSyncPrefs() {
135135
findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let {
136136
it.summary = when (vectorPreferences.getFdroidSyncBackgroundMode()) {
@@ -251,6 +251,9 @@ class VectorSettingsNotificationPreferenceFragment @Inject constructor(
251251
if (context is VectorSettingsFragmentInteractionListener) {
252252
interactionListener = context
253253
}
254+
(activity?.supportFragmentManager
255+
?.findFragmentByTag("syncDialog") as BackgroundSyncModeChooserDialog?)
256+
?.interactionListener = this
254257
}
255258

256259
override fun onDetach() {

0 commit comments

Comments
 (0)