qt-chat-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

MessageListModel.qml (1868B)


      1 import QtQuick
      2 
      3 // Simplified message list model - caching and transformation done in Rust
      4 Item {
      5     id: root
      6 
      7     property ListModel model: ListModel {}
      8     property int chatId: -1
      9     property string currentCursor: ""
     10     property bool hasMore: true
     11     property bool isLoadingMore: false
     12 
     13     function loadMore() {
     14         if (!hasMore || isLoadingMore || chatId < 0) return
     15         isLoadingMore = true
     16         NetworkManager.fetchMessages(chatId, currentCursor)
     17     }
     18 
     19     Connections {
     20         target: NetworkManager
     21 
     22         function onMessagesFetched(messagesJson, cursor) {
     23             var messages = JSON.parse(messagesJson)
     24             var isInitial = (root.currentCursor === "")
     25 
     26             if (isInitial) {
     27                 model.clear()
     28                 // Messages come from Rust already reversed for display
     29                 for (var i = 0; i < messages.length; i++) {
     30                     model.append(messages[i])
     31                 }
     32             } else {
     33                 // Prepend older messages (pagination)
     34                 for (var i = messages.length - 1; i >= 0; i--) {
     35                     model.insert(0, messages[i])
     36                 }
     37             }
     38 
     39             root.currentCursor = cursor || ""
     40             root.hasMore = (cursor !== undefined && cursor !== null && cursor !== "")
     41             root.isLoadingMore = false
     42         }
     43 
     44         function onMessageReceived(messageJson) {
     45             var msg = JSON.parse(messageJson)
     46             // Only add if for this chat (Rust already deduplicated)
     47             if (msg.chat_id === root.chatId) {
     48                 model.append(msg)
     49             }
     50         }
     51     }
     52 
     53     onChatIdChanged: {
     54         if (chatId >= 0) {
     55             model.clear()
     56             currentCursor = ""
     57             hasMore = true
     58             isLoadingMore = true
     59             NetworkManager.fetchMessages(chatId, "")
     60         }
     61     }
     62 }