mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-30 02:44:56 +01:00
Adding use cases to handle toggle of push notifications for current session
This commit is contained in:
parent
41ab29d4c0
commit
2941cfa329
@ -1679,7 +1679,8 @@
|
||||
<string name="create_new_room">Create New Room</string>
|
||||
<string name="create_new_space">Create New Space</string>
|
||||
<string name="error_no_network">No network. Please check your Internet connection.</string>
|
||||
<string name="error_check_network">Something went wrong. Please check your network connection and try again.</string>
|
||||
<!-- TODO delete -->
|
||||
<string name="error_check_network" tools:ignore="UnusedResources">Something went wrong. Please check your network connection and try again.</string>
|
||||
<string name="change_room_directory_network">"Change network"</string>
|
||||
<string name="please_wait">"Please wait…"</string>
|
||||
<string name="updating_your_data">Updating your data…</string>
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<VectorPreference>(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<VectorPreference>(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())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user