mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 02:25:21 +00:00
@@ -50,6 +50,7 @@ fun ChatScreen(viewModel: ChatViewModel) {
|
|||||||
var showPasswordDialog by remember { mutableStateOf(false) }
|
var showPasswordDialog by remember { mutableStateOf(false) }
|
||||||
var passwordInput by remember { mutableStateOf("") }
|
var passwordInput by remember { mutableStateOf("") }
|
||||||
var showLocationChannelsSheet by remember { mutableStateOf(false) }
|
var showLocationChannelsSheet by remember { mutableStateOf(false) }
|
||||||
|
var forceScrollToBottom by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
// Show password dialog when needed
|
// Show password dialog when needed
|
||||||
LaunchedEffect(showPasswordPrompt) {
|
LaunchedEffect(showPasswordPrompt) {
|
||||||
@@ -85,7 +86,8 @@ fun ChatScreen(viewModel: ChatViewModel) {
|
|||||||
messages = displayMessages,
|
messages = displayMessages,
|
||||||
currentUserNickname = nickname,
|
currentUserNickname = nickname,
|
||||||
meshService = viewModel.meshService,
|
meshService = viewModel.meshService,
|
||||||
modifier = Modifier.weight(1f)
|
modifier = Modifier.weight(1f),
|
||||||
|
forceScrollToBottom = forceScrollToBottom
|
||||||
)
|
)
|
||||||
// Input area - stays at bottom
|
// Input area - stays at bottom
|
||||||
ChatInputSection(
|
ChatInputSection(
|
||||||
@@ -99,6 +101,7 @@ fun ChatScreen(viewModel: ChatViewModel) {
|
|||||||
if (messageText.text.trim().isNotEmpty()) {
|
if (messageText.text.trim().isNotEmpty()) {
|
||||||
viewModel.sendMessage(messageText.text.trim())
|
viewModel.sendMessage(messageText.text.trim())
|
||||||
messageText = TextFieldValue("")
|
messageText = TextFieldValue("")
|
||||||
|
forceScrollToBottom = !forceScrollToBottom // Toggle to trigger scroll
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
showCommandSuggestions = showCommandSuggestions,
|
showCommandSuggestions = showCommandSuggestions,
|
||||||
|
|||||||
@@ -31,13 +31,37 @@ fun MessagesList(
|
|||||||
messages: List<BitchatMessage>,
|
messages: List<BitchatMessage>,
|
||||||
currentUserNickname: String,
|
currentUserNickname: String,
|
||||||
meshService: BluetoothMeshService,
|
meshService: BluetoothMeshService,
|
||||||
modifier: Modifier = Modifier
|
modifier: Modifier = Modifier,
|
||||||
|
forceScrollToBottom: Boolean = false
|
||||||
) {
|
) {
|
||||||
val listState = rememberLazyListState()
|
val listState = rememberLazyListState()
|
||||||
|
|
||||||
// Auto-scroll to bottom when new messages arrive
|
// Track if this is the first time messages are being loaded
|
||||||
|
var hasScrolledToInitialPosition by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
|
// Smart scroll: auto-scroll to bottom for initial load, then only when user is at or near the bottom
|
||||||
LaunchedEffect(messages.size) {
|
LaunchedEffect(messages.size) {
|
||||||
if (messages.isNotEmpty()) {
|
if (messages.isNotEmpty()) {
|
||||||
|
val layoutInfo = listState.layoutInfo
|
||||||
|
val lastVisibleIndex = layoutInfo.visibleItemsInfo.lastOrNull()?.index ?: -1
|
||||||
|
val totalItems = layoutInfo.totalItemsCount
|
||||||
|
|
||||||
|
// Always scroll to bottom on first load, or when user is near the bottom
|
||||||
|
val isFirstLoad = !hasScrolledToInitialPosition
|
||||||
|
val isNearBottom = lastVisibleIndex >= totalItems - 3
|
||||||
|
|
||||||
|
if (isFirstLoad || isNearBottom) {
|
||||||
|
listState.animateScrollToItem(messages.size - 1)
|
||||||
|
if (isFirstLoad) {
|
||||||
|
hasScrolledToInitialPosition = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Force scroll to bottom when requested (e.g., when user sends a message)
|
||||||
|
LaunchedEffect(forceScrollToBottom) {
|
||||||
|
if (forceScrollToBottom && messages.isNotEmpty()) {
|
||||||
listState.animateScrollToItem(messages.size - 1)
|
listState.animateScrollToItem(messages.size - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user