Browse Source

Update Android PM screens from asynchronous core events

master
evgeny 3 days ago
parent
commit
1457275139
  1. 41
      tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt

41
tools/chatgui-android/app/src/main/java/com/utun/chat/viewmodel/ChatViewModel.kt

@ -511,6 +511,20 @@ class ChatViewModel : ViewModel() {
if (cur != null && cur.convId == convId) refreshDmMessages() if (cur != null && cur.convId == convId) refreshDmMessages()
refreshDmConversations() refreshDmConversations()
} }
41 -> { /* DM_MESSAGES: [conv_len:1][conv:utf8][JSON:utf8] */
if (data == null || data.isEmpty()) return
val length = data[0].toInt() and 0xFF
if (data.size <= 1 + length) return
val dm = _currentDm.value ?: return
val conv = String(data, 1, length, Charsets.UTF_8)
if (conv != dm.convId) return
val json = String(data, 1 + length, data.size - 1 - length, Charsets.UTF_8)
_dmMessages.value = repo?.parseDmMessages(json, dm.peerName) ?: emptyList()
}
45 -> {
if (data == null) return
_dmConversations.value = repo?.parseDmConversations(String(data, Charsets.UTF_8)) ?: emptyList()
}
43 -> { /* RADIO_TALK: [group_id:8][src:8][stream:2][on:1] */ 43 -> { /* RADIO_TALK: [group_id:8][src:8][stream:2][on:1] */
if (data == null || data.size < 19) return if (data == null || data.size < 19) return
val bb = java.nio.ByteBuffer.wrap(data, 0, 19).order(java.nio.ByteOrder.LITTLE_ENDIAN) val bb = java.nio.ByteBuffer.wrap(data, 0, 19).order(java.nio.ByteOrder.LITTLE_ENDIAN)
@ -737,7 +751,7 @@ class ChatViewModel : ViewModel() {
val r = repo ?: return val r = repo ?: return
viewModelScope.launch { viewModelScope.launch {
_channels.value = withContext(Dispatchers.IO) { r.getChannels() } _channels.value = withContext(Dispatchers.IO) { r.getChannels() }
_dmConversations.value = withContext(Dispatchers.IO) { r.getDmConversations() } refreshDmConversations()
} }
} }
@ -771,18 +785,12 @@ class ChatViewModel : ViewModel() {
/* ── DM (личные сообщения) ── */ /* ── DM (личные сообщения) ── */
fun refreshDmConversations() { fun refreshDmConversations() {
val r = repo ?: return if (repo != null) NativeLib.dmRequestConversations()
viewModelScope.launch {
_dmConversations.value = withContext(Dispatchers.IO) { r.getDmConversations() }
}
} }
fun refreshDmMessages() { fun refreshDmMessages() {
val r = repo ?: return
val dm = _currentDm.value ?: return val dm = _currentDm.value ?: return
viewModelScope.launch { if (repo != null) NativeLib.dmRequestMessages(dm.convId)
_dmMessages.value = withContext(Dispatchers.IO) { r.getDmMessages(dm.convId, dm.peerName) }
}
} }
fun startDm(channelId: String, nodeId: Long, name: String) { fun startDm(channelId: String, nodeId: Long, name: String) {
@ -796,12 +804,11 @@ class ChatViewModel : ViewModel() {
fun consumeDmOpen() { _dmOpen.value = null } fun consumeDmOpen() { _dmOpen.value = null }
fun openDm(convId: String, peerNodeId: Long, peerName: String) { fun openDm(convId: String, peerNodeId: Long, peerName: String) {
val r = repo ?: return if (repo == null) return
val dm = DmConversation(convId = convId, peerNodeId = peerNodeId, peerName = peerName) val dm = DmConversation(convId = convId, peerNodeId = peerNodeId, peerName = peerName)
_currentDm.value = dm _currentDm.value = dm
viewModelScope.launch { _dmMessages.value = emptyList()
_dmMessages.value = withContext(Dispatchers.IO) { r.getDmMessages(convId, peerName) } refreshDmMessages()
}
} }
fun closeDm() { fun closeDm() {
@ -814,10 +821,10 @@ class ChatViewModel : ViewModel() {
val dm = _currentDm.value ?: return val dm = _currentDm.value ?: return
if (text.isBlank()) return if (text.isBlank()) return
r.dmSend(dm.convId, text) r.dmSend(dm.convId, text)
viewModelScope.launch { }
_dmMessages.value = withContext(Dispatchers.IO) { r.getDmMessages(dm.convId, dm.peerName) }
refreshDmConversations() fun sendDmFile(convId: String, path: String, name: String) {
} if (repo != null) NativeLib.dmSendFile(convId, path, name)
} }
fun refreshMembers(chId: String) { fun refreshMembers(chId: String) {

Loading…
Cancel
Save