android, desktop: fix item statuses updates (#5840)

This commit is contained in:
spaced4ndy
2025-04-22 10:47:17 +00:00
committed by GitHub
parent 805a69da94
commit 4a103c94c1
5 changed files with 28 additions and 20 deletions
@@ -301,7 +301,6 @@ object ChatModel {
* If you use api call to get the items, use just [add] instead of [addToChatItems].
* Never modify underlying list directly because it produces unexpected results in ChatView's LazyColumn (setting by index is ok) */
val chatItems = mutableStateOf(SnapshotStateList<ChatItem>())
val chatItemStatuses = mutableMapOf<Long, CIStatus>()
// set listener here that will be notified on every add/delete of a chat item
val chatState = ActiveChatState()
@@ -539,15 +538,16 @@ object ChatModel {
val items = chatItems.value
val itemIndex = items.indexOfFirst { it.id == cItem.id }
if (itemIndex >= 0) {
items[itemIndex] = cItem
} else {
val status = chatItemStatuses.remove(cItem.id)
val ci = if (status != null && cItem.meta.itemStatus is CIStatus.SndNew) {
cItem.copy(meta = cItem.meta.copy(itemStatus = status))
val oldStatus = items[itemIndex].meta.itemStatus
val newStatus = cItem.meta.itemStatus
val ci = if (shouldKeepOldSndCIStatus(oldStatus, newStatus)) {
cItem.copy(meta = cItem.meta.copy(itemStatus = oldStatus))
} else {
cItem
}
addToChatItems(ci)
items[itemIndex] = ci
} else {
addToChatItems(cItem)
itemAdded = true
}
}
@@ -556,10 +556,6 @@ object ChatModel {
}
suspend fun updateChatItem(cInfo: ChatInfo, cItem: ChatItem, status: CIStatus? = null, atIndex: Int? = null) {
// TODO [knocking] see diff:
// why chatItemStatuses gets updated even if chatId doesn't match?
// should chatItemBelongsToScope replace chatId check?
// should chatItemStatuses be updated only if chatItemBelongsToScope (as in changed code)? this seems to make more sense
withContext(Dispatchers.Main) {
if (chatItemBelongsToScope(cInfo, cItem)) {
val items = chatItems.value
@@ -567,9 +563,6 @@ object ChatModel {
if (itemIndex >= 0) {
items[itemIndex] = cItem
}
if (status != null) {
chatItemStatuses[cItem.id] = status
}
}
}
}
@@ -657,7 +650,6 @@ object ChatModel {
}
// clear current chat
if (chatId.value == cInfo.id) {
chatItemStatuses.clear()
chatItems.clearAndNotify()
}
}
@@ -3082,6 +3074,18 @@ sealed class CIStatus {
@Serializable @SerialName("rcvRead") class RcvRead: CIStatus()
@Serializable @SerialName("invalid") class Invalid(val text: String): CIStatus()
fun isSent(): Boolean = when(this) {
is SndNew -> false
is SndSent -> true
is SndRcvd -> false
is SndErrorAuth -> true
is CISSndError -> true
is SndWarning -> true
is RcvNew -> false
is RcvRead -> false
is Invalid -> false
}
fun statusIcon(
primaryColor: Color,
metaColor: Color = CurrentColors.value.colors.secondary,
@@ -3121,6 +3125,13 @@ sealed class CIStatus {
}
}
fun shouldKeepOldSndCIStatus(oldStatus: CIStatus, newStatus: CIStatus): Boolean =
when {
oldStatus is CIStatus.SndRcvd && newStatus !is CIStatus.SndRcvd -> true
oldStatus.isSent() && newStatus is CIStatus.SndNew -> true
else -> false
}
@Serializable
sealed class SndError {
@Serializable @SerialName("auth") class Auth: SndError()
@@ -2588,10 +2588,10 @@ object ChatController {
val cItem = chatItem.chatItem
if (!cItem.isDeletedContent && active(r.user)) {
withContext(Dispatchers.Main) {
chatModel.chatsContext.updateChatItem(cInfo, cItem, status = cItem.meta.itemStatus)
chatModel.chatsContext.upsertChatItem(rhId, cInfo, cItem)
}
withContext(Dispatchers.Main) {
chatModel.secondaryChatsContext.value?.updateChatItem(cInfo, cItem, status = cItem.meta.itemStatus)
chatModel.secondaryChatsContext.value?.upsertChatItem(rhId, cInfo, cItem)
}
}
}
@@ -68,7 +68,6 @@ suspend fun processLoadedChat(
}
}
withContext(Dispatchers.Main) {
chatsCtx.chatItemStatuses.clear()
chatsCtx.chatItems.replaceAll(chat.chatItems)
chatModel.chatId.value = chat.id
splits.value = newSplits
@@ -236,7 +236,6 @@ suspend fun openChat(
suspend fun openLoadedChat(chat: Chat) {
withContext(Dispatchers.Main) {
chatModel.chatsContext.chatItemStatuses.clear()
chatModel.chatsContext.chatItems.replaceAll(chat.chatItems)
chatModel.chatId.value = chat.chatInfo.id
chatModel.chatsContext.chatState.clear()
@@ -45,7 +45,6 @@ fun AddGroupView(chatModel: ChatModel, rh: RemoteHostInfo?, close: () -> Unit, c
withContext(Dispatchers.Main) {
chatModel.chatsContext.updateGroup(rhId = rhId, groupInfo)
chatModel.chatsContext.chatItems.clearAndNotify()
chatModel.chatsContext.chatItemStatuses.clear()
chatModel.chatId.value = groupInfo.id
}
setGroupMembers(rhId, groupInfo, chatModel)