From 675b3ab3837e94bbe89447db9f40910ee186399e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Thu, 17 Aug 2023 15:23:34 +0200 Subject: [PATCH] Add debouncing to `observeNotificationSettings`. This should conceal a bit the issue with notification settings 'flashing' when we receive an update. --- .../RoomNotificationSettingsPresenter.kt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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 13b0e00b1e..d5c88e53d4 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 @@ -30,10 +30,14 @@ import io.element.android.libraries.matrix.api.room.MatrixRoom import io.element.android.libraries.matrix.api.room.RoomNotificationMode import io.element.android.libraries.matrix.api.room.roomNotificationSettings import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.FlowPreview +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch +import timber.log.Timber import javax.inject.Inject +import kotlin.time.Duration.Companion.seconds class RoomNotificationSettingsPresenter @Inject constructor( private val room: MatrixRoom, @@ -71,6 +75,8 @@ class RoomNotificationSettingsPresenter @Inject constructor( } } + Timber.d("NotifState: $roomNotificationSettingsState") + return RoomNotificationSettingsState( roomNotificationSettings = roomNotificationSettingsState.roomNotificationSettings(), defaultRoomNotificationMode = defaultRoomNotificationMode.value, @@ -78,10 +84,14 @@ class RoomNotificationSettingsPresenter @Inject constructor( ) } + @OptIn(FlowPreview::class) private fun CoroutineScope.observeNotificationSettings() { - notificationSettingsService.notificationSettingsChangeFlow.onEach { - room.updateRoomNotificationSettings() - }.launchIn(this) + notificationSettingsService.notificationSettingsChangeFlow + .debounce(0.5.seconds) + .onEach { + room.updateRoomNotificationSettings() + } + .launchIn(this) } private fun CoroutineScope.getDefaultRoomNotificationMode(defaultRoomNotificationMode: MutableState) = launch {