aboutsummaryrefslogtreecommitdiff
path: root/qml/ChatlistPage.qml
blob: 2e1d7fe9edbdbe937d8742396367e589db63f21f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQml.Models 2.1
import org.kde.kirigami 2.13 as Kirigami

import DeltaChat 1.0

Kirigami.Page {
    title: qsTr("Chats")
    id: chatlistPage

    property DcContext context

    signal messagesChanged
    onMessagesChanged: {
        // Reload chatlist
        updateChatlist();
    }

    Component.onCompleted: {
        eventEmitter.onMessagesChanged.connect(messagesChanged)
        updateChatlist()
    }

    ListModel {
        id: chatlistModel
    }

    function updateChatlist() {
        let chatlist = chatlistPage.context.getChatlist()

        // Merge new chatlist with existing one.
        // To preserve selected item, we do not simply clear and fill
        // the model from scratch.

        for (let i = 0; i < chatlist.getChatCount(); i++) {
            const summary = chatlist.getSummary(i)
            const chatId = chatlist.getChatId(i)
            const chat = chatlistPage.context.getChat(chatId)
            const profileImage = chat.getProfileImage()

            const item = {
                chatId: chatId,
                msgId: chatlist.getMsgId(i),
                username: (summary.text1 != "" ? summary.text1 + ": " : "") + summary.text2,
                avatarSource: profileImage ? "file:" + profileImage : "",
                chatName: chat.name,
                freshMsgCnt: chatlistPage.context.getFreshMsgCnt(chatId)
            }

            let j;
            for (j = i; j < chatlistModel.count; j++) {
                if (chatlistModel.get(j).chatId == chatId) {
                    // This chat was already in the chatlist,
                    // move it to the new place and update.
                    chatlistModel.move(j, i, 1)
                    chatlistModel.set(i, item)
                    break
                }
            }

            // This chat is new, insert it.
            if (j == chatlistModel.count) {
                chatlistModel.insert(i, item)
            }
        }

        // Remove any chats that are not present in the new chatlist.
        if (chatlistModel.count > chatlist.getChatCount()) {
            chatlistModel.remove(chatlist.getChatCount(),
                                 chatlistModel.count - chatlist.getChatCount())
        }
    }

    ListView {
        id: chatlist

        anchors.fill: parent
        model: chatlistModel

        onCurrentItemChanged: {
            var chatId = chatlistModel.get(currentIndex).chatId

            console.log("Current index is " + currentIndex)
            console.log("Selected chat " + chatId)

            console.log("Depth is " + pageStack.depth)
            let chatPageComponent = Qt.createComponent("qrc:/qml/ChatPage.qml")
            if (chatPageComponent.status == Component.Ready) {
                let myPage = chatPageComponent.createObject(chatlistPage, {chatId: chatId})
                if (pageStack.depth == 2) {
                    pageStack.push(myPage)
                } else if (pageStack.depth == 3) {
                    pageStack.currentIndex = 2
                    pageStack.replace(myPage)
                }
            } else if (chatPageComponent.status == Component.Error) {
                console.log("Error loading chat page: " + chatPageComponent.errorString())
            }
        }

        delegate: Kirigami.BasicListItem {
            width: chatlist.width

            label: chatlistPage.context.getChat(model.chatId).getName()
            subtitle: model.username

            leading: Kirigami.Avatar {
                source: model.avatarSource
                name: model.chatName
                implicitWidth: height
                color: chatlistPage.context.getChat(model.chatId).getColor()
            }

            trailing: Label {
                text: model.freshMsgCnt
                visible: model.freshMsgCnt > 0
                verticalAlignment: Text.AlignVCenter
            }
        }

        ScrollBar.vertical: ScrollBar {}
    }
}