From c44d85732ce58293757ce38bd87a60c73dc86ad6 Mon Sep 17 00:00:00 2001 From: "Sebastian J. Wolf" Date: Wed, 19 Aug 2020 09:55:13 +0200 Subject: [PATCH] First steps to get a chat list on screen :) --- harbour-fernschreiber.pro | 2 + qml/components/ImageThumbnail.qml | 4 +- qml/pages/OverviewPage.qml | 162 +++++++++++++++++++++++++++++- src/chatlistmodel.cpp | 41 ++++++++ src/chatlistmodel.h | 28 ++++++ src/harbour-fernschreiber.cpp | 8 +- src/tdlibwrapper.h | 2 +- 7 files changed, 238 insertions(+), 9 deletions(-) create mode 100644 src/chatlistmodel.cpp create mode 100644 src/chatlistmodel.h diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index ee0e2a1..0a2d286 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -15,6 +15,7 @@ TARGET = harbour-fernschreiber CONFIG += sailfishapp sailfishapp_i18n SOURCES += src/harbour-fernschreiber.cpp \ + src/chatlistmodel.cpp \ src/tdlibreceiver.cpp \ src/tdlibwrapper.cpp @@ -72,6 +73,7 @@ INSTALLS += telegram 86.png 108.png 128.png 172.png 256.png \ fernschreiber.desktop gui images HEADERS += \ + src/chatlistmodel.h \ src/tdlibreceiver.h \ src/tdlibsecrets.h \ src/tdlibwrapper.h diff --git a/qml/components/ImageThumbnail.qml b/qml/components/ImageThumbnail.qml index 69823ad..1671b54 100644 --- a/qml/components/ImageThumbnail.qml +++ b/qml/components/ImageThumbnail.qml @@ -39,7 +39,7 @@ Item { Connections { target: tdLibWrapper onFileUpdated: { - if (id === imageData.id) { + if (fileId === imageData.id) { console.log("File updated, completed? " + fileInformation.local.is_downloading_completed); imageThumbnail.imageData = fileInformation; if (imageThumbnail.imageData.local.is_downloading_completed) { @@ -65,7 +65,7 @@ Item { width: parent.width - Theme.paddingSmall height: parent.height - Theme.paddingSmall color: Theme.primaryColor - radius: parent.width / 7 + radius: parent.width / 2 anchors.centerIn: singleImage visible: false } diff --git a/qml/pages/OverviewPage.qml b/qml/pages/OverviewPage.qml index 912048e..b0f5e04 100644 --- a/qml/pages/OverviewPage.qml +++ b/qml/pages/OverviewPage.qml @@ -22,7 +22,8 @@ import QtMultimedia 5.0 import Sailfish.Silica 1.0 import Nemo.Notifications 1.0 import WerkWolf.Fernschreiber 1.0 - +import "../components" +import "../js/twemoji.js" as Emoji Page { id: overviewPage @@ -119,7 +120,8 @@ Page { SilicaFlickable { id: aboutContainer - contentHeight: column.height + contentHeight: parent.height + contentWidth: parent.width anchors.fill: parent visible: !overviewPage.loading @@ -133,9 +135,11 @@ Page { Column { id: column width: parent.width - spacing: Theme.paddingLarge + height: parent.height + spacing: Theme.paddingMedium Row { + id: headerRow width: parent.width GlassItem { @@ -155,7 +159,157 @@ Page { } } - VerticalScrollDecorator {} + SilicaListView { + + id: chatListView + + width: parent.width + height: parent.height - Theme.paddingMedium - headerRow.height + + clip: true + visible: count > 0 + + model: chatListModel + delegate: ListItem { + + id: chatListItem + + contentHeight: chatListRow.height + chatListSeparator.height + 2 * Theme.paddingMedium + contentWidth: parent.width + + onClicked: { + // jump to chat details here... ;) + // pageStack.push(Qt.resolvedUrl("../pages/ConversationPage.qml"), { "conversationModel" : display, "myUserId": overviewPage.myUser.id_str, "configuration": overviewPage.configuration }); + } + + Column { + id: chatListColumn + width: parent.width - ( 2 * Theme.horizontalPageMargin ) + spacing: Theme.paddingSmall + anchors { + horizontalCenter: parent.horizontalCenter + verticalCenter: parent.verticalCenter + } + + Row { + id: chatListRow + width: parent.width + spacing: Theme.paddingMedium + + Column { + id: chatListPictureColumn + width: parent.width / 6 + height: parent.width / 6 + spacing: Theme.paddingSmall + + ImageThumbnail { + id: chatListPictureThumbnail + visible: display.photo + imageData: display.photo.small + width: parent.width + height: parent.width + } + } + + Column { + id: chatListContentColumn + width: parent.width * 5 / 6 - Theme.horizontalPageMargin + + spacing: Theme.paddingSmall + + Text { + id: chatListNameText + text: Emoji.emojify(display.title, Theme.fontSizeMedium) + textFormat: Text.StyledText + font.pixelSize: Theme.fontSizeMedium + color: Theme.primaryColor + elide: Text.ElideRight + width: parent.width + onTruncatedChanged: { + // There is obviously a bug in QML in truncating text with images. + // We simply remove Emojis then... + if (truncated) { + text = text.replace(/\]+\/\>/g, ""); + } + } + } + + Row { + id: chatListLastMessageRow + width: parent.width + spacing: Theme.paddingMedium + Text { + id: chatListLastUserText + text: Emoji.emojify("Unknown", Theme.fontSizeExtraSmall) + font.pixelSize: Theme.fontSizeExtraSmall + color: Theme.highlightColor + textFormat: Text.StyledText + onTruncatedChanged: { + // There is obviously a bug in QML in truncating text with images. + // We simply remove Emojis then... + if (truncated) { + text = text.replace(/\]+\/\>/g, ""); + } + } + } + Text { + id: chatListLastMessageText + text: Emoji.emojify("Unknown", Theme.fontSizeExtraSmall) + font.pixelSize: Theme.fontSizeExtraSmall + color: Theme.primaryColor + width: parent.width - Theme.paddingMedium - chatListLastUserText.width + elide: Text.ElideRight + textFormat: Text.StyledText + onTruncatedChanged: { + // There is obviously a bug in QML in truncating text with images. + // We simply remove Emojis then... + if (truncated) { + text = text.replace(/\]+\/\>/g, ""); + } + } + } + } + + Timer { + id: messageContactTimeUpdater + interval: 60000 + running: true + repeat: true + onTriggered: { + //messageContactTimeElapsedText.text = getConversationTimeElapsed(display.messages); + } + } + + Text { + id: messageContactTimeElapsedText + //text: getConversationTimeElapsed(display.messages) + text: "somewhen" + font.pixelSize: Theme.fontSizeTiny + color: Theme.primaryColor + } + } + } + + } + + Separator { + id: chatListSeparator + + anchors { + top: chatListColumn.bottom + topMargin: Theme.paddingMedium + } + + width: parent.width + color: Theme.primaryColor + horizontalAlignment: Qt.AlignHCenter + } + + } + + VerticalScrollDecorator {} + } + } } diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp new file mode 100644 index 0000000..7ef3e2d --- /dev/null +++ b/src/chatlistmodel.cpp @@ -0,0 +1,41 @@ +#include "chatlistmodel.h" + +ChatListModel::ChatListModel(TDLibWrapper *tdLibWrapper) +{ + this->tdLibWrapper = tdLibWrapper; + connect(this->tdLibWrapper, SIGNAL(newChatDiscovered(QString, QVariantMap)), this, SLOT(handleChatDiscovered(QString, QVariantMap))); +} + +ChatListModel::~ChatListModel() +{ + +} + +int ChatListModel::rowCount(const QModelIndex &) const +{ + return chatList.size(); +} + +QVariant ChatListModel::data(const QModelIndex &index, int role) const +{ + if(index.isValid() && role == Qt::DisplayRole) { + return QVariant(chatList.value(index.row())); + } + return QVariant(); +} + +bool ChatListModel::insertRows(int row, int count, const QModelIndex &parent) +{ + qDebug() << "[ChatListModel] Inserting at " << row << ", row count: " << count; + beginInsertRows(parent, rowCount(QModelIndex()), rowCount(QModelIndex()) + count - 1); + this->chatList.append(this->chatToBeAdded); + endInsertRows(); + return true; +} + +void ChatListModel::handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation) +{ + qDebug() << "[ChatListModel] Adding new chat " << chatId; + this->chatToBeAdded = chatInformation; + insertRows(rowCount(QModelIndex()), 1); +} diff --git a/src/chatlistmodel.h b/src/chatlistmodel.h new file mode 100644 index 0000000..b5eb54c --- /dev/null +++ b/src/chatlistmodel.h @@ -0,0 +1,28 @@ +#ifndef CHATLISTMODEL_H +#define CHATLISTMODEL_H + +#include +#include +#include "tdlibwrapper.h" + +class ChatListModel : public QAbstractListModel +{ + Q_OBJECT +public: + ChatListModel(TDLibWrapper *tdLibWrapper); + ~ChatListModel() override; + + virtual int rowCount(const QModelIndex&) const override; + virtual QVariant data(const QModelIndex &index, int role) const override; + virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override; + +public slots: + void handleChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); + +private: + TDLibWrapper *tdLibWrapper; + QVariantList chatList; + QVariantMap chatToBeAdded; +}; + +#endif // CHATLISTMODEL_H diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index 2b91580..7ce5eb6 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -30,6 +30,7 @@ #include #include "tdlibwrapper.h" +#include "chatlistmodel.h" int main(int argc, char *argv[]) { @@ -38,10 +39,13 @@ int main(int argc, char *argv[]) QQmlContext *context = view.data()->rootContext(); - TDLibWrapper tdLibWrapper; - context->setContextProperty("tdLibWrapper", &tdLibWrapper); + TDLibWrapper *tdLibWrapper = new TDLibWrapper(view.data()); + context->setContextProperty("tdLibWrapper", tdLibWrapper); qmlRegisterType("WerkWolf.Fernschreiber", 1, 0, "TelegramAPI"); + ChatListModel chatListModel(tdLibWrapper); + context->setContextProperty("chatListModel", &chatListModel); + view->setSource(SailfishApp::pathTo("qml/harbour-fernschreiber.qml")); view->show(); return app->exec(); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 8807f1d..e7ecfd5 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -79,7 +79,7 @@ signals: void optionUpdated(const QString &optionName, const QVariant &optionValue); void connectionStateChanged(const TDLibWrapper::ConnectionState &connectionState); void fileUpdated(const int fileId, const QVariantMap &fileInformation); - void newChatDiscovered(const QString chatId, const QVariantMap &chatInformation); + void newChatDiscovered(const QString &chatId, const QVariantMap &chatInformation); void unreadMessageCountUpdated(const QVariantMap &messageCountInformation); void unreadChatCountUpdated(const QVariantMap &chatCountInformation);