Skip to content

Commit

Permalink
refactor: migrate MessagesFragment to Compose (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir authored Dec 1, 2024
1 parent 5d3b365 commit 3c581f8
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 451 deletions.
8 changes: 0 additions & 8 deletions app/src/main/java/com/geeksville/mesh/model/UIState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ class UIViewModel @Inject constructor(
val quickChatActions get() = quickChatActionRepository.getAllActions()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

private val _focusedNode = MutableStateFlow<NodeEntity?>(null)
val focusedNode: StateFlow<NodeEntity?> = _focusedNode

private val nodeFilterText = MutableStateFlow("")
private val nodeSortOption = MutableStateFlow(NodeSortOption.LAST_HEARD)
private val includeUnknown = MutableStateFlow(preferences.getBoolean("include-unknown", false))
Expand Down Expand Up @@ -729,11 +726,6 @@ class UIViewModel @Inject constructor(
_currentTab.value = tab
}

fun focusUserNode(node: NodeEntity?) {
_currentTab.value = 1
_focusedNode.value = node
}

fun setNodeFilterText(text: String) {
nodeFilterText.value = text
}
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/java/com/geeksville/mesh/ui/ContactsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ class ContactsFragment : ScreenFragment("Messages"), Logging {
private inner class ActionModeCallback : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
mode.menuInflater.inflate(R.menu.menu_messages, menu)
menu.findItem(R.id.resendButton).isVisible = false
menu.findItem(R.id.copyButton).isVisible = false
mode.title = "1"
return true
}
Expand Down
26 changes: 17 additions & 9 deletions app/src/main/java/com/geeksville/mesh/ui/MessageListView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

package com.geeksville.mesh.ui

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
Expand All @@ -41,12 +43,12 @@ import kotlinx.coroutines.flow.debounce
@Composable
internal fun MessageListView(
messages: List<Message>,
selectedList: List<Message>,
onClick: (Message) -> Unit,
onLongClick: (Message) -> Unit,
onChipClick: (Message) -> Unit,
selectedIds: MutableState<Set<Long>>,
onUnreadChanged: (Long) -> Unit,
contentPadding: PaddingValues,
onClick: (Message) -> Unit = {}
) {
val inSelectionMode by remember { derivedStateOf { selectedIds.value.isNotEmpty() } }
val listState = rememberLazyListState(
initialFirstVisibleItemIndex = messages.indexOfLast { !it.read }.coerceAtLeast(0)
)
Expand All @@ -60,24 +62,30 @@ internal fun MessageListView(
SimpleAlertDialog(title = title, text = text) { showStatusDialog = null }
}

fun toggle(uuid: Long) = if (selectedIds.value.contains(uuid)) {
selectedIds.value -= uuid
} else {
selectedIds.value += uuid
}

LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
reverseLayout = true,
// contentPadding = PaddingValues(8.dp)
contentPadding = contentPadding
) {
items(messages, key = { it.uuid }) { msg ->
val selected by remember { derivedStateOf { selectedList.contains(msg) } }
val selected by remember { derivedStateOf { selectedIds.value.contains(msg.uuid) } }

MessageItem(
shortName = msg.user.shortName.takeIf { msg.user.id != DataPacket.ID_LOCAL },
messageText = msg.text,
messageTime = msg.time,
messageStatus = msg.status,
selected = selected,
onClick = { onClick(msg) },
onLongClick = { onLongClick(msg) },
onChipClick = { onChipClick(msg) },
onClick = { if (inSelectionMode) toggle(msg.uuid) },
onLongClick = { toggle(msg.uuid) },
onChipClick = { onClick(msg) },
onStatusClick = { showStatusDialog = msg }
)
}
Expand Down
Loading

0 comments on commit 3c581f8

Please sign in to comment.