qt-chat-app

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

ChatListModel.qml (2812B)


      1 import QtQuick
      2 
      3 // Simplified chat list model - all business logic is in Rust
      4 Item {
      5     id: root
      6 
      7     property ListModel sourceModel: ListModel {}
      8     property ListModel filteredModel: ListModel {}
      9     property string filter: ""
     10     property string currentCursor: ""
     11     property bool hasMore: true
     12     property bool isLoadingMore: false
     13 
     14     // Re-filter when filter changes
     15     onFilterChanged: applyFilter()
     16 
     17     function applyFilter() {
     18         filteredModel.clear()
     19         var query = filter.toLowerCase()
     20         for (var i = 0; i < sourceModel.count; i++) {
     21             var chat = sourceModel.get(i)
     22             if (!query || chat.title.toLowerCase().indexOf(query) !== -1) {
     23                 filteredModel.append(chat)
     24             }
     25         }
     26     }
     27 
     28     function loadMore() {
     29         if (!hasMore || isLoadingMore) return
     30         isLoadingMore = true
     31         NetworkManager.fetchChats(currentCursor)
     32     }
     33 
     34     function subscribeToAllChats() {
     35         var chatIds = []
     36         for (var i = 0; i < sourceModel.count; i++) {
     37             var chatId = sourceModel.get(i).id
     38             if (chatId > 0) chatIds.push(chatId)
     39         }
     40         if (chatIds.length > 0) {
     41             NetworkManager.subscribeToChats(JSON.stringify(chatIds))
     42         }
     43     }
     44 
     45     function getChatById(chatId) {
     46         for (var i = 0; i < sourceModel.count; i++) {
     47             if (sourceModel.get(i).id === chatId) {
     48                 return { index: i, chat: sourceModel.get(i) }
     49             }
     50         }
     51         return null
     52     }
     53 
     54     function updateChat(chatId, newTitle) {
     55         var found = getChatById(chatId)
     56         if (found) {
     57             sourceModel.setProperty(found.index, "title", newTitle)
     58             applyFilter()
     59         }
     60     }
     61 
     62     Connections {
     63         target: NetworkManager
     64 
     65         function onChatsFetched(chatsJson, cursor) {
     66             var chats = JSON.parse(chatsJson)
     67             var isInitial = (root.currentCursor === "")
     68 
     69             if (isInitial) sourceModel.clear()
     70 
     71             for (var i = 0; i < chats.length; i++) {
     72                 sourceModel.append(chats[i])
     73             }
     74 
     75             root.currentCursor = cursor || ""
     76             root.hasMore = (cursor !== undefined && cursor !== null && cursor !== "")
     77             root.isLoadingMore = false
     78             root.applyFilter()
     79             root.subscribeToAllChats()
     80         }
     81 
     82         function onChatCreated(chatJson) {
     83             var chat = JSON.parse(chatJson)
     84             sourceModel.insert(0, chat)
     85             root.applyFilter()
     86             root.subscribeToAllChats()
     87         }
     88 
     89         function onChatUpdated(chatJson) {
     90             var chat = JSON.parse(chatJson)
     91             root.updateChat(chat.id, chat.title)
     92         }
     93     }
     94 
     95     Component.onCompleted: {
     96         isLoadingMore = true
     97         NetworkManager.fetchChats("")
     98     }
     99 }