From 4c4f2fce74d3b0f0dc4fe6136b40b9e440e90116 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Thu, 23 Sep 2021 12:27:56 +0100 Subject: [PATCH] catching remove pusher errors in the notification targets screen - displays a dialog with a human readable version of the error --- .../settings/push/PushGatewayViewEvents.kt | 23 +++++++++++++++++++ .../settings/push/PushGatewaysFragment.kt | 13 +++++++++++ .../settings/push/PushGatewaysViewModel.kt | 9 ++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 vector/src/main/java/im/vector/app/features/settings/push/PushGatewayViewEvents.kt diff --git a/vector/src/main/java/im/vector/app/features/settings/push/PushGatewayViewEvents.kt b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewayViewEvents.kt new file mode 100644 index 0000000000..8b2a833b5c --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewayViewEvents.kt @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 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.push + +import im.vector.app.core.platform.VectorViewEvents + +sealed class PushGatewayViewEvents : VectorViewEvents { + data class RemovePusherFailed(val cause: Throwable): PushGatewayViewEvents() +} diff --git a/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysFragment.kt b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysFragment.kt index 696505cc52..9602804bf8 100644 --- a/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysFragment.kt @@ -24,9 +24,11 @@ import android.view.ViewGroup import androidx.appcompat.app.AppCompatActivity import com.airbnb.mvrx.fragmentViewModel import com.airbnb.mvrx.withState +import com.google.android.material.dialog.MaterialAlertDialogBuilder import im.vector.app.R import im.vector.app.core.extensions.cleanup import im.vector.app.core.extensions.configureWith +import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.VectorBaseFragment import im.vector.app.databinding.FragmentGenericRecyclerBinding import org.matrix.android.sdk.api.session.pushers.Pusher @@ -69,6 +71,17 @@ class PushGatewaysFragment @Inject constructor( override fun onRemovePushTapped(pusher: Pusher) = viewModel.handle(PushGatewayAction.RemovePusher(pusher)) } views.genericRecyclerView.configureWith(epoxyController, dividerDrawable = R.drawable.divider_horizontal) + viewModel.observeViewEvents { + when(it) { + is PushGatewayViewEvents.RemovePusherFailed -> { + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.dialog_title_error) + .setMessage(errorFormatter.toHumanReadable(it.cause)) + .setPositiveButton(android.R.string.ok, null) + .show() + } + }.exhaustive + } } override fun onDestroyView() { diff --git a/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysViewModel.kt b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysViewModel.kt index c3f799730c..20c0605fb3 100644 --- a/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/settings/push/PushGatewaysViewModel.kt @@ -29,6 +29,7 @@ import dagger.assisted.AssistedFactory import im.vector.app.core.extensions.exhaustive import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel +import im.vector.app.features.home.HomeActivityViewEvents import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.pushers.Pusher @@ -40,7 +41,7 @@ data class PushGatewayViewState( class PushGatewaysViewModel @AssistedInject constructor(@Assisted initialState: PushGatewayViewState, private val session: Session) - : VectorViewModel(initialState) { + : VectorViewModel(initialState) { @AssistedFactory interface Factory { @@ -79,7 +80,11 @@ class PushGatewaysViewModel @AssistedInject constructor(@Assisted initialState: private fun removePusher(pusher: Pusher) { viewModelScope.launch { - session.removePusher(pusher.pushKey, pusher.appId) + kotlin.runCatching { + session.removePusher(pusher.pushKey, pusher.appId) + }.onFailure { + _viewEvents.post(PushGatewayViewEvents.RemovePusherFailed(it)) + } } }