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() {
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

View File

@ -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<VectorPreference>(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<VectorPreference>(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() {