From 9b339ecf5b451e5178297008ca96fed28759c23f Mon Sep 17 00:00:00 2001 From: Lefucjusz Date: Tue, 4 Jul 2023 16:26:08 +0200 Subject: [PATCH] [MOS-1003] Fix automatically marking new message as read Fix of the issue that after entering messages app, opening one of the threads and returning to main messages app window, new messages in this thread were automatically marked as read. --- .../models/SMSThreadModel.cpp | 5 +- .../models/SMSThreadModel.hpp | 2 +- .../windows/SMSThreadViewWindow.cpp | 51 ++++++++++--------- .../windows/SMSThreadViewWindow.hpp | 6 +-- module-db/Interface/ThreadRecord.hpp | 26 +++++----- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/module-apps/application-messages/models/SMSThreadModel.cpp b/module-apps/application-messages/models/SMSThreadModel.cpp index 764f7c6d30390ef8a74806aa607fc1d502b9f213..d3cfe401025982326df5f9ceecad220124cb0181 100644 --- a/module-apps/application-messages/models/SMSThreadModel.cpp +++ b/module-apps/application-messages/models/SMSThreadModel.cpp @@ -53,7 +53,7 @@ unsigned int SMSThreadModel::requestRecordsCount() return recordsCount; } -void SMSThreadModel::requestRecords(uint32_t offset, uint32_t limit) +void SMSThreadModel::requestRecords(std::uint32_t offset, std::uint32_t limit) { auto query = std::make_unique(smsThreadID, offset, limit, numberID); auto task = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::SMS); @@ -80,8 +80,6 @@ auto SMSThreadModel::handleQueryResponse(db::QueryResult *queryResult) -> bool // Additional one element for SMSInputWidget. recordsCount = msgResponse->getCount() + 1; list->reSendLastRebuildRequest(); - - markCurrentThreadAsRead(); return false; } @@ -129,6 +127,7 @@ void SMSThreadModel::resetInputWidget() smsInput->clearNavigationItem(gui::NavigationDirection::UP); smsInput->clearNavigationItem(gui::NavigationDirection::DOWN); } + void SMSThreadModel::markCurrentThreadAsRead() { const auto [code, msg] = DBServiceAPI::GetQueryWithReply(application, diff --git a/module-apps/application-messages/models/SMSThreadModel.hpp b/module-apps/application-messages/models/SMSThreadModel.hpp index e9853036f5bf0e0a47348accd41e5c70b4960abb..82d02be591b7085164e4f07efd30150ab9dd6a2a 100644 --- a/module-apps/application-messages/models/SMSThreadModel.hpp +++ b/module-apps/application-messages/models/SMSThreadModel.hpp @@ -32,7 +32,7 @@ class SMSThreadModel : public app::DatabaseModel, unsigned int requestRecordsCount() override; bool updateRecords(std::vector records) override; - void requestRecords(uint32_t offset, uint32_t limit) override; + void requestRecords(std::uint32_t offset, std::uint32_t limit) override; unsigned int getMinimalItemSpaceRequired() const override; gui::ListItem *getItem(gui::Order order) override; }; diff --git a/module-apps/application-messages/windows/SMSThreadViewWindow.cpp b/module-apps/application-messages/windows/SMSThreadViewWindow.cpp index 3ebd334ed6aa25fbf948323f27a7c297f6b2dc10..0f082328fd0e193e2fbd2f04a94a746d8194dcf8 100644 --- a/module-apps/application-messages/windows/SMSThreadViewWindow.cpp +++ b/module-apps/application-messages/windows/SMSThreadViewWindow.cpp @@ -15,11 +15,8 @@ #include #include -#include - namespace gui { - SMSThreadViewWindow::SMSThreadViewWindow(app::ApplicationCommon *app) : AppWindow(app, name::window::thread_view), app::AsyncCallbackReceiver{app}, smsModel{std::make_shared(app)} @@ -62,24 +59,23 @@ namespace gui void SMSThreadViewWindow::onBeforeShow(ShowMode mode, SwitchData *data) { if (mode == ShowMode::GUI_SHOW_RETURN) { + smsModel->markCurrentThreadAsRead(); smsList->rebuildList(); } - if (auto pdata = dynamic_cast(data); pdata) { + if (const auto pdata = dynamic_cast(data); pdata != nullptr) { LOG_INFO("Thread data received: %" PRIu32, pdata->thread->ID); saveInfoAboutPreviousAppForProperSwitchBack(data); - requestContact(pdata->thread->numberID); - - // Mark thread as Read - if (pdata->thread->isUnread()) { - auto app = dynamic_cast(application); - assert(app != nullptr); - if (application->getCurrentWindow() == this) { - app->markSmsThreadAsRead(pdata->thread.get()); - } - } + smsModel->numberID = pdata->thread->numberID; smsModel->smsThreadID = pdata->thread->ID; + requestContact(smsModel->numberID); + + // Mark thread as read + if (pdata->thread->isUnread() && (this == application->getCurrentWindow())) { + smsModel->markCurrentThreadAsRead(); + } + smsList->rebuildList(); } else if (smsModel->numberID != DB_ID_NONE) { @@ -125,20 +121,27 @@ namespace gui bool SMSThreadViewWindow::onDatabaseMessage(sys::Message *msgl) { const auto msg = dynamic_cast(msgl); - if (msg != nullptr) { - if ((msg->interface == db::Interface::Name::SMS) && msg->dataModified()) { - rebuild(); - return true; - } - if ((msg->interface == db::Interface::Name::SMSThread) && (msg->type == db::Query::Type::Delete) && - (this == application->getCurrentWindow())) { + if (msg == nullptr) { + return false; + } + + if ((msg->interface == db::Interface::Name::SMS) && msg->dataModified()) { + rebuild(); + return true; + } + + if ((msg->interface == db::Interface::Name::SMSThread) && (this == application->getCurrentWindow())) { + if (msg->type == db::Query::Type::Delete) { application->switchWindow(gui::name::window::main_window); } + else { + smsModel->markCurrentThreadAsRead(); + } } return false; } - auto SMSThreadViewWindow::requestContact(unsigned int numberID) -> void + void SMSThreadViewWindow::requestContact(unsigned int numberID) { auto query = std::make_unique(numberID); auto task = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Contact); @@ -152,7 +155,7 @@ namespace gui task->execute(application, this); } - auto SMSThreadViewWindow::handleContactQueryResponse(db::QueryResult *queryResult) -> bool + bool SMSThreadViewWindow::handleContactQueryResponse(db::QueryResult *queryResult) { auto msgResponse = dynamic_cast(queryResult); if (msgResponse == nullptr) { @@ -173,7 +176,7 @@ namespace gui task->execute(application, this); } - auto SMSThreadViewWindow::handleNumberQueryResponse(db::QueryResult *queryResult) -> bool + bool SMSThreadViewWindow::handleNumberQueryResponse(db::QueryResult *queryResult) { auto msgResponse = dynamic_cast(queryResult); if (msgResponse == nullptr) { diff --git a/module-apps/application-messages/windows/SMSThreadViewWindow.hpp b/module-apps/application-messages/windows/SMSThreadViewWindow.hpp index 2e35723785f93def0c1e225be08d36748ffda7c3..8dd16621b09b85a81fac64f5b7585664821bb2e6 100644 --- a/module-apps/application-messages/windows/SMSThreadViewWindow.hpp +++ b/module-apps/application-messages/windows/SMSThreadViewWindow.hpp @@ -22,10 +22,10 @@ namespace gui std::shared_ptr smsModel; gui::ListView *smsList = nullptr; - auto requestContact(unsigned int numberID) -> void; - auto handleContactQueryResponse(db::QueryResult *) -> bool; + void requestContact(unsigned int numberID); + bool handleContactQueryResponse(db::QueryResult *); void requestNumber(unsigned int numberID); - auto handleNumberQueryResponse(db::QueryResult *) -> bool; + bool handleNumberQueryResponse(db::QueryResult *); public: explicit SMSThreadViewWindow(app::ApplicationCommon *app); diff --git a/module-db/Interface/ThreadRecord.hpp b/module-db/Interface/ThreadRecord.hpp index 27e61e378ba6343aa79f98998832cb79b3f1d62c..fb035ac5c9f485c3e5fb8084d0fc0140211a9340 100644 --- a/module-db/Interface/ThreadRecord.hpp +++ b/module-db/Interface/ThreadRecord.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved. +// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #pragma once @@ -13,14 +13,15 @@ #include #include + struct ThreadRecord : Record { - uint32_t date = 0; - uint32_t msgCount = 0; - uint32_t unreadMsgCount = 0; + std::uint32_t date = 0; + std::uint32_t msgCount = 0; + std::uint32_t unreadMsgCount = 0; UTF8 snippet; SMSType type = SMSType::UNKNOWN; - uint32_t numberID = DB_ID_NONE; + std::uint32_t numberID = DB_ID_NONE; ThreadRecord() = default; ThreadRecord(const ThreadsTableRow &rec) @@ -47,19 +48,18 @@ class ThreadRecordInterface : public RecordInterface> GetLimitOffset(uint32_t offset, uint32_t limit) override final; + std::unique_ptr> GetLimitOffset(std::uint32_t offset, std::uint32_t limit) override final; - std::unique_ptr> GetLimitOffsetByField(uint32_t offset, - uint32_t limit, + std::unique_ptr> GetLimitOffsetByField(std::uint32_t offset, + std::uint32_t limit, ThreadRecordField field, const char *str) override final;