~aleteoryx/muditaos

ref: 7d7003d62639426be8eb00f4fc288b68139f5566 muditaos/module-apps/application-calendar/models/DayEventsModel.cpp -rw-r--r-- 3.6 KiB
7d7003d6 — Marcin Smoczyński Merge branch 'master' into stable 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2017-2020, 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 <ListView.hpp>
#include <queries/calendar/QueryEventsGetFiltered.hpp>
#include <service-db/DBServiceAPI.hpp>
#include <service-db/QueryMessage.hpp>
#include <module-db/queries/RecordQuery.hpp>

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<db::query::events::GetFiltered>(filterFrom, filterTill, 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<EventsRecord>(*record);
        auto data = std::make_unique<EventRecordData>(std::move(rec));
        application->switchWindow(style::window::calendar::name::details_window, std::move(data));
        return true;
    };

    return item;
}

bool DayEventsModel::updateRecords(std::vector<EventsRecord> records)
{
    DatabaseModel::updateRecords(std::move(records));
    list->onProviderDataUpdate();
    return true;
}

auto DayEventsModel::handleQueryResponse(db::QueryResult *queryResult) -> bool
{
    if (auto response = dynamic_cast<db::query::events::GetFilteredResult *>(queryResult); response != nullptr) {
        if (recordsCount != (response->getCountResult())) {
            recordsCount = response->getCountResult();
            list->rebuildList(style::listview::RebuildType::Full, 0, true);
            return false;
        }

        auto records = response->getResult();
        if (auto app = dynamic_cast<app::ApplicationCalendar *>(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();
            if (eventShift) {
                for (auto &record : records) {
                    record.date_from += hours(eventShift);
                    record.date_till += 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)
{
    this->filterFrom    = from;
    this->filterTill    = till;
    this->dayMonthTitle = dayMonth;
}