~aleteoryx/muditaos

ref: 040550f3b72f2f2d57c2171b963da301ef00cf4f muditaos/module-apps/application-settings-new/models/QuotesModel.cpp -rw-r--r-- 5.4 KiB
040550f3 — Pawel.Paprocki [BH-356] Move TPLIB (microtar) to separate directory 4 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "application-settings-new/windows/QuotesMainWindow.hpp"
#include "application-settings-new/ApplicationSettings.hpp"
#include "QuotesModel.hpp"

#include <InputEvent.hpp>
#include <i18n/i18n.hpp>
#include <json/json11.hpp>
#include <Utils.hpp>
#include <string>
#include <utility>
#include <service-db/QuotesMessages.hpp>

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

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

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

    bool QuotesModel::updateRecords(std::vector<QuoteRecord> records)
    {
        if (DatabaseModel::updateRecords(std::move(records))) {
            list->onProviderDataUpdate();
            return true;
        }
        return false;
    }

    auto QuotesModel::getMinimalItemHeight() 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()->bottomBarTemporaryMode(text, gui::BottomBar::Side::CENTER, false);
            },
            [app = app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); });

        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)
    {
        LOG_DEBUG("Adding quote: lang_id = %u, author = %s, quote = %s",
                  static_cast<unsigned>(record.lang_id),
                  record.quote.c_str(),
                  record.author.c_str());

        auto query = std::make_unique<Messages::AddQuoteRequest>(record.lang_id, 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)
    {
        LOG_DEBUG("Saving quote: lang_id = %u, author = %s, quote = %s",
                  static_cast<unsigned>(record.lang_id),
                  record.quote.c_str(),
                  record.author.c_str());

        auto query = std::make_unique<Messages::WriteQuoteRequest>(
            record.quote_id, record.lang_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)
    {
        LOG_DEBUG("Removing quote: lang_id = %u, author = %s, quote = %s",
                  static_cast<unsigned>(record.lang_id),
                  record.quote.c_str(),
                  record.author.c_str());

        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