From 2aaed62bd8ae5ab848f84fd7ba9f78a7d992d272 Mon Sep 17 00:00:00 2001 From: KacperLewandowski Date: Wed, 23 Sep 2020 01:06:00 +0200 Subject: [PATCH] [EGD-3699] Add notifications handling in calendar. --- changelog.md | 7 ++ .../ApplicationCalendar.cpp | 31 +++++- .../models/AllEventsModel.cpp | 23 ++++- .../models/AllEventsModel.hpp | 4 +- .../widgets/CalendarStyle.hpp | 4 +- .../windows/AllEventsWindow.cpp | 33 ++----- .../windows/CalendarMainWindow.cpp | 67 ++++++------- .../windows/CalendarMainWindow.hpp | 2 +- .../windows/DayEventsWindow.cpp | 56 ++++++----- .../windows/DayEventsWindow.hpp | 1 + module-db/Interface/EventsRecord.cpp | 99 +++++++++++-------- module-db/Interface/EventsRecord.hpp | 16 +-- module-db/Tables/EventsTable.cpp | 2 +- 13 files changed, 205 insertions(+), 140 deletions(-) diff --git a/changelog.md b/changelog.md index 62380431c363ce6347cd2e2c73a9a2ec8e98b228..804e8fb9ce8f41e40605ac8117983af873628bcf 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,12 @@ # Mudita PurePhone changelog +## [Current release] + +### Added + +* `[calendar]` Add notifications from database handling. + + ## [0.39.1 2020-09-25] ### Added diff --git a/module-apps/application-calendar/ApplicationCalendar.cpp b/module-apps/application-calendar/ApplicationCalendar.cpp index 616bc525df8e7d0de404d7b02a2929256627013f..8d398b76e7a6269b37dc0450650d1e38ba6ee57a 100644 --- a/module-apps/application-calendar/ApplicationCalendar.cpp +++ b/module-apps/application-calendar/ApplicationCalendar.cpp @@ -12,6 +12,7 @@ #include #include #include +#include namespace app { @@ -41,7 +42,9 @@ namespace app uint32_t stackDepth, sys::ServicePriority priority) : Application(name, parent, false, stackDepth, priority) - {} + { + busChannels.push_back(sys::BusChannels::ServiceDBNotifications); + } sys::Message_t ApplicationCalendar::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp) { @@ -50,6 +53,19 @@ namespace app if (retMsg && (dynamic_cast(retMsg.get())->retCode == sys::ReturnCodes::Success)) { return retMsg; } + + auto msg = dynamic_cast(msgl); + if (msg != nullptr) { + LOG_DEBUG("Received notification"); + // window-specific actions + if (msg->interface == db::Interface::Name::Events) { + for (auto &[name, window] : windows) { + window->onDatabaseMessage(msg); + } + } + return std::make_shared(); + } + // this variable defines whether message was processed. bool handled = false; // handle database response @@ -57,8 +73,17 @@ namespace app handled = true; switch (resp->responseTo) { case MessageType::DBQuery: { - if (getCurrentWindow()->onDatabaseMessage(resp)) - refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); + + if (auto queryResponse = dynamic_cast(resp)) { + auto result = queryResponse->getResult(); + LOG_DEBUG("queryResponse != nullptr"); + if (result->hasListener()) { + LOG_DEBUG("Has listener"); + if (result->handle()) { + refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST); + } + } + } } break; default: break; diff --git a/module-apps/application-calendar/models/AllEventsModel.cpp b/module-apps/application-calendar/models/AllEventsModel.cpp index b52bb9e5a8319df0e24e2eb8897ea2c56770afc4..858f5b5542e4c25c1ce80bc8f201a111459b3e71 100644 --- a/module-apps/application-calendar/models/AllEventsModel.cpp +++ b/module-apps/application-calendar/models/AllEventsModel.cpp @@ -2,6 +2,7 @@ #include "application-calendar/widgets/AllEventsItem.hpp" #include "application-calendar/widgets/CalendarStyle.hpp" #include "module-apps/application-calendar/data/CalendarData.hpp" +#include "module-apps/application-calendar/ApplicationCalendar.hpp" #include #include #include @@ -20,6 +21,8 @@ unsigned int AllEventsModel::requestRecordsCount() void AllEventsModel::requestRecords(const uint32_t offset, const uint32_t limit) { auto query = std::make_unique(offset, limit); + query->setQueryListener( + db::QueryCallback::fromFunction([this](auto response) { return handleQueryResponse(response); })); DBServiceAPI::GetQuery(application, db::Interface::Name::Events, std::move(query)); } @@ -56,7 +59,23 @@ bool AllEventsModel::updateRecords(std::unique_ptr> re list->onProviderDataUpdate(); return true; } -void AllEventsModel::setRecordsCount(const uint32_t count) + +auto AllEventsModel::handleQueryResponse(db::QueryResult *queryResult) -> bool { - list->setElementsCount(count); + auto response = dynamic_cast(queryResult); + assert(response != nullptr); + + auto records_data = response->getResult(); + list->setElementsCount(*response->getCountResult()); + auto records = std::make_unique>(records_data->begin(), records_data->end()); + + if (records->empty()) { + auto app = dynamic_cast(application); + assert(application != nullptr); + auto filter = std::chrono::system_clock::now(); + app->switchToNoEventsWindow( + utils::localize.get("app_calendar_title_main"), filter, style::window::calendar::name::all_events_window); + } + + return this->updateRecords(std::move(records)); } diff --git a/module-apps/application-calendar/models/AllEventsModel.hpp b/module-apps/application-calendar/models/AllEventsModel.hpp index 7d41d25e85a508a28082844b1d8bd26dc30d2393..b6e6c3a2ef6e909dcc1cb700dbac5c6073934d7c 100644 --- a/module-apps/application-calendar/models/AllEventsModel.hpp +++ b/module-apps/application-calendar/models/AllEventsModel.hpp @@ -1,9 +1,9 @@ #pragma once - #include "Application.hpp" #include #include #include +#include class AllEventsModel : public app::DatabaseModel, public gui::ListItemProvider { @@ -13,10 +13,10 @@ class AllEventsModel : public app::DatabaseModel, public gui::List AllEventsModel(app::Application *app); virtual ~AllEventsModel() override = default; - void setRecordsCount(const uint32_t count); void requestRecords(const uint32_t offset, const uint32_t limit) override; bool updateRecords(std::unique_ptr> records) override; + auto handleQueryResponse(db::QueryResult *) -> bool; // virtual methods for ListViewProvider [[nodiscard]] unsigned int getMinimalItemHeight() const override; gui::ListItem *getItem(gui::Order order) override; diff --git a/module-apps/application-calendar/widgets/CalendarStyle.hpp b/module-apps/application-calendar/widgets/CalendarStyle.hpp index 3c4f357dbfa112964a3187e128d3469a2d330017..f0be6766761e3f68758d42f5b36f0fee5c7c862f 100644 --- a/module-apps/application-calendar/widgets/CalendarStyle.hpp +++ b/module-apps/application-calendar/widgets/CalendarStyle.hpp @@ -82,11 +82,11 @@ namespace style namespace eventDetail { - const inline int height_min = 90; + const inline int height_min = 90; const inline int height_max = 155; const inline int margin_top = 2 * style::margins::big; const inline int event_time_margin = 25; - const inline int title_h = 20; + const inline int title_h = 20; const inline int label_h = 35; } // namespace eventDetail diff --git a/module-apps/application-calendar/windows/AllEventsWindow.cpp b/module-apps/application-calendar/windows/AllEventsWindow.cpp index ece090629a7f8bd5678644837740533590268e30..b5ff1389c03468da47a5cb39f753e689823678b6 100644 --- a/module-apps/application-calendar/windows/AllEventsWindow.cpp +++ b/module-apps/application-calendar/windows/AllEventsWindow.cpp @@ -1,5 +1,4 @@ #include "AllEventsWindow.hpp" -#include "module-apps/application-calendar/ApplicationCalendar.hpp" #include "module-apps/application-calendar/data/CalendarData.hpp" #include #include @@ -10,6 +9,7 @@ #include #include #include