From 306b779ca5cad9c46457582d44339bfb6aea91fd Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 28 May 2024 14:18:00 +0200 Subject: [PATCH] Change type of `NotificationSettingsState.currentPushDistributor` from `AsyncAction` to `AsyncData` --- .../notifications/NotificationSettingsPresenter.kt | 11 ++++++----- .../impl/notifications/NotificationSettingsState.kt | 3 ++- .../NotificationSettingsStateProvider.kt | 9 +++++---- .../impl/notifications/NotificationSettingsView.kt | 11 +++++------ .../android/libraries/architecture/AsyncData.kt | 2 ++ 5 files changed, 20 insertions(+), 16 deletions(-) diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsPresenter.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsPresenter.kt index 3eb8739a99..5cfc14545e 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsPresenter.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsPresenter.kt @@ -27,6 +27,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import io.element.android.libraries.architecture.AsyncAction +import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.architecture.runCatchingUpdatingState import io.element.android.libraries.matrix.api.MatrixClient @@ -90,16 +91,16 @@ class NotificationSettingsPresenter @Inject constructor( distributors.map { it.second.name }.toImmutableList() } - var currentDistributorName by remember { mutableStateOf>(AsyncAction.Uninitialized) } + var currentDistributorName by remember { mutableStateOf>(AsyncData.Uninitialized) } var refreshPushProvider by remember { mutableIntStateOf(0) } LaunchedEffect(refreshPushProvider) { val p = pushService.getCurrentPushProvider() val name = p?.getCurrentDistributor(matrixClient)?.name currentDistributorName = if (name != null) { - AsyncAction.Success(name) + AsyncData.Success(name) } else { - AsyncAction.Failure(Exception("Failed to get current push provider")) + AsyncData.Failure(Exception("Failed to get current push provider")) } } @@ -112,7 +113,7 @@ class NotificationSettingsPresenter @Inject constructor( data ?: return@launch // No op if the value is the same. if (data.second.name == currentDistributorName.dataOrNull()) return@launch - currentDistributorName = AsyncAction.Loading + currentDistributorName = AsyncData.Loading(currentDistributorName.dataOrNull()) data.let { (pushProvider, distributor) -> pushService.registerWith( matrixClient = matrixClient, @@ -124,7 +125,7 @@ class NotificationSettingsPresenter @Inject constructor( refreshPushProvider++ }, { - currentDistributorName = AsyncAction.Failure(it) + currentDistributorName = AsyncData.Failure(it) } ) } diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsState.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsState.kt index 1d3b326c29..b545213c7a 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsState.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsState.kt @@ -18,6 +18,7 @@ package io.element.android.features.preferences.impl.notifications import androidx.compose.runtime.Immutable import io.element.android.libraries.architecture.AsyncAction +import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.matrix.api.room.RoomNotificationMode import kotlinx.collections.immutable.ImmutableList @@ -26,7 +27,7 @@ data class NotificationSettingsState( val matrixSettings: MatrixSettings, val appSettings: AppSettings, val changeNotificationSettingAction: AsyncAction, - val currentPushDistributor: AsyncAction, + val currentPushDistributor: AsyncData, val availablePushDistributors: ImmutableList, val showChangePushProviderDialog: Boolean, val eventSink: (NotificationSettingsEvents) -> Unit, diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsStateProvider.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsStateProvider.kt index eb1fb98806..5685804e06 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsStateProvider.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsStateProvider.kt @@ -18,6 +18,7 @@ package io.element.android.features.preferences.impl.notifications import androidx.compose.ui.tooling.preview.PreviewParameterProvider import io.element.android.libraries.architecture.AsyncAction +import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.matrix.api.room.RoomNotificationMode import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.toImmutableList @@ -35,8 +36,8 @@ open class NotificationSettingsStateProvider : PreviewParameterProvider = AsyncAction.Success("Firebase"), + currentPushDistributor: AsyncData = AsyncData.Success("Firebase"), availablePushDistributors: List = listOf("Firebase", "ntfy"), showChangePushProviderDialog: Boolean = false, eventSink: (NotificationSettingsEvents) -> Unit = {}, @@ -84,7 +85,7 @@ fun aInvalidNotificationSettingsState( appNotificationsEnabled = true, ), changeNotificationSettingAction = AsyncAction.Uninitialized, - currentPushDistributor = AsyncAction.Uninitialized, + currentPushDistributor = AsyncData.Uninitialized, availablePushDistributors = persistentListOf(), showChangePushProviderDialog = false, eventSink = eventSink, diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsView.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsView.kt index 307f5e9cc5..0594d8ffd3 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsView.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/notifications/NotificationSettingsView.kt @@ -28,7 +28,7 @@ import androidx.lifecycle.Lifecycle import io.element.android.compound.tokens.generated.CompoundIcons import io.element.android.features.preferences.impl.R import io.element.android.libraries.androidutils.system.startNotificationSettingsIntent -import io.element.android.libraries.architecture.AsyncAction +import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.designsystem.atomic.molecules.DialogLikeBannerMolecule import io.element.android.libraries.designsystem.components.async.AsyncActionView import io.element.android.libraries.designsystem.components.dialogs.ErrorDialog @@ -188,9 +188,8 @@ private fun NotificationSettingsContentView( Text(text = stringResource(id = R.string.screen_advanced_settings_push_provider_android)) }, trailingContent = when (state.currentPushDistributor) { - AsyncAction.Uninitialized, - AsyncAction.Confirming, - AsyncAction.Loading -> ListItemContent.Custom { + AsyncData.Uninitialized, + is AsyncData.Loading -> ListItemContent.Custom { CircularProgressIndicator( modifier = Modifier .progressSemantics() @@ -198,10 +197,10 @@ private fun NotificationSettingsContentView( strokeWidth = 2.dp ) } - is AsyncAction.Failure -> ListItemContent.Text( + is AsyncData.Failure -> ListItemContent.Text( stringResource(id = CommonStrings.common_error) ) - is AsyncAction.Success -> ListItemContent.Text( + is AsyncData.Success -> ListItemContent.Text( state.currentPushDistributor.dataOrNull() ?: "" ) }, diff --git a/libraries/architecture/src/main/kotlin/io/element/android/libraries/architecture/AsyncData.kt b/libraries/architecture/src/main/kotlin/io/element/android/libraries/architecture/AsyncData.kt index 75bb503390..dab7a5f3b1 100644 --- a/libraries/architecture/src/main/kotlin/io/element/android/libraries/architecture/AsyncData.kt +++ b/libraries/architecture/src/main/kotlin/io/element/android/libraries/architecture/AsyncData.kt @@ -91,6 +91,8 @@ sealed interface AsyncData { fun isSuccess(): Boolean = this is Success fun isUninitialized(): Boolean = this == Uninitialized + + fun isReady() = isSuccess() || isFailure() } suspend inline fun MutableState>.runCatchingUpdatingState(