~aleteoryx/muditaos

ref: 0e8b4c848e3f87f3bbb1f64ef7460cf56017b87f muditaos/module-apps/application-settings/models/display-keypad/QuotesModel.cpp -rw-r--r-- 4.7 KiB
0e8b4c84 — Lefucjusz [BH-2108] Fix misaligned charging symbol 3 months 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "QuotesModel.hpp"

#include <application-settings/widgets/display-keypad/QuoteWidget.hpp>
#include <application-settings/windows/WindowNames.hpp>

#include <InputEvent.hpp>
#include <ListView.hpp>
#include <AppWindow.hpp>

namespace style::quotes::list
{
    constexpr auto item_height = 63;
} // namespace style::quotes::list

namespace Quotes
{
    QuotesModel::QuotesModel(app::ApplicationCommon *application)
        : DatabaseModel(application), app::AsyncCallbackReceiver{application}, app(application)
    {}

    auto QuotesModel::requestRecordsCount() -> unsigned int
    {
        return recordsCount;
    }

    bool QuotesModel::updateRecords(std::vector<QuoteRecord> records)
    {
        auto status = DatabaseModel::updateRecords(std::move(records));
        list->onProviderDataUpdate();
        return status;
    }

    auto QuotesModel::getMinimalItemSpaceRequired() const -> unsigned int
    {
        return style::quotes::list::item_height;
    }

    void QuotesModel::requestRecords(const uint32_t offset, const uint32_t limit)
    {
        LOG_DEBUG(
            "Request quotes: offset = %u, limit = %u", static_cast<unsigned>(offset), static_cast<unsigned>(limit));
        auto query = std::make_unique<Messages::GetQuotesListFromCustomCategoryRequest>(offset, limit);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Quotes);
        task->setCallback([this](auto response) { return handleQueryResponse(response); });
        task->execute(app, this);
    }

    auto QuotesModel::handleQueryResponse(db::QueryResult *queryResult) -> bool
    {
        auto msgResponse = dynamic_cast<Messages::GetQuotesListFromCustomCategoryResponse *>(queryResult);
        assert(msgResponse != nullptr);

        // If list record count has changed we need to rebuild list.
        if (recordsCount != (msgResponse->getCount())) {
            recordsCount = msgResponse->getCount();
            list->reSendLastRebuildRequest();
            return false;
        }

        LOG_DEBUG("Quotes count: %u", static_cast<unsigned>(msgResponse->getCount()));
        auto records = msgResponse->getResults();
        return this->updateRecords(std::move(records));
    }

    auto QuotesModel::getItem(gui::Order order) -> gui::ListItem *
    {
        auto quote = getRecord(order);

        if (!quote) {
            return nullptr;
        }

        auto item = new gui::QuoteWidget(
            *quote,
            [this, &quote = *quote](bool enable) {
                LOG_DEBUG("Sending enable quote request: quote_id = %u, enable = %d",
                          static_cast<unsigned>(quote.quote_id),
                          enable);

                auto query = std::make_unique<Messages::EnableQuoteByIdRequest>(quote.quote_id, enable);
                auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Quotes);
                task->execute(app, this);
            },
            [app = app](const UTF8 &text) {
                app->getCurrentWindow()->navBarTemporaryMode(text, gui::nav_bar::Side::Center, false);
            },
            [app = app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); });

        item->inputCallback = [app = app, item](gui::Item &, const gui::InputEvent &event) {
            if (event.isShortRelease(gui::KeyCode::KEY_LF)) {
                app->switchWindow(gui::window::name::options_quote,
                                  std::make_unique<gui::QuoteSwitchData>(gui::QuoteAction::None, item->getQuoteData()));
            }
            return false;
        };

        return item;
    }

    void QuotesModel::add(const Quotes::QuoteRecord &record)
    {
        auto query = std::make_unique<Messages::AddQuoteRequest>(record.quote, record.author, true);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Quotes);
        task->execute(app, this);
    }

    void QuotesModel::edit(const Quotes::QuoteRecord &record)
    {
        auto query =
            std::make_unique<Messages::WriteQuoteRequest>(record.quote_id, record.quote, record.author, record.enabled);
        auto task = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Quotes);
        task->execute(app, this);
    }

    void QuotesModel::remove(const Quotes::QuoteRecord &record)
    {
        auto query = std::make_unique<Messages::DeleteQuoteRequest>(record.quote_id);
        auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Quotes);
        task->execute(app, this);
    }

} // namespace Quotes