From e7228b94607e5ae26c7b278ddef00cc03cd659cf Mon Sep 17 00:00:00 2001 From: ganfra Date: Mon, 2 Sep 2024 14:06:05 +0200 Subject: [PATCH] Pinned events : add debounce on list --- .../impl/pinned/banner/PinnedMessagesBannerPresenter.kt | 4 ++-- .../impl/pinned/list/PinnedMessagesListPresenter.kt | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/banner/PinnedMessagesBannerPresenter.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/banner/PinnedMessagesBannerPresenter.kt index b577995acb..4c9238da00 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/banner/PinnedMessagesBannerPresenter.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/banner/PinnedMessagesBannerPresenter.kt @@ -46,7 +46,7 @@ import kotlin.time.Duration.Companion.milliseconds class PinnedMessagesBannerPresenter @Inject constructor( private val room: MatrixRoom, private val itemFactory: PinnedMessagesBannerItemFactory, - private val timelineController: PinnedEventsTimelineProvider, + private val pinnedEventsTimelineProvider: PinnedEventsTimelineProvider, ) : Presenter { private val pinnedItems = mutableStateOf>>(AsyncData.Uninitialized) @@ -124,7 +124,7 @@ class PinnedMessagesBannerPresenter @Inject constructor( ) { val updatedOnItemsChange by rememberUpdatedState(onItemsChange) LaunchedEffect(Unit) { - timelineController.timelineStateFlow + pinnedEventsTimelineProvider.timelineStateFlow .flatMapLatest { asyncTimeline -> when (asyncTimeline) { AsyncData.Uninitialized -> flowOf(AsyncData.Uninitialized) diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/list/PinnedMessagesListPresenter.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/list/PinnedMessagesListPresenter.kt index 5e61b4903b..b3fb8b8927 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/list/PinnedMessagesListPresenter.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/pinned/list/PinnedMessagesListPresenter.kt @@ -50,13 +50,16 @@ import io.element.android.libraries.matrix.api.room.roomMembers import io.element.android.libraries.ui.strings.CommonStrings import kotlinx.collections.immutable.ImmutableList import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import timber.log.Timber +import kotlin.time.Duration.Companion.milliseconds class PinnedMessagesListPresenter @AssistedInject constructor( @Assisted private val navigator: PinnedMessagesListNavigator, @@ -171,6 +174,7 @@ class PinnedMessagesListPresenter @AssistedInject constructor( } } + @OptIn(FlowPreview::class) @Composable private fun PinnedMessagesListEffect(onItemsChange: (AsyncData>) -> Unit) { val updatedOnItemsChange by rememberUpdatedState(onItemsChange) @@ -183,7 +187,8 @@ class PinnedMessagesListPresenter @AssistedInject constructor( is AsyncData.Failure -> flowOf(AsyncData.Failure(asyncTimeline.error)) is AsyncData.Loading -> flowOf(AsyncData.Loading()) is AsyncData.Success -> { - combine(asyncTimeline.data.timelineItems, room.membersStateFlow) { items, membersState -> + val timelineItemsFlow = asyncTimeline.data.timelineItems.debounce(300.milliseconds) + combine(timelineItemsFlow, room.membersStateFlow) { items, membersState -> timelineItemsFactory.replaceWith( timelineItems = items, roomMembers = membersState.roomMembers().orEmpty()