diff --git a/library/ui-strings/src/main/res/values/strings.xml b/library/ui-strings/src/main/res/values/strings.xml
index f05a2a11e6..372692770e 100644
--- a/library/ui-strings/src/main/res/values/strings.xml
+++ b/library/ui-strings/src/main/res/values/strings.xml
@@ -1679,7 +1679,8 @@
Create New Room
Create New Space
No network. Please check your Internet connection.
- Something went wrong. Please check your network connection and try again.
+
+ Something went wrong. Please check your network connection and try again.
"Change network"
"Please wait…"
Updating your data…
diff --git a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt
index cda6f5bae8..6f186262fc 100644
--- a/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt
+++ b/vector/src/main/java/im/vector/app/core/pushers/PushersManager.kt
@@ -97,12 +97,6 @@ class PushersManager @Inject constructor(
return session.pushersService().getPushers().firstOrNull { it.deviceId == deviceId }
}
- suspend fun togglePusherForCurrentSession(enable: Boolean) {
- val session = activeSessionHolder.getSafeActiveSession() ?: return
- val pusher = getPusherForCurrentSession() ?: return
- session.pushersService().togglePusher(pusher, enable)
- }
-
suspend fun unregisterEmailPusher(email: String) {
val currentSession = activeSessionHolder.getSafeActiveSession() ?: return
currentSession.pushersService().removeEmailPusher(email)
diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt
new file mode 100644
index 0000000000..8962e8d67d
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/settings/notifications/DisableNotificationsForCurrentSessionUseCase.kt
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2022 New Vector Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package im.vector.app.features.settings.notifications
+
+import im.vector.app.core.di.ActiveSessionHolder
+import im.vector.app.core.pushers.PushersManager
+import im.vector.app.core.pushers.UnifiedPushHelper
+import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase
+import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
+import javax.inject.Inject
+
+class DisableNotificationsForCurrentSessionUseCase @Inject constructor(
+ private val activeSessionHolder: ActiveSessionHolder,
+ private val unifiedPushHelper: UnifiedPushHelper,
+ private val pushersManager: PushersManager,
+ private val checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase,
+ private val togglePushNotificationUseCase: TogglePushNotificationUseCase,
+) {
+
+ // TODO add unit tests
+ suspend fun execute() {
+ val session = activeSessionHolder.getSafeActiveSession() ?: return
+ val deviceId = session.sessionParams.deviceId ?: return
+ if (checkIfCanTogglePushNotificationsViaPusherUseCase.execute()) {
+ togglePushNotificationUseCase.execute(deviceId, enabled = false)
+ } else {
+ unifiedPushHelper.unregister(pushersManager)
+ }
+ }
+}
diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt
new file mode 100644
index 0000000000..ef37f67bef
--- /dev/null
+++ b/vector/src/main/java/im/vector/app/features/settings/notifications/EnableNotificationsForCurrentSessionUseCase.kt
@@ -0,0 +1,72 @@
+/*
+ * Copyright (c) 2022 New Vector Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package im.vector.app.features.settings.notifications
+
+import androidx.fragment.app.FragmentActivity
+import im.vector.app.core.di.ActiveSessionHolder
+import im.vector.app.core.pushers.FcmHelper
+import im.vector.app.core.pushers.PushersManager
+import im.vector.app.core.pushers.UnifiedPushHelper
+import im.vector.app.features.settings.devices.v2.notification.CheckIfCanTogglePushNotificationsViaPusherUseCase
+import im.vector.app.features.settings.devices.v2.notification.TogglePushNotificationUseCase
+import javax.inject.Inject
+import kotlin.coroutines.resume
+import kotlin.coroutines.resumeWithException
+import kotlin.coroutines.suspendCoroutine
+
+class EnableNotificationsForCurrentSessionUseCase @Inject constructor(
+ private val activeSessionHolder: ActiveSessionHolder,
+ private val unifiedPushHelper: UnifiedPushHelper,
+ private val pushersManager: PushersManager,
+ private val fcmHelper: FcmHelper,
+ private val checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase,
+ private val togglePushNotificationUseCase: TogglePushNotificationUseCase,
+) {
+
+ // TODO add unit tests
+ suspend fun execute(fragmentActivity: FragmentActivity) {
+ val pusherForCurrentSession = pushersManager.getPusherForCurrentSession()
+ if (pusherForCurrentSession == null) {
+ registerPusher(fragmentActivity)
+ }
+
+ if (checkIfCanTogglePushNotificationsViaPusherUseCase.execute()) {
+ val session = activeSessionHolder.getSafeActiveSession() ?: return
+ val deviceId = session.sessionParams.deviceId ?: return
+ togglePushNotificationUseCase.execute(deviceId, enabled = true)
+ }
+ }
+
+ private suspend fun registerPusher(fragmentActivity: FragmentActivity) {
+ suspendCoroutine { continuation ->
+ try {
+ unifiedPushHelper.register(fragmentActivity) {
+ if (unifiedPushHelper.isEmbeddedDistributor()) {
+ fcmHelper.ensureFcmTokenIsRetrieved(
+ fragmentActivity,
+ pushersManager,
+ registerPusher = true
+ )
+ }
+ continuation.resume(Unit)
+ }
+ } catch (error: Exception) {
+ continuation.resumeWithException(error)
+ }
+ }
+ }
+}
diff --git a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
index f800c518f3..4a43a20de3 100644
--- a/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
+++ b/vector/src/main/java/im/vector/app/features/settings/notifications/VectorSettingsNotificationPreferenceFragment.kt
@@ -57,7 +57,6 @@ import im.vector.app.features.settings.VectorSettingsBaseFragment
import im.vector.app.features.settings.VectorSettingsFragmentInteractionListener
import im.vector.lib.core.utils.compat.getParcelableExtraCompat
import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.tryOrNull
import org.matrix.android.sdk.api.session.Session
@@ -81,6 +80,8 @@ class VectorSettingsNotificationPreferenceFragment :
@Inject lateinit var guardServiceStarter: GuardServiceStarter
@Inject lateinit var vectorFeatures: VectorFeatures
@Inject lateinit var notificationPermissionManager: NotificationPermissionManager
+ @Inject lateinit var disableNotificationsForCurrentSessionUseCase: DisableNotificationsForCurrentSessionUseCase
+ @Inject lateinit var enableNotificationsForCurrentSessionUseCase: EnableNotificationsForCurrentSessionUseCase
override var titleRes: Int = R.string.settings_notifications
override val preferenceXmlRes = R.xml.vector_settings_notifications
@@ -126,28 +127,12 @@ class VectorSettingsNotificationPreferenceFragment :
it.setTransactionalSwitchChangeListener(lifecycleScope) { isChecked ->
if (isChecked) {
- unifiedPushHelper.register(requireActivity()) {
- // Update the summary
- if (unifiedPushHelper.isEmbeddedDistributor()) {
- fcmHelper.ensureFcmTokenIsRetrieved(
- requireActivity(),
- pushersManager,
- vectorPreferences.areNotificationEnabledForDevice()
- )
- }
- findPreference(VectorPreferences.SETTINGS_NOTIFICATION_METHOD_KEY)
- ?.summary = unifiedPushHelper.getCurrentDistributorName()
- lifecycleScope.launch {
- val result = runCatching {
- pushersManager.togglePusherForCurrentSession(true)
- }
+ enableNotificationsForCurrentSessionUseCase.execute(requireActivity())
- result.exceptionOrNull()?.let { _ ->
- Toast.makeText(context, R.string.error_check_network, Toast.LENGTH_SHORT).show()
- it.isChecked = false
- }
- }
- }
+ findPreference(VectorPreferences.SETTINGS_NOTIFICATION_METHOD_KEY)
+ ?.summary = unifiedPushHelper.getCurrentDistributorName()
+
+ // TODO test with API 33
notificationPermissionManager.eventuallyRequestPermission(
requireActivity(),
postPermissionLauncher,
@@ -155,8 +140,7 @@ class VectorSettingsNotificationPreferenceFragment :
ignorePreference = true
)
} else {
- unifiedPushHelper.unregister(pushersManager)
- session.pushersService().refreshPushers()
+ disableNotificationsForCurrentSessionUseCase.execute()
notificationPermissionManager.eventuallyRevokePermission(requireActivity())
}
}