feat: jump to oldest unread message upon opening a thread, display divider between read/unread (#3693)

This commit is contained in:
Mac DeCourcy 2025-11-14 11:03:46 -08:00 committed by GitHub
parent 427fb33e7e
commit 2a081f3c1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 1322 additions and 209 deletions

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.ui.component
import androidx.compose.foundation.lazy.LazyListState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
private const val SCROLL_TO_TOP_INDEX = 0
private const val FAST_SCROLL_THRESHOLD = 10
/**
* Executes the smart scroll-to-top policy.
*
* Policy:
* - If the first visible item is already at index 0, do nothing.
* - Otherwise, smoothly animate the list back to the first item.
*/
fun LazyListState.smartScrollToTop(coroutineScope: CoroutineScope) {
smartScrollToIndex(coroutineScope = coroutineScope, targetIndex = SCROLL_TO_TOP_INDEX)
}
/**
* Scrolls to the [targetIndex] while applying the same fast-scroll optimisation used by [smartScrollToTop].
*
* If the destination is far away, the list first jumps closer to the goal (within [FAST_SCROLL_THRESHOLD] items) to
* avoid long smooth animations and then animates the final segment.
*
* @param coroutineScope Scope used to perform the scroll operations.
* @param targetIndex Absolute index that should end up at the top of the viewport.
*/
fun LazyListState.smartScrollToIndex(coroutineScope: CoroutineScope, targetIndex: Int) {
if (targetIndex < 0 || firstVisibleItemIndex == targetIndex) {
return
}
coroutineScope.launch {
val totalItems = layoutInfo.totalItemsCount
if (totalItems == 0) {
return@launch
}
val clampedTarget = targetIndex.coerceIn(0, totalItems - 1)
val difference = firstVisibleItemIndex - clampedTarget
val jumpIndex =
when {
difference > FAST_SCROLL_THRESHOLD ->
(clampedTarget + FAST_SCROLL_THRESHOLD).coerceAtMost(totalItems - 1)
difference < -FAST_SCROLL_THRESHOLD -> (clampedTarget - FAST_SCROLL_THRESHOLD).coerceAtLeast(0)
else -> null
}
jumpIndex?.let { scrollToItem(it) }
animateScrollToItem(index = clampedTarget)
}
}

View file

@ -1,46 +0,0 @@
/*
* Copyright (c) 2025 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.meshtastic.core.ui.component
import androidx.compose.foundation.lazy.LazyListState
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlin.math.max
private const val SCROLL_TO_TOP_INDEX = 0
private const val FAST_SCROLL_THRESHOLD = 10
/**
* Executes the smart scroll-to-top policy.
*
* Policy:
* - If the first visible item is already at index 0, do nothing.
* - Otherwise, smoothly animate the list back to the first item.
*/
fun LazyListState.smartScrollToTop(coroutineScope: CoroutineScope) {
if (firstVisibleItemIndex == SCROLL_TO_TOP_INDEX) {
return
}
coroutineScope.launch {
if (firstVisibleItemIndex > FAST_SCROLL_THRESHOLD) {
val jumpIndex = max(SCROLL_TO_TOP_INDEX, firstVisibleItemIndex - FAST_SCROLL_THRESHOLD)
scrollToItem(jumpIndex)
}
animateScrollToItem(index = SCROLL_TO_TOP_INDEX)
}
}