// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "DayEventsModel.hpp" #include "application-calendar/widgets/DayEventsItem.hpp" #include "application-calendar/data/CalendarData.hpp" #include "application-calendar/ApplicationCalendar.hpp" #include #include #include #include #include DayEventsModel::DayEventsModel(app::Application *app) : DatabaseModel(app), app::AsyncCallbackReceiver{app}, application(app) {} unsigned int DayEventsModel::requestRecordsCount() { return recordsCount; } unsigned int DayEventsModel::getMinimalItemHeight() const { return style::window::calendar::item::dayEvents::height; } void DayEventsModel::requestRecords(const uint32_t offset, const uint32_t limit) { auto query = std::make_unique(filterFrom, offset, limit); auto task = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Events); task->setCallback([this](auto response) { return handleQueryResponse(response); }); task->execute(application, this); } gui::ListItem *DayEventsModel::getItem(gui::Order order) { auto record = getRecord(order); if (record == nullptr) { LOG_DEBUG("Empty record in DayEventsModel::GetItem"); return nullptr; } auto *item = new gui::DayEventsItem(); item->setEvent(record); item->activatedCallback = [=](gui::Item &item) { LOG_INFO("Switch to event details window"); auto rec = std::make_unique(*record); auto data = std::make_unique(std::move(rec)); application->switchWindow(style::window::calendar::name::details_window, std::move(data)); return true; }; return item; } bool DayEventsModel::updateRecords(std::vector records) { DatabaseModel::updateRecords(std::move(records)); list->onProviderDataUpdate(); return true; } auto DayEventsModel::handleQueryResponse(db::QueryResult *queryResult) -> bool { if (auto response = dynamic_cast(queryResult); response != nullptr) { if (recordsCount != (response->getCountResult())) { recordsCount = response->getCountResult(); list->rebuildList(gui::listview::RebuildType::Full, 0, true); return false; } if (auto app = dynamic_cast(application); app != nullptr) { if (response->getCountResult() == 0) { LOG_DEBUG("Empty records"); if (app->getEquivalentToEmptyWindow() == EquivalentWindow::DayEventsWindow) { app->switchToNoEventsWindow(dayMonthTitle, filterFrom); return true; } } auto eventShift = app->getEventShift(); auto records = response->getResult(); if (eventShift) { for (auto &record : records) { record.date_from += std::chrono::hours(eventShift); record.date_till += std::chrono::hours(eventShift); } } return updateRecords(std::move(records)); } LOG_ERROR("App is not a calendar"); return false; } LOG_ERROR("GetFilteredResult response is nullptr"); return false; } void DayEventsModel::setFilters(TimePoint from, TimePoint till, const std::string &dayMonth) { /// TODO: change to one filter this->filterFrom = from; this->filterTill = till; this->dayMonthTitle = dayMonth; }