Timeline: handle loadMore pre-loading
This commit is contained in:
parent
42982d2238
commit
8d87578890
1 changed files with 28 additions and 6 deletions
|
|
@ -39,6 +39,7 @@ import io.element.android.x.features.messages.model.content.*
|
||||||
import io.element.android.x.features.messages.textcomposer.MessageComposerViewModel
|
import io.element.android.x.features.messages.textcomposer.MessageComposerViewModel
|
||||||
import io.element.android.x.features.messages.textcomposer.MessageComposerViewState
|
import io.element.android.x.features.messages.textcomposer.MessageComposerViewState
|
||||||
import io.element.android.x.textcomposer.TextComposer
|
import io.element.android.x.textcomposer.TextComposer
|
||||||
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
|
|
@ -279,11 +280,15 @@ fun TimelineItems(
|
||||||
}
|
}
|
||||||
if (hasMoreToLoad) {
|
if (hasMoreToLoad) {
|
||||||
item {
|
item {
|
||||||
MessagesLoadingMoreIndicator(onReachedLoadMore)
|
MessagesLoadingMoreIndicator()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
MessagesScrollHelper(lazyListState = lazyListState, timelineItems = timelineItems)
|
MessagesScrollHelper(
|
||||||
|
lazyListState = lazyListState,
|
||||||
|
timelineItems = timelineItems,
|
||||||
|
onLoadMore = onReachedLoadMore
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -420,10 +425,12 @@ private fun MessageSenderInformation(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal fun BoxScope.MessagesScrollHelper(
|
internal fun BoxScope.MessagesScrollHelper(
|
||||||
lazyListState: LazyListState,
|
lazyListState: LazyListState,
|
||||||
timelineItems: List<MessagesTimelineItemState>,
|
timelineItems: List<MessagesTimelineItemState>,
|
||||||
|
onLoadMore: () -> Unit,
|
||||||
) {
|
) {
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val firstVisibleItemIndex by remember { derivedStateOf { lazyListState.firstVisibleItemIndex } }
|
val firstVisibleItemIndex by remember { derivedStateOf { lazyListState.firstVisibleItemIndex } }
|
||||||
|
|
@ -437,6 +444,24 @@ internal fun BoxScope.MessagesScrollHelper(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle load more preloading
|
||||||
|
val loadMore by remember {
|
||||||
|
derivedStateOf {
|
||||||
|
val layoutInfo = lazyListState.layoutInfo
|
||||||
|
val totalItemsNumber = layoutInfo.totalItemsCount
|
||||||
|
val lastVisibleItemIndex = (layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: 0) + 1
|
||||||
|
lastVisibleItemIndex > (totalItemsNumber - 30)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LaunchedEffect(loadMore) {
|
||||||
|
snapshotFlow { loadMore }
|
||||||
|
.distinctUntilChanged()
|
||||||
|
.collect {
|
||||||
|
onLoadMore()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Jump to bottom button
|
// Jump to bottom button
|
||||||
if (firstVisibleItemIndex > 2) {
|
if (firstVisibleItemIndex > 2) {
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
|
|
@ -463,7 +488,7 @@ internal fun BoxScope.MessagesScrollHelper(
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
internal fun MessagesLoadingMoreIndicator(onReachedLoadMore: () -> Unit) {
|
internal fun MessagesLoadingMoreIndicator() {
|
||||||
Box(
|
Box(
|
||||||
Modifier
|
Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
|
|
@ -475,9 +500,6 @@ internal fun MessagesLoadingMoreIndicator(onReachedLoadMore: () -> Unit) {
|
||||||
strokeWidth = 2.dp,
|
strokeWidth = 2.dp,
|
||||||
color = MaterialTheme.colorScheme.primary
|
color = MaterialTheme.colorScheme.primary
|
||||||
)
|
)
|
||||||
LaunchedEffect(Unit) {
|
|
||||||
onReachedLoadMore()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue