Hide the advance setting section if there is no error and their is only one available push distributor.

This commit is contained in:
Benoit Marty
2024-05-27 11:10:43 +02:00
committed by Benoit Marty
parent eacc591824
commit f5b495d9ff
3 changed files with 56 additions and 43 deletions

View File

@@ -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
}

View File

@@ -25,10 +25,15 @@ import kotlinx.collections.immutable.toImmutableList
open class NotificationSettingsStateProvider : PreviewParameterProvider<NotificationSettingsState> {
override val values: Sequence<NotificationSettingsState>
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"))),

View File

@@ -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) },
)
}
}
}
}