mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: Add bottom-nav scroll-to-top handling for nodes and conversations (#3674)
This commit is contained in:
parent
00276bc5d4
commit
bc8ff26167
8 changed files with 176 additions and 13 deletions
|
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* 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
|
||||
|
||||
/**
|
||||
* Event emitted when a user re-presses a bottom navigation destination that should trigger a scroll-to-top behaviour on
|
||||
* the corresponding screen.
|
||||
*/
|
||||
sealed class ScrollToTopEvent {
|
||||
data object NodesTabPressed : ScrollToTopEvent()
|
||||
|
||||
data object ConversationsTabPressed : ScrollToTopEvent()
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue