~aleteoryx/muditaos

ref: 09d095b355dfd7b978f67166059bf60a06d00a04 muditaos/module-apps/application-settings-new/model/QuotesModel.cpp -rw-r--r-- 3.1 KiB
09d095b3 — RobertPiet [EGD-4960] license 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
// 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 "application-settings-new/model/QuotesRepository.hpp"
#include "application-settings-new/model/QuotesModel.hpp"
#include "application-settings-new/model/QuotesModel.hpp"

#include <InputEvent.hpp>
#include <i18n/i18n.hpp>
#include <json/json11.hpp>
#include <Utils.hpp>
#include <string>
#include <utility>

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

namespace app
{
    QuotesModel::QuotesModel(app::Application *app, std::unique_ptr<QuotesRepository> repository)
        : application(app), repository(std::move(repository))
    {}

    auto QuotesModel::requestRecordsCount() -> unsigned int
    {
        return internalData.size();
    }

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

    void QuotesModel::requestRecords(const uint32_t offset, const uint32_t limit)
    {
        setupModel(offset, limit);
        list->onProviderDataUpdate();
    }

    auto QuotesModel::getItem(gui::Order order) -> gui::ListItem *
    {
        auto app   = application;
        auto *item = dynamic_cast<gui::QuoteWidget *>(getRecord(order));

        if (item != nullptr) {
            item->inputCallback = [app, item](gui::Item &, const gui::InputEvent &event) {
                if (event.isShortPress() && event.is(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::rebuild()
    {
        list->clear();
        eraseInternalData();
        createData();
        list->rebuildList();
    }

    void QuotesModel::createData()
    {
        repository->get(0, style::quotes::list::max_quotes, [this](const std::list<QuoteRecord> &quotes, unsigned int) {
            auto app = application;
            for (const auto &quote : quotes) {
                auto item = new gui::QuoteWidget(
                    quote,
                    [app](const UTF8 &text) {
                        app->getCurrentWindow()->bottomBarTemporaryMode(text, gui::BottomBar::Side::CENTER, false);
                    },
                    [app]() { app->getCurrentWindow()->bottomBarRestoreFromTemporaryMode(); });

                item->deleteByList = false;
                internalData.push_back(item);
            }
            return true;
        });
    }

    void QuotesModel::remove(const app::QuoteRecord &quote)
    {
        repository->remove(quote);
    }

    void QuotesModel::save(const app::QuoteRecord &quote)
    {
        repository->save(quote);
    }

} // namespace app