Ensure that the Loading Dialog and the toggles update at the same time.

This commit is contained in:
Benoit Marty 2024-06-12 11:38:24 +02:00 committed by Benoit Marty
parent 55a1ac4bb5
commit 4dff3e9cce
3 changed files with 48 additions and 19 deletions

View file

@ -29,7 +29,8 @@ import androidx.compose.runtime.setValue
import io.element.android.libraries.architecture.AsyncAction import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.architecture.AsyncData import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.architecture.runCatchingUpdatingState import io.element.android.libraries.architecture.runUpdatingState
import io.element.android.libraries.architecture.runUpdatingStateNoSuccess
import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService
import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.room.RoomNotificationMode
@ -74,7 +75,7 @@ class NotificationSettingsPresenter @Inject constructor(
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
fetchSettings(matrixSettings) fetchSettings(matrixSettings)
observeNotificationSettings(matrixSettings) observeNotificationSettings(matrixSettings, changeNotificationSettingAction)
} }
// List of PushProvider -> Distributor // List of PushProvider -> Distributor
@ -172,11 +173,15 @@ class NotificationSettingsPresenter @Inject constructor(
} }
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
private fun CoroutineScope.observeNotificationSettings(target: MutableState<NotificationSettingsState.MatrixSettings>) { private fun CoroutineScope.observeNotificationSettings(
target: MutableState<NotificationSettingsState.MatrixSettings>,
changeNotificationSettingAction: MutableState<AsyncAction<Unit>>,
) {
notificationSettingsService.notificationSettingsChangeFlow notificationSettingsService.notificationSettingsChangeFlow
.debounce(0.5.seconds) .debounce(0.5.seconds)
.onEach { .onEach {
fetchSettings(target) fetchSettings(target)
changeNotificationSettingAction.value = AsyncAction.Uninitialized
} }
.launchIn(this) .launchIn(this)
} }
@ -238,21 +243,21 @@ class NotificationSettingsPresenter @Inject constructor(
} }
private fun CoroutineScope.setAtRoomNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch { private fun CoroutineScope.setAtRoomNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch {
suspend { action.runUpdatingStateNoSuccess {
notificationSettingsService.setRoomMentionEnabled(enabled).getOrThrow() notificationSettingsService.setRoomMentionEnabled(enabled)
}.runCatchingUpdatingState(action) }
} }
private fun CoroutineScope.setCallNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch { private fun CoroutineScope.setCallNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch {
suspend { action.runUpdatingStateNoSuccess {
notificationSettingsService.setCallEnabled(enabled).getOrThrow() notificationSettingsService.setCallEnabled(enabled)
}.runCatchingUpdatingState(action) }
} }
private fun CoroutineScope.setInviteForMeNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch { private fun CoroutineScope.setInviteForMeNotificationsEnabled(enabled: Boolean, action: MutableState<AsyncAction<Unit>>) = launch {
suspend { action.runUpdatingStateNoSuccess {
notificationSettingsService.setInviteForMeEnabled(enabled).getOrThrow() notificationSettingsService.setInviteForMeEnabled(enabled)
}.runCatchingUpdatingState(action) }
} }
private fun CoroutineScope.setNotificationsEnabled(userPushStore: UserPushStore, enabled: Boolean) = launch { private fun CoroutineScope.setNotificationsEnabled(userPushStore: UserPushStore, enabled: Boolean) = launch {

View file

@ -29,7 +29,7 @@ import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import io.element.android.libraries.architecture.AsyncAction import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.architecture.runCatchingUpdatingState import io.element.android.libraries.architecture.runUpdatingStateNoSuccess
import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService
import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.room.RoomNotificationMode
@ -73,7 +73,7 @@ class EditDefaultNotificationSettingPresenter @AssistedInject constructor(
val localCoroutineScope = rememberCoroutineScope() val localCoroutineScope = rememberCoroutineScope()
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
fetchSettings(mode) fetchSettings(mode)
observeNotificationSettings(mode) observeNotificationSettings(mode, changeNotificationSettingAction)
observeRoomSummaries(roomsWithUserDefinedMode) observeRoomSummaries(roomsWithUserDefinedMode)
displayMentionsOnlyDisclaimer = !notificationSettingsService.canHomeServerPushEncryptedEventsToDevice().getOrDefault(true) displayMentionsOnlyDisclaimer = !notificationSettingsService.canHomeServerPushEncryptedEventsToDevice().getOrDefault(true)
} }
@ -102,11 +102,15 @@ class EditDefaultNotificationSettingPresenter @AssistedInject constructor(
} }
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
private fun CoroutineScope.observeNotificationSettings(mode: MutableState<RoomNotificationMode?>) { private fun CoroutineScope.observeNotificationSettings(
mode: MutableState<RoomNotificationMode?>,
changeNotificationSettingAction: MutableState<AsyncAction<Unit>>,
) {
notificationSettingsService.notificationSettingsChangeFlow notificationSettingsService.notificationSettingsChangeFlow
.debounce(0.5.seconds) .debounce(0.5.seconds)
.onEach { .onEach {
fetchSettings(mode) fetchSettings(mode)
changeNotificationSettingAction.value = AsyncAction.Uninitialized
} }
.launchIn(this) .launchIn(this)
} }
@ -139,10 +143,12 @@ class EditDefaultNotificationSettingPresenter @AssistedInject constructor(
} }
private fun CoroutineScope.setDefaultNotificationMode(mode: RoomNotificationMode, action: MutableState<AsyncAction<Unit>>) = launch { private fun CoroutineScope.setDefaultNotificationMode(mode: RoomNotificationMode, action: MutableState<AsyncAction<Unit>>) = launch {
suspend { action.runUpdatingStateNoSuccess {
// On modern clients, we don't have different settings for encrypted and non-encrypted rooms (Legacy clients did). // On modern clients, we don't have different settings for encrypted and non-encrypted rooms (Legacy clients did).
notificationSettingsService.setDefaultRoomNotificationMode(isEncrypted = true, mode = mode, isOneToOne = isOneToOne).getOrThrow() notificationSettingsService.setDefaultRoomNotificationMode(isEncrypted = true, mode = mode, isOneToOne = isOneToOne)
notificationSettingsService.setDefaultRoomNotificationMode(isEncrypted = false, mode = mode, isOneToOne = isOneToOne).getOrThrow() .map {
}.runCatchingUpdatingState(action) notificationSettingsService.setDefaultRoomNotificationMode(isEncrypted = false, mode = mode, isOneToOne = isOneToOne)
}
}
} }
} }

View file

@ -125,6 +125,24 @@ suspend inline fun <T> MutableState<AsyncAction<T>>.runUpdatingState(
resultBlock = resultBlock, resultBlock = resultBlock,
) )
/**
* Run the given block and update the state accordingly, using only Loading and Failure states.
* It's up to the caller to manage the Success state.
*/
@OptIn(ExperimentalContracts::class)
inline fun <T> MutableState<AsyncAction<T>>.runUpdatingStateNoSuccess(
resultBlock: () -> Result<Unit>,
): Result<Unit> {
contract {
callsInPlace(resultBlock, InvocationKind.EXACTLY_ONCE)
}
value = AsyncAction.Loading
return resultBlock()
.onFailure { failure ->
value = AsyncAction.Failure(failure)
}
}
/** /**
* Calls the specified [Result]-returning function [resultBlock] * Calls the specified [Result]-returning function [resultBlock]
* encapsulating its progress and return value into an [AsyncAction] while * encapsulating its progress and return value into an [AsyncAction] while