From f5b495d9ff4e67580f1fd762f0d9007aefbd656a Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 27 May 2024 11:10:43 +0200 Subject: [PATCH] Hide the advance setting section if there is no error and their is only one available push distributor. --- .../NotificationSettingsState.kt | 6 ++ .../NotificationSettingsStateProvider.kt | 7 +- .../notifications/NotificationSettingsView.kt | 86 ++++++++++--------- 3 files changed, 56 insertions(+), 43 deletions(-) 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 0a1c1d76a7..1d3b326c29 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 @@ -50,4 +50,10 @@ data class NotificationSettingsState( val systemNotificationsEnabled: Boolean, val appNotificationsEnabled: Boolean, ) + + /** + * Whether the advanced settings should be shown. + * This is true if the current push distributor is in a failure state or if there are multiple push distributors available. + */ + val showAdvancedSettings: Boolean = currentPushDistributor.isFailure() || availablePushDistributors.size > 1 } 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 6b363fc422..eb1fb98806 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 @@ -25,10 +25,15 @@ import kotlinx.collections.immutable.toImmutableList open class NotificationSettingsStateProvider : PreviewParameterProvider { override val values: Sequence get() = sequenceOf( - aValidNotificationSettingsState(), aValidNotificationSettingsState(systemNotificationsEnabled = false), + aValidNotificationSettingsState(), aValidNotificationSettingsState(changeNotificationSettingAction = AsyncAction.Loading), aValidNotificationSettingsState(changeNotificationSettingAction = AsyncAction.Failure(Throwable("error"))), + aValidNotificationSettingsState( + availablePushDistributors = listOf("Firebase"), + changeNotificationSettingAction = AsyncAction.Failure(Throwable("error")), + ), + aValidNotificationSettingsState(availablePushDistributors = listOf("Firebase")), aValidNotificationSettingsState(showChangePushProviderDialog = true), aValidNotificationSettingsState(currentPushDistributor = AsyncAction.Loading), aValidNotificationSettingsState(currentPushDistributor = AsyncAction.Failure(Exception("Failed to change distributor"))), 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 439b98953f..307f5e9cc5 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 @@ -181,50 +181,52 @@ private fun NotificationSettingsContentView( onClick = onTroubleshootNotificationsClicked ) } - PreferenceCategory(title = stringResource(id = CommonStrings.common_advanced_settings)) { - ListItem( - headlineContent = { - Text(text = stringResource(id = R.string.screen_advanced_settings_push_provider_android)) - }, - trailingContent = when (state.currentPushDistributor) { - AsyncAction.Uninitialized, - AsyncAction.Confirming, - AsyncAction.Loading -> ListItemContent.Custom { - CircularProgressIndicator( - modifier = Modifier - .progressSemantics() - .size(20.dp), - strokeWidth = 2.dp + if (state.showAdvancedSettings) { + PreferenceCategory(title = stringResource(id = CommonStrings.common_advanced_settings)) { + ListItem( + headlineContent = { + Text(text = stringResource(id = R.string.screen_advanced_settings_push_provider_android)) + }, + trailingContent = when (state.currentPushDistributor) { + AsyncAction.Uninitialized, + AsyncAction.Confirming, + AsyncAction.Loading -> ListItemContent.Custom { + CircularProgressIndicator( + modifier = Modifier + .progressSemantics() + .size(20.dp), + strokeWidth = 2.dp + ) + } + is AsyncAction.Failure -> ListItemContent.Text( + stringResource(id = CommonStrings.common_error) ) + is AsyncAction.Success -> ListItemContent.Text( + state.currentPushDistributor.dataOrNull() ?: "" + ) + }, + onClick = { + if (state.currentPushDistributor.isReady()) { + state.eventSink(NotificationSettingsEvents.ChangePushProvider) + } } - is AsyncAction.Failure -> ListItemContent.Text( - stringResource(id = CommonStrings.common_error) - ) - is AsyncAction.Success -> ListItemContent.Text( - state.currentPushDistributor.dataOrNull() ?: "" - ) - }, - onClick = { - if (state.currentPushDistributor.isReady()) { - state.eventSink(NotificationSettingsEvents.ChangePushProvider) - } - } - ) - } - if (state.showChangePushProviderDialog) { - SingleSelectionDialog( - title = stringResource(id = R.string.screen_advanced_settings_choose_distributor_dialog_title_android), - options = state.availablePushDistributors.map { - ListOption(title = it) - }.toImmutableList(), - initialSelection = state.availablePushDistributors.indexOf(state.currentPushDistributor.dataOrNull()), - onOptionSelected = { index -> - state.eventSink( - NotificationSettingsEvents.SetPushProvider(index) - ) - }, - onDismissRequest = { state.eventSink(NotificationSettingsEvents.CancelChangePushProvider) }, - ) + ) + } + if (state.showChangePushProviderDialog) { + SingleSelectionDialog( + title = stringResource(id = R.string.screen_advanced_settings_choose_distributor_dialog_title_android), + options = state.availablePushDistributors.map { + ListOption(title = it) + }.toImmutableList(), + initialSelection = state.availablePushDistributors.indexOf(state.currentPushDistributor.dataOrNull()), + onOptionSelected = { index -> + state.eventSink( + NotificationSettingsEvents.SetPushProvider(index) + ) + }, + onDismissRequest = { state.eventSink(NotificationSettingsEvents.CancelChangePushProvider) }, + ) + } } } }