Preserve UI snapshots after parse failure and ignore superseded queries

This commit is contained in:
evgeny committed 2026-10-29 13:34:26 +01:00
1 parent 2552c57730
commit 5acab13e5e
2 files changed
+22 -12

No files matched your search

@@ -126,7 +126,7 @@ object NativeLib {
fun connectMemberNode(channelId: String, nodeId: Long) { nativeConnectMemberNode(channelId, nodeId) }
fun getChannels(): String = checkNotNull(nativeGetChannels()) { "Channel snapshot unavailable" }
fun getMessages(channelId: String, limit: Int = 50, anchor: String = "", exact: Boolean = false): String =
nativeGetMessages(channelId, limit, messageIdBytes(anchor), exact) ?: "[]"
checkNotNull(nativeGetMessages(channelId, limit, messageIdBytes(anchor), exact)) { "Message snapshot unavailable channel=$channelId" }
fun feedbackCommand(json: String, requestId: Long = 0): Boolean = nativeFeedbackCommand(json.toByteArray(Charsets.UTF_8), requestId)
fun clearDatabase(): Boolean = nativeClearDatabase()
fun setDebugLevel(category: String, level: String) { nativeSetDebugLevel(category, level) }
@@ -71,6 +71,7 @@ class ChatViewModel : ViewModel() {
private val _messageJump = MutableStateFlow<MessageJump?>(null)
val messageJump: StateFlow<MessageJump?> = _messageJump
private var messageRefreshRevision = 0L
private var channelRefreshRevision = 0L
private var jumpRevision = 0L
private var pendingLatestJump: MessageJump? = null
private val _originalMissing = MutableStateFlow(false)
@@ -612,7 +613,9 @@ class ChatViewModel : ViewModel() {
if (joining != null && joining.pending && joining.request == request && joining.channelId == chId && joining.nodeId == nodeId) {
_linkJoin.value = joining.copy(pending = false, result = result)
if (result == 0) viewModelScope.launch {
val channels = withContext(Dispatchers.IO) { repo?.getChannels().orEmpty() }
val r = repo ?: return@launch
val channels = withContext(Dispatchers.IO) { r.getChannels() } ?: return@launch
if (repo !== r) return@launch
_channels.value = channels
val channel = channels.find { it.id.toULongOrNull()?.toLong() == chId }
if (_linkJoin.value?.link == joining.link && channel != null) {
@@ -629,6 +632,8 @@ class ChatViewModel : ViewModel() {
4 -> { if (repo != null) { LogManager.addLog("INFO", "VM", "CHANNEL_UPDATED — refreshChannels"); refreshChannels() } else { LogManager.addLog("WARN", "VM", "CHANNEL_UPDATED skipped — repo is NULL") } }
12 -> { LogManager.addLog("INFO", "VM", "service started"); watchStreams() }
13 -> {
repo = null; dbReady = false
++channelRefreshRevision; ++messageRefreshRevision
_streamSources.value = emptyList(); _streamSourcesFailed.value = false; selectStream(null)
for (id in submissions.keys.toList()) submissionResult(id, false)
_radioTalkState.value = RadioTalkState()
@@ -735,7 +740,7 @@ class ChatViewModel : ViewModel() {
}
19 -> { /* LOCAL_SOCKETS: снимок создан в потоке ядра; пустое событие просит обновить его. */
if (data == null || data.isEmpty()) refreshLocalSockets()
else repo?.let { _localSockets.value = it.parseLocalSockets(String(data, Charsets.UTF_8)) }
else repo?.parseLocalSockets(String(data, Charsets.UTF_8))?.let { _localSockets.value = it }
}
23 -> Unit /* Применено общим MemberRepository до доставки UI-события. */
24 -> {
@@ -899,12 +904,13 @@ class ChatViewModel : ViewModel() {
LogManager.addLog("DEBUG", "PM", "Discarded stale history conv=$conv generation=$generation"); return
}
val json = String(data, 9 + length, data.size - 9 - length, Charsets.UTF_8)
_dmMessages.value = repo?.parseDmMessages(json, dm.peerName) ?: emptyList()
repo?.parseDmMessages(json, dm.peerName)?.let { _dmMessages.value = it }
}
45 -> {
if (data == null) return
val previous = _dmConversations.value.associateBy { it.convId }
_dmConversations.value = (repo?.parseDmConversations(String(data, Charsets.UTF_8)) ?: emptyList()).map {
val snapshot = repo?.parseDmConversations(String(data, Charsets.UTF_8)) ?: return
_dmConversations.value = snapshot.map {
it.copy(online = previous[it.convId]?.online == true)
}
_currentDm.value?.let { current ->
@@ -1138,8 +1144,11 @@ class ChatViewModel : ViewModel() {
fun refreshChannels() {
val r = repo ?: return
val revision = ++channelRefreshRevision
viewModelScope.launch {
_channels.value = withContext(Dispatchers.IO) { r.getChannels() }
val channels = withContext(Dispatchers.IO) { r.getChannels() } ?: return@launch
if (repo !== r || revision != channelRefreshRevision) return@launch
_channels.value = channels
refreshDmConversations()
}
}
@@ -1151,14 +1160,14 @@ class ChatViewModel : ViewModel() {
val revision = ++messageRefreshRevision
viewModelScope.launch {
val (rows, originals) = withContext(Dispatchers.IO) {
val rows = r.getMessages(chId, 100, anchor)
val rows = r.getMessages(chId, 100, anchor) ?: return@withContext null
val loaded = rows.associateBy { it.messageId }.toMutableMap()
for (id in rows.map { it.replyTo }.filter { it.isNotEmpty() }.distinct()) {
if (id !in loaded) r.getMessages(chId, 1, id, true).firstOrNull()?.let { loaded[id] = it }
if (id !in loaded) r.getMessages(chId, 1, id, true)?.firstOrNull()?.let { loaded[id] = it }
}
rows to loaded
}
if (_currentChannel.value?.id != chId || revision != messageRefreshRevision) return@launch
} ?: return@launch
if (repo !== r || _currentChannel.value?.id != chId || revision != messageRefreshRevision) return@launch
_messages.value = rows.map { it.copy(feedback = feedbackCache[it.messageId]) }
_replyOriginals.value = originals
if (anchor.isEmpty()) pendingLatestJump?.let { _messageJump.value = it; pendingLatestJump = null }
@@ -1174,8 +1183,8 @@ class ChatViewModel : ViewModel() {
val revision = ++jumpRevision
pendingLatestJump = null
viewModelScope.launch {
val rows = withContext(Dispatchers.IO) { r.getMessages(channel, 100, id) }
if (_currentChannel.value?.id != channel || jumpRevision != revision) return@launch
val rows = withContext(Dispatchers.IO) { r.getMessages(channel, 100, id) } ?: return@launch
if (repo !== r || _currentChannel.value?.id != channel || jumpRevision != revision) return@launch
if (rows.none { it.messageId == id }) {
LogManager.addLog("WARN", "Reply", "original unavailable ch=$channel id=$id")
_originalMissing.value = true
@@ -1862,6 +1871,7 @@ class ChatViewModel : ViewModel() {
}
fun clearState() {
++channelRefreshRevision; ++messageRefreshRevision; ++jumpRevision
memberDetailJob?.cancel(); memberDetailTarget = null; _selectedMember.value = null
dismissPmInvite(); _linkJoin.value = null; _joinedChannel.value = null
_attachmentPreparations.value = emptyMap()