mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-01-31 03:17:13 +01:00
Extracting the logic to toggle notifications for device into a ViewModel
This commit is contained in:
parent
b29191e892
commit
3f944e9d36
@ -105,6 +105,7 @@ import im.vector.app.features.settings.ignored.IgnoredUsersViewModel
|
||||
import im.vector.app.features.settings.labs.VectorSettingsLabsViewModel
|
||||
import im.vector.app.features.settings.legals.LegalsViewModel
|
||||
import im.vector.app.features.settings.locale.LocalePickerViewModel
|
||||
import im.vector.app.features.settings.notifications.VectorSettingsNotificationPreferenceViewModel
|
||||
import im.vector.app.features.settings.push.PushGatewaysViewModel
|
||||
import im.vector.app.features.settings.threepids.ThreePidsSettingsViewModel
|
||||
import im.vector.app.features.share.IncomingShareViewModel
|
||||
@ -683,4 +684,9 @@ interface MavericksViewModelModule {
|
||||
@IntoMap
|
||||
@MavericksViewModelKey(AttachmentTypeSelectorViewModel::class)
|
||||
fun attachmentTypeSelectorViewModelFactory(factory: AttachmentTypeSelectorViewModel.Factory): MavericksAssistedViewModelFactory<*, *>
|
||||
|
||||
@Binds
|
||||
@IntoMap
|
||||
@MavericksViewModelKey(VectorSettingsNotificationPreferenceViewModel::class)
|
||||
fun vectorSettingsNotificationPreferenceViewModelFactory(factory: VectorSettingsNotificationPreferenceViewModel.Factory): MavericksAssistedViewModelFactory<*, *>
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ import im.vector.app.R
|
||||
import im.vector.app.core.error.ErrorFormatter
|
||||
import im.vector.app.core.extensions.singletonEntryPoint
|
||||
import im.vector.app.core.platform.VectorBaseActivity
|
||||
import im.vector.app.core.platform.VectorViewEvents
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import im.vector.app.core.utils.toast
|
||||
import im.vector.app.features.analytics.AnalyticsTracker
|
||||
import im.vector.app.features.analytics.plan.MobileScreen
|
||||
@ -60,6 +62,19 @@ abstract class VectorSettingsBaseFragment : PreferenceFragmentCompat(), Maverick
|
||||
protected lateinit var session: Session
|
||||
protected lateinit var errorFormatter: ErrorFormatter
|
||||
|
||||
/* ==========================================================================================
|
||||
* ViewEvents
|
||||
* ========================================================================================== */
|
||||
|
||||
protected fun <T : VectorViewEvents> VectorViewModel<*, *, T>.observeViewEvents(observer: (T) -> Unit) {
|
||||
viewEvents
|
||||
.stream()
|
||||
.onEach {
|
||||
observer(it)
|
||||
}
|
||||
.launchIn(viewLifecycleOwner.lifecycleScope)
|
||||
}
|
||||
|
||||
/* ==========================================================================================
|
||||
* Views
|
||||
* ========================================================================================== */
|
||||
@ -148,7 +163,7 @@ abstract class VectorSettingsBaseFragment : PreferenceFragmentCompat(), Maverick
|
||||
}
|
||||
}
|
||||
|
||||
protected fun displayErrorDialog(throwable: Throwable) {
|
||||
protected fun displayErrorDialog(throwable: Throwable?) {
|
||||
displayErrorDialog(errorFormatter.toHumanReadable(throwable))
|
||||
}
|
||||
|
||||
|
@ -16,52 +16,45 @@
|
||||
|
||||
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.EnsureFcmTokenIsRetrievedUseCase
|
||||
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.core.pushers.RegisterUnifiedPushUseCase
|
||||
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 checkIfCanTogglePushNotificationsViaPusherUseCase: CheckIfCanTogglePushNotificationsViaPusherUseCase,
|
||||
private val togglePushNotificationUseCase: TogglePushNotificationUseCase,
|
||||
private val registerUnifiedPushUseCase: RegisterUnifiedPushUseCase,
|
||||
private val ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase,
|
||||
) {
|
||||
|
||||
sealed interface EnableNotificationsResult {
|
||||
object Success : EnableNotificationsResult
|
||||
object Failure : EnableNotificationsResult
|
||||
data class NeedToAskUserForDistributor(val distributors: List<String>) : EnableNotificationsResult
|
||||
}
|
||||
|
||||
// TODO update unit tests
|
||||
suspend fun execute(fragmentActivity: FragmentActivity) {
|
||||
suspend fun execute(distributor: String = ""): EnableNotificationsResult {
|
||||
val pusherForCurrentSession = pushersManager.getPusherForCurrentSession()
|
||||
if (pusherForCurrentSession == null) {
|
||||
registerPusher(fragmentActivity)
|
||||
}
|
||||
|
||||
val session = activeSessionHolder.getSafeActiveSession() ?: return
|
||||
if (checkIfCanTogglePushNotificationsViaPusherUseCase.execute(session)) {
|
||||
val deviceId = session.sessionParams.deviceId ?: return
|
||||
togglePushNotificationUseCase.execute(deviceId, enabled = true)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun registerPusher(fragmentActivity: FragmentActivity) {
|
||||
suspendCoroutine { continuation ->
|
||||
try {
|
||||
unifiedPushHelper.register(fragmentActivity) {
|
||||
ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = true)
|
||||
continuation.resume(Unit)
|
||||
when (val result = registerUnifiedPushUseCase.execute(distributor)) {
|
||||
is RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.NeedToAskUserForDistributor -> {
|
||||
return EnableNotificationsResult.NeedToAskUserForDistributor(result.distributors)
|
||||
}
|
||||
RegisterUnifiedPushUseCase.RegisterUnifiedPushResult.Success -> {
|
||||
ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = true)
|
||||
}
|
||||
} catch (error: Exception) {
|
||||
continuation.resumeWithException(error)
|
||||
}
|
||||
}
|
||||
|
||||
val session = activeSessionHolder.getSafeActiveSession() ?: return EnableNotificationsResult.Failure
|
||||
val deviceId = session.sessionParams.deviceId ?: return EnableNotificationsResult.Failure
|
||||
togglePushNotificationUseCase.execute(deviceId, enabled = true)
|
||||
|
||||
return EnableNotificationsResult.Success
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ import android.content.Intent
|
||||
import android.media.RingtoneManager
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.distinctUntilChanged
|
||||
@ -29,6 +30,7 @@ import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.map
|
||||
import androidx.preference.Preference
|
||||
import androidx.preference.SwitchPreference
|
||||
import com.airbnb.mvrx.fragmentViewModel
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import im.vector.app.R
|
||||
import im.vector.app.core.di.ActiveSessionHolder
|
||||
@ -81,8 +83,6 @@ 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
|
||||
@Inject lateinit var ensureFcmTokenIsRetrievedUseCase: EnsureFcmTokenIsRetrievedUseCase
|
||||
|
||||
override var titleRes: Int = R.string.settings_notifications
|
||||
@ -90,6 +90,8 @@ class VectorSettingsNotificationPreferenceFragment :
|
||||
|
||||
private var interactionListener: VectorSettingsFragmentInteractionListener? = null
|
||||
|
||||
private val viewModel: VectorSettingsNotificationPreferenceViewModel by fragmentViewModel()
|
||||
|
||||
private val notificationStartForActivityResult = registerStartForActivityResult { _ ->
|
||||
// No op
|
||||
}
|
||||
@ -106,6 +108,22 @@ class VectorSettingsNotificationPreferenceFragment :
|
||||
analyticsScreenName = MobileScreen.ScreenName.SettingsNotifications
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
observeViewEvents()
|
||||
}
|
||||
|
||||
private fun observeViewEvents() {
|
||||
viewModel.observeViewEvents {
|
||||
when (it) {
|
||||
VectorSettingsNotificationPreferenceViewEvent.NotificationForDeviceEnabled -> onNotificationsForDeviceEnabled()
|
||||
VectorSettingsNotificationPreferenceViewEvent.NotificationForDeviceDisabled -> onNotificationsForDeviceDisabled()
|
||||
is VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor -> askUserToSelectPushDistributor(it.distributors)
|
||||
VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure -> displayErrorDialog(throwable = null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun bindPref() {
|
||||
findPreference<VectorSwitchPreference>(VectorPreferences.SETTINGS_ENABLE_ALL_NOTIF_PREFERENCE_KEY)!!.let { pref ->
|
||||
val pushRuleService = session.pushRuleService()
|
||||
@ -123,23 +141,15 @@ class VectorSettingsNotificationPreferenceFragment :
|
||||
}
|
||||
|
||||
findPreference<SwitchPreference>(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
|
||||
?.setTransactionalSwitchChangeListener(lifecycleScope) { isChecked ->
|
||||
if (isChecked) {
|
||||
enableNotificationsForCurrentSessionUseCase.execute(requireActivity())
|
||||
|
||||
findPreference<VectorPreference>(VectorPreferences.SETTINGS_NOTIFICATION_METHOD_KEY)
|
||||
?.summary = unifiedPushHelper.getCurrentDistributorName()
|
||||
|
||||
notificationPermissionManager.eventuallyRequestPermission(
|
||||
requireActivity(),
|
||||
postPermissionLauncher,
|
||||
showRationale = false,
|
||||
ignorePreference = true
|
||||
)
|
||||
?.setOnPreferenceChangeListener { _, isChecked ->
|
||||
val action = if (isChecked as Boolean) {
|
||||
VectorSettingsNotificationPreferenceViewAction.EnableNotificationsForDevice(pushDistributor = "")
|
||||
} else {
|
||||
disableNotificationsForCurrentSessionUseCase.execute()
|
||||
notificationPermissionManager.eventuallyRevokePermission(requireActivity())
|
||||
VectorSettingsNotificationPreferenceViewAction.DisableNotificationsForDevice
|
||||
}
|
||||
viewModel.handle(action)
|
||||
// preference will be updated on ViewEvent reception
|
||||
false
|
||||
}
|
||||
|
||||
findPreference<VectorPreference>(VectorPreferences.SETTINGS_FDROID_BACKGROUND_SYNC_MODE)?.let {
|
||||
@ -184,6 +194,8 @@ class VectorSettingsNotificationPreferenceFragment :
|
||||
if (vectorFeatures.allowExternalUnifiedPushDistributors()) {
|
||||
it.summary = unifiedPushHelper.getCurrentDistributorName()
|
||||
it.onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||
// TODO show dialog to pick a distributor
|
||||
// TODO call unregister then register only when a new distributor has been selected => use UnifiedPushHelper method
|
||||
unifiedPushHelper.forceRegister(requireActivity(), pushersManager) {
|
||||
ensureFcmTokenIsRetrievedUseCase.execute(pushersManager, registerPusher = vectorPreferences.areNotificationEnabledForDevice())
|
||||
it.summary = unifiedPushHelper.getCurrentDistributorName()
|
||||
@ -203,6 +215,33 @@ class VectorSettingsNotificationPreferenceFragment :
|
||||
handleSystemPreference()
|
||||
}
|
||||
|
||||
private fun onNotificationsForDeviceEnabled() {
|
||||
findPreference<SwitchPreference>(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
|
||||
?.isChecked = true
|
||||
findPreference<VectorPreference>(VectorPreferences.SETTINGS_NOTIFICATION_METHOD_KEY)
|
||||
?.summary = unifiedPushHelper.getCurrentDistributorName()
|
||||
|
||||
notificationPermissionManager.eventuallyRequestPermission(
|
||||
requireActivity(),
|
||||
postPermissionLauncher,
|
||||
showRationale = false,
|
||||
ignorePreference = true
|
||||
)
|
||||
}
|
||||
|
||||
private fun onNotificationsForDeviceDisabled() {
|
||||
findPreference<SwitchPreference>(VectorPreferences.SETTINGS_ENABLE_THIS_DEVICE_PREFERENCE_KEY)
|
||||
?.isChecked = false
|
||||
notificationPermissionManager.eventuallyRevokePermission(requireActivity())
|
||||
}
|
||||
|
||||
// TODO add an argument to know if unregister should be called
|
||||
private fun askUserToSelectPushDistributor(distributors: List<String>) {
|
||||
unifiedPushHelper.showSelectDistributorDialog(requireContext(), distributors) { selection ->
|
||||
viewModel.handle(VectorSettingsNotificationPreferenceViewAction.RegisterPushDistributor(selection))
|
||||
}
|
||||
}
|
||||
|
||||
private fun bindEmailNotifications() {
|
||||
val initialEmails = session.getEmailsWithPushInformation()
|
||||
bindEmailNotificationCategory(initialEmails)
|
||||
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.platform.VectorViewModelAction
|
||||
|
||||
sealed interface VectorSettingsNotificationPreferenceViewAction : VectorViewModelAction {
|
||||
data class EnableNotificationsForDevice(val pushDistributor: String) : VectorSettingsNotificationPreferenceViewAction
|
||||
object DisableNotificationsForDevice : VectorSettingsNotificationPreferenceViewAction
|
||||
data class RegisterPushDistributor(val pushDistributor: String) : VectorSettingsNotificationPreferenceViewAction
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.platform.VectorViewEvents
|
||||
|
||||
sealed interface VectorSettingsNotificationPreferenceViewEvent : VectorViewEvents {
|
||||
object NotificationForDeviceEnabled : VectorSettingsNotificationPreferenceViewEvent
|
||||
object EnableNotificationForDeviceFailure : VectorSettingsNotificationPreferenceViewEvent
|
||||
object NotificationForDeviceDisabled : VectorSettingsNotificationPreferenceViewEvent
|
||||
data class AskUserForPushDistributor(val distributors: List<String>) : VectorSettingsNotificationPreferenceViewEvent
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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 com.airbnb.mvrx.MavericksViewModelFactory
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
import im.vector.app.core.di.MavericksAssistedViewModelFactory
|
||||
import im.vector.app.core.di.hiltMavericksViewModelFactory
|
||||
import im.vector.app.core.platform.VectorDummyViewState
|
||||
import im.vector.app.core.platform.VectorViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class VectorSettingsNotificationPreferenceViewModel @AssistedInject constructor(
|
||||
@Assisted initialState: VectorDummyViewState,
|
||||
private val enableNotificationsForCurrentSessionUseCase: EnableNotificationsForCurrentSessionUseCase,
|
||||
private val disableNotificationsForCurrentSessionUseCase: DisableNotificationsForCurrentSessionUseCase,
|
||||
) : VectorViewModel<VectorDummyViewState, VectorSettingsNotificationPreferenceViewAction, VectorSettingsNotificationPreferenceViewEvent>(initialState) {
|
||||
|
||||
@AssistedFactory
|
||||
interface Factory : MavericksAssistedViewModelFactory<VectorSettingsNotificationPreferenceViewModel, VectorDummyViewState> {
|
||||
override fun create(initialState: VectorDummyViewState): VectorSettingsNotificationPreferenceViewModel
|
||||
}
|
||||
|
||||
companion object : MavericksViewModelFactory<VectorSettingsNotificationPreferenceViewModel, VectorDummyViewState> by hiltMavericksViewModelFactory()
|
||||
|
||||
// TODO add unit tests
|
||||
override fun handle(action: VectorSettingsNotificationPreferenceViewAction) {
|
||||
when (action) {
|
||||
VectorSettingsNotificationPreferenceViewAction.DisableNotificationsForDevice -> handleDisableNotificationsForDevice()
|
||||
is VectorSettingsNotificationPreferenceViewAction.EnableNotificationsForDevice -> handleEnableNotificationsForDevice(action.pushDistributor)
|
||||
is VectorSettingsNotificationPreferenceViewAction.RegisterPushDistributor -> handleRegisterPushDistributor(action.pushDistributor)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleDisableNotificationsForDevice() {
|
||||
viewModelScope.launch {
|
||||
disableNotificationsForCurrentSessionUseCase.execute()
|
||||
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationForDeviceDisabled)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleEnableNotificationsForDevice(distributor: String) {
|
||||
viewModelScope.launch {
|
||||
when (val result = enableNotificationsForCurrentSessionUseCase.execute(distributor)) {
|
||||
EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Failure -> {
|
||||
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.EnableNotificationForDeviceFailure)
|
||||
}
|
||||
is EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.NeedToAskUserForDistributor -> {
|
||||
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.AskUserForPushDistributor(result.distributors))
|
||||
}
|
||||
EnableNotificationsForCurrentSessionUseCase.EnableNotificationsResult.Success -> {
|
||||
_viewEvents.post(VectorSettingsNotificationPreferenceViewEvent.NotificationForDeviceEnabled)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleRegisterPushDistributor(distributor: String) {
|
||||
handleEnableNotificationsForDevice(distributor)
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user