aboutsummaryrefslogtreecommitdiff
path: root/qml/ChatlistPage.qml
diff options
context:
space:
mode:
authorlink2xt <link2xt@testrun.org>2020-09-12 14:10:13 +0300
committerlink2xt <link2xt@testrun.org>2020-10-03 00:20:03 +0300
commitb8762ddb38dd975b0acb217b793594dfed83a824 (patch)
tree23ccefbba703fed6c07acce82ff72e32ba77c9ba /qml/ChatlistPage.qml
downloadkdeltachat-b8762ddb38dd975b0acb217b793594dfed83a824.tar.gz
kdeltachat-b8762ddb38dd975b0acb217b793594dfed83a824.zip
Initial commit
Diffstat (limited to 'qml/ChatlistPage.qml')
-rw-r--r--qml/ChatlistPage.qml105
1 files changed, 105 insertions, 0 deletions
diff --git a/qml/ChatlistPage.qml b/qml/ChatlistPage.qml
new file mode 100644
index 0000000..3a5259a
--- /dev/null
+++ b/qml/ChatlistPage.qml
@@ -0,0 +1,105 @@
+import QtQuick 2.14
+import QtQuick.Controls 2.14
+import QtQuick.Layouts 1.14
+import QtQml.Models 2.1
+import org.kde.kirigami 2.12 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 item = {
+ chatId: chatId,
+ msgId: chatlist.getMsgId(i),
+ username: summary.text1
+ }
+
+ 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)
+ }
+ }
+ }
+
+ delegate: Kirigami.BasicListItem {
+ width: chatlist.width
+
+ label: chatlistPage.context.getChat(model.chatId).getName()
+ subtitle: model.username
+ }
+
+ ScrollBar.vertical: ScrollBar {}
+ }
+}