From c98863a70ecb4dbd414022a9fea2dde93dfb2cf1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 12 Jun 2024 12:35:20 +0200 Subject: [PATCH] Ensure that setting change is taken at least 300ms to avoid dialog flickering (#1647) --- .../RoomNotificationSettingsPresenter.kt | 5 ++-- .../libraries/core/coroutine/Suspend.kt | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 libraries/core/src/main/kotlin/io/element/android/libraries/core/coroutine/Suspend.kt diff --git a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/notificationsettings/RoomNotificationSettingsPresenter.kt b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/notificationsettings/RoomNotificationSettingsPresenter.kt index 05283c7669..170c0fabba 100644 --- a/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/notificationsettings/RoomNotificationSettingsPresenter.kt +++ b/features/roomdetails/impl/src/main/kotlin/io/element/android/features/roomdetails/impl/notificationsettings/RoomNotificationSettingsPresenter.kt @@ -32,6 +32,7 @@ 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.core.coroutine.suspendWithMinimumDuration import io.element.android.libraries.matrix.api.notificationsettings.NotificationSettingsService import io.element.android.libraries.matrix.api.room.MatrixRoom import io.element.android.libraries.matrix.api.room.RoomNotificationMode @@ -171,7 +172,7 @@ class RoomNotificationSettingsPresenter @AssistedInject constructor( pendingDefaultState: MutableState, action: MutableState> ) = launch { - suspend { + suspendWithMinimumDuration { pendingModeState.value = mode pendingDefaultState.value = false val result = notificationSettingsService.setRoomNotificationMode(room.roomId, mode) @@ -187,7 +188,7 @@ class RoomNotificationSettingsPresenter @AssistedInject constructor( action: MutableState>, pendingDefaultState: MutableState ) = launch { - suspend { + suspendWithMinimumDuration { pendingDefaultState.value = true val result = notificationSettingsService.restoreDefaultRoomNotificationMode(room.roomId) if (result.isFailure) { diff --git a/libraries/core/src/main/kotlin/io/element/android/libraries/core/coroutine/Suspend.kt b/libraries/core/src/main/kotlin/io/element/android/libraries/core/coroutine/Suspend.kt new file mode 100644 index 0000000000..46a52c8443 --- /dev/null +++ b/libraries/core/src/main/kotlin/io/element/android/libraries/core/coroutine/Suspend.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2024 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 io.element.android.libraries.core.coroutine + +import kotlinx.coroutines.delay +import kotlin.system.measureTimeMillis + +fun suspendWithMinimumDuration( + minimumDurationMillis: Long = 500, + block: suspend () -> Unit +) = suspend { + val duration = measureTimeMillis { + block() + } + delay(minimumDurationMillis - duration) +}