From 19a17ed3f37433056188c23f359906236c6da7e6 Mon Sep 17 00:00:00 2001 From: Sebastian Wolf Date: Sun, 27 Dec 2020 00:16:25 +0100 Subject: [PATCH] Now you can search in a chat... --- qml/pages/ChatPage.qml | 66 +++++++++++++++++++++ qml/pages/OverviewPage.qml | 15 +++-- src/chatmodel.cpp | 61 ++++++++++++++----- src/chatmodel.h | 5 +- src/tdlibwrapper.cpp | 14 +++++ src/tdlibwrapper.h | 1 + translations/harbour-fernschreiber-de.ts | 12 +++- translations/harbour-fernschreiber-en.ts | 12 +++- translations/harbour-fernschreiber-es.ts | 10 +++- translations/harbour-fernschreiber-fi.ts | 10 +++- translations/harbour-fernschreiber-hu.ts | 10 +++- translations/harbour-fernschreiber-it.ts | 10 +++- translations/harbour-fernschreiber-pl.ts | 10 +++- translations/harbour-fernschreiber-ru.ts | 10 +++- translations/harbour-fernschreiber-sv.ts | 10 +++- translations/harbour-fernschreiber-zh_CN.ts | 10 +++- translations/harbour-fernschreiber.ts | 10 +++- 17 files changed, 243 insertions(+), 33 deletions(-) diff --git a/qml/pages/ChatPage.qml b/qml/pages/ChatPage.qml index ef793a3..0387ef8 100644 --- a/qml/pages/ChatPage.qml +++ b/qml/pages/ChatPage.qml @@ -332,6 +332,14 @@ Page { return false; } + function resetFocus() { + if (searchInChatField.text === "") { + chatOverviewItem.visible = true; + } + searchInChatField.focus = false; + chatPage.focus = true; + } + Timer { id: forwardMessagesTimer interval: 200 @@ -348,6 +356,17 @@ Page { } } + Timer { + id: searchInChatTimer + interval: 300 + running: false + repeat: false + onTriggered: { + Debug.log("Searching for '" + searchInChatField.text + "'"); + chatModel.setSearchQuery(searchInChatField.text); + } + } + Component.onCompleted: { initializePage(); } @@ -627,6 +646,17 @@ Page { } text: chatInformation.notification_settings.mute_for > 0 ? qsTr("Unmute Chat") : qsTr("Mute Chat") } + + MenuItem { + id: searchInChatMenuItem + visible: !chatPage.isSecretChat + onClicked: { + // This automatically shows the search field as well + chatOverviewItem.visible = false; + searchInChatField.focus = true; + } + text: qsTr("Search in Chat") + } } BackgroundItem { @@ -700,6 +730,8 @@ Page { Item { id: chatOverviewItem + opacity: visible ? 1 : 0 + Behavior on opacity { FadeAnimation {} } width: parent.width - chatPictureThumbnail.width - Theme.paddingMedium height: chatNameText.height + chatStatusText.height anchors.bottom: parent.bottom @@ -732,6 +764,40 @@ Page { maximumLineCount: 1 } } + + Item { + id: searchInChatItem + visible: !chatOverviewItem.visible + opacity: visible ? 1 : 0 + Behavior on opacity { FadeAnimation {} } + width: parent.width - chatPictureThumbnail.width - Theme.paddingMedium + height: searchInChatField.height + anchors.bottom: parent.bottom + anchors.bottomMargin: chatPage.isPortrait ? Theme.paddingMedium : Theme.paddingSmall + + SearchField { + id: searchInChatField + visible: false + width: visible ? parent.width : 0 + height: parent.height + placeholderText: qsTr("Search in chat...") + active: searchInChatItem.visible + canHide: text === "" + + onTextChanged: { + searchInChatTimer.restart(); + } + + onHideClicked: { + resetFocus(); + } + + EnterKey.iconSource: "image://theme/icon-m-enter-close" + EnterKey.onClicked: { + resetFocus(); + } + } + } } PinnedMessageItem { diff --git a/qml/pages/OverviewPage.qml b/qml/pages/OverviewPage.qml index 62e9342..cbb7775 100644 --- a/qml/pages/OverviewPage.qml +++ b/qml/pages/OverviewPage.qml @@ -156,11 +156,13 @@ Page { } function resetFocus() { + if (chatSearchField.text === "") { + chatSearchField.visible = false; + pageHeader.visible = true; + searchChatButton.visible = overviewPage.connectionState === TelegramAPI.ConnectionReady; + } chatSearchField.focus = false; overviewPage.focus = true; - chatSearchField.visible = false; - pageHeader.visible = true; - searchChatButton.visible = overviewPage.connectionState === TelegramAPI.ConnectionReady; } Connections { @@ -282,13 +284,18 @@ Page { Behavior on opacity { FadeAnimation {} } width: visible ? ( parent.width - pageStatus.width ) : 0 height: pageHeader.height - placeholderText: qsTr("Search a chat...") + placeholderText: qsTr("Filter your chats...") active: searchHeaderItem.visible + canHide: text === "" onTextChanged: { searchChatTimer.restart(); } + onHideClicked: { + resetFocus(); + } + EnterKey.iconSource: "image://theme/icon-m-enter-close" EnterKey.onClicked: { resetFocus(); diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index f7e85b3..2082e8d 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -149,11 +149,13 @@ QVariant ChatModel::data(const QModelIndex &index, int role) const return QVariant(); } -void ChatModel::clear() +void ChatModel::clear(bool contentOnly) { LOG("Clearing chat model"); inReload = false; inIncrementalUpdate = false; + searchModeActive = false; + searchQuery.clear(); if (!messages.isEmpty()) { beginResetModel(); qDeleteAll(messages); @@ -161,13 +163,16 @@ void ChatModel::clear() messageIndexMap.clear(); endResetModel(); } - if (!chatInformation.isEmpty()) { - chatInformation.clear(); - emit smallPhotoChanged(); - } - if (chatId) { - chatId = 0; - emit chatIdChanged(); + + if (!contentOnly) { + if (!chatInformation.isEmpty()) { + chatInformation.clear(); + emit smallPhotoChanged(); + } + if (chatId) { + chatId = 0; + emit chatIdChanged(); + } } } @@ -181,6 +186,7 @@ void ChatModel::initialize(const QVariantMap &chatInformation) this->chatId = chatId; this->messages.clear(); this->messageIndexMap.clear(); + this->searchQuery.clear(); endResetModel(); emit chatIdChanged(); emit smallPhotoChanged(); @@ -190,15 +196,21 @@ void ChatModel::initialize(const QVariantMap &chatInformation) void ChatModel::triggerLoadMoreHistory() { if (!this->inIncrementalUpdate && !messages.isEmpty()) { - LOG("Trigger loading older history..."); - this->inIncrementalUpdate = true; - this->tdLibWrapper->getChatHistory(chatId, messages.first()->messageId); + if (searchModeActive) { + LOG("Trigger loading older found messages..."); + this->inIncrementalUpdate = true; + this->tdLibWrapper->searchChatMessages(chatId, searchQuery, messages.first()->messageId); + } else { + LOG("Trigger loading older history..."); + this->inIncrementalUpdate = true; + this->tdLibWrapper->getChatHistory(chatId, messages.first()->messageId); + } } } void ChatModel::triggerLoadMoreFuture() { - if (!this->inIncrementalUpdate && !messages.isEmpty()) { + if (!this->inIncrementalUpdate && !messages.isEmpty() && !searchModeActive) { LOG("Trigger loading newer future..."); this->inIncrementalUpdate = true; this->tdLibWrapper->getChatHistory(chatId, messages.last()->messageId, -49); @@ -214,9 +226,8 @@ QVariantMap ChatModel::getMessage(int index) { if (index >= 0 && index < messages.size()) { return messages.at(index)->messageData; - } else { - return QVariantMap(); } + return QVariantMap(); } int ChatModel::getLastReadMessageIndex() @@ -240,6 +251,20 @@ int ChatModel::getLastReadMessageIndex() } } +void ChatModel::setSearchQuery(const QString newSearchQuery) +{ + if (this->searchQuery != newSearchQuery) { + this->clear(true); + this->searchQuery = newSearchQuery; + this->searchModeActive = !this->searchQuery.isEmpty(); + if (this->searchModeActive) { + this->tdLibWrapper->searchChatMessages(this->chatId, this->searchQuery); + } else { + this->tdLibWrapper->getChatHistory(chatId, this->chatInformation.value(LAST_READ_INBOX_MESSAGE_ID).toLongLong()); + } + } +} + QVariantMap ChatModel::smallPhoto() const { return chatInformation.value(PHOTO).toMap().value(SMALL).toMap(); @@ -253,6 +278,7 @@ qlonglong ChatModel::getChatId() const void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCount) { LOG("Receiving new messages :)" << messages.size()); + LOG("Received while search mode is" << searchModeActive); if (messages.size() == 0) { LOG("No additional messages loaded, notifying chat UI..."); @@ -269,6 +295,7 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo if (this->isMostRecentMessageLoaded() || this->inIncrementalUpdate) { QList messagesToBeAdded; QListIterator messagesIterator(messages); + while (messagesIterator.hasNext()) { const QVariantMap messageData = messagesIterator.next().toMap(); const qlonglong messageId = messageData.value(ID).toLongLong(); @@ -288,7 +315,11 @@ void ChatModel::handleMessagesReceived(const QVariantList &messages, int totalCo if (!messagesToBeAdded.isEmpty() && (messagesToBeAdded.size() + messages.size()) < 10 && !inReload) { LOG("Only a few messages received in first call, loading more..."); this->inReload = true; - this->tdLibWrapper->getChatHistory(chatId, messagesToBeAdded.first()->messageId, 0); + if (this->searchModeActive) { + this->tdLibWrapper->searchChatMessages(chatId, searchQuery, messagesToBeAdded.first()->messageId); + } else { + this->tdLibWrapper->getChatHistory(chatId, messagesToBeAdded.first()->messageId, 0); + } } else { LOG("Messages loaded, notifying chat UI..."); this->inReload = false; diff --git a/src/chatmodel.h b/src/chatmodel.h index 9353037..e4ad47f 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -37,13 +37,14 @@ public: virtual int rowCount(const QModelIndex&) const override; virtual QVariant data(const QModelIndex &index, int role) const override; - Q_INVOKABLE void clear(); + Q_INVOKABLE void clear(bool contentOnly = false); Q_INVOKABLE void initialize(const QVariantMap &chatInformation); Q_INVOKABLE void triggerLoadMoreHistory(); Q_INVOKABLE void triggerLoadMoreFuture(); Q_INVOKABLE QVariantMap getChatInformation(); Q_INVOKABLE QVariantMap getMessage(int index); Q_INVOKABLE int getLastReadMessageIndex(); + Q_INVOKABLE void setSearchQuery(const QString newSearchQuery); QVariantMap smallPhoto() const; qlonglong getChatId() const; @@ -92,6 +93,8 @@ private: qlonglong chatId; bool inReload; bool inIncrementalUpdate; + bool searchModeActive; + QString searchQuery; }; #endif // CHATMODEL_H diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 42223a1..9df4e89 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -925,6 +925,20 @@ void TDLibWrapper::importContacts(const QVariantList &contacts) this->sendRequest(requestObject); } +void TDLibWrapper::searchChatMessages(const qlonglong &chatId, const QString &query, const qlonglong fromMessageId) +{ + LOG("Searching for messages" << chatId << query); + QVariantMap requestObject; + requestObject.insert(_TYPE, "searchChatMessages"); + requestObject.insert("chat_id", chatId); + requestObject.insert("query", query); + requestObject.insert("from_message_id", fromMessageId); + requestObject.insert("offset", 0); + requestObject.insert("limit", 100); + requestObject.insert(_EXTRA, "searchChatMessages"); + this->sendRequest(requestObject); +} + void TDLibWrapper::searchEmoji(const QString &queryString) { LOG("Searching emoji" << queryString); diff --git a/src/tdlibwrapper.h b/src/tdlibwrapper.h index 50eb2fb..5e10408 100644 --- a/src/tdlibwrapper.h +++ b/src/tdlibwrapper.h @@ -178,6 +178,7 @@ public: Q_INVOKABLE void getSecretChat(qlonglong secretChatId); Q_INVOKABLE void closeSecretChat(qlonglong secretChatId); Q_INVOKABLE void importContacts(const QVariantList &contacts); + Q_INVOKABLE void searchChatMessages(const qlonglong &chatId, const QString &query, const qlonglong fromMessageId = 0); // Others (candidates for extraction ;)) Q_INVOKABLE void searchEmoji(const QString &queryString); diff --git a/translations/harbour-fernschreiber-de.ts b/translations/harbour-fernschreiber-de.ts index 5e97037..eb1a53b 100644 --- a/translations/harbour-fernschreiber-de.ts +++ b/translations/harbour-fernschreiber-de.ts @@ -391,6 +391,14 @@ Close Chat Chat schließen + + Search in Chat + Im Chat suchen + + + Search in chat... + Im Chat suchen... + ChatSelectionPage @@ -1027,8 +1035,8 @@ Neuer Chat - Search a chat... - Einen Chat suchen... + Filter your chats... + Ihre Chats filtern... diff --git a/translations/harbour-fernschreiber-en.ts b/translations/harbour-fernschreiber-en.ts index 3ec3f7f..37bbccd 100644 --- a/translations/harbour-fernschreiber-en.ts +++ b/translations/harbour-fernschreiber-en.ts @@ -391,6 +391,14 @@ Close Chat Close Chat + + Search in Chat + Search in Chat + + + Search in chat... + Search in chat... + ChatSelectionPage @@ -1027,8 +1035,8 @@ New Chat - Search a chat... - + Filter your chats... + Filter your chats... diff --git a/translations/harbour-fernschreiber-es.ts b/translations/harbour-fernschreiber-es.ts index 0e89436..016a015 100644 --- a/translations/harbour-fernschreiber-es.ts +++ b/translations/harbour-fernschreiber-es.ts @@ -381,6 +381,14 @@ Close Chat Cerrar charla + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1016,7 +1024,7 @@ Nueva charla - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-fi.ts b/translations/harbour-fernschreiber-fi.ts index 871dce3..617a8b2 100644 --- a/translations/harbour-fernschreiber-fi.ts +++ b/translations/harbour-fernschreiber-fi.ts @@ -391,6 +391,14 @@ Close Chat Sulje keskustelu + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1028,7 +1036,7 @@ Uusi keskustelu - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-hu.ts b/translations/harbour-fernschreiber-hu.ts index b44ac76..7c8f35e 100644 --- a/translations/harbour-fernschreiber-hu.ts +++ b/translations/harbour-fernschreiber-hu.ts @@ -381,6 +381,14 @@ Close Chat + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1016,7 +1024,7 @@ - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-it.ts b/translations/harbour-fernschreiber-it.ts index c19d412..11ec827 100644 --- a/translations/harbour-fernschreiber-it.ts +++ b/translations/harbour-fernschreiber-it.ts @@ -391,6 +391,14 @@ Close Chat Chiudi chat + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1027,7 +1035,7 @@ Nuova chat - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-pl.ts b/translations/harbour-fernschreiber-pl.ts index 68d58b3..d279543 100644 --- a/translations/harbour-fernschreiber-pl.ts +++ b/translations/harbour-fernschreiber-pl.ts @@ -401,6 +401,14 @@ Close Chat Zamknij czat + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1038,7 +1046,7 @@ Nowy czat - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-ru.ts b/translations/harbour-fernschreiber-ru.ts index 8e1ee21..d76f441 100644 --- a/translations/harbour-fernschreiber-ru.ts +++ b/translations/harbour-fernschreiber-ru.ts @@ -401,6 +401,14 @@ Close Chat Закрыть чат + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1038,7 +1046,7 @@ Новый Чат - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-sv.ts b/translations/harbour-fernschreiber-sv.ts index 174b53b..8e64333 100644 --- a/translations/harbour-fernschreiber-sv.ts +++ b/translations/harbour-fernschreiber-sv.ts @@ -391,6 +391,14 @@ Close Chat Stäng chatten + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1027,7 +1035,7 @@ Ny chatt - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber-zh_CN.ts b/translations/harbour-fernschreiber-zh_CN.ts index f3c7d1d..a0b1304 100644 --- a/translations/harbour-fernschreiber-zh_CN.ts +++ b/translations/harbour-fernschreiber-zh_CN.ts @@ -381,6 +381,14 @@ Close Chat 关闭对话 + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1016,7 +1024,7 @@ 新对话 - Search a chat... + Filter your chats... diff --git a/translations/harbour-fernschreiber.ts b/translations/harbour-fernschreiber.ts index cf0789a..abe8bad 100644 --- a/translations/harbour-fernschreiber.ts +++ b/translations/harbour-fernschreiber.ts @@ -391,6 +391,14 @@ Close Chat + + Search in Chat + + + + Search in chat... + + ChatSelectionPage @@ -1027,7 +1035,7 @@ - Search a chat... + Filter your chats...