~aleteoryx/muditaos

muditaos/module-apps/application-notes/model/NotesListModel.cpp -rw-r--r-- 2.9 KiB
a405cad6Aleteoryx trim readme 6 days 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
// 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 "NotesListModel.hpp"

#include "module-apps/application-notes/widgets/NotesItem.hpp"
#include "module-apps/application-notes/style/NotesListStyle.hpp"
#include "module-apps/application-notes/windows/NotesOptions.hpp"
#include "module-apps/application-notes/data/NoteSwitchData.hpp"
#include <apps-common/messages/OptionsWindow.hpp>
#include <apps-common/options/OptionWindowName.hpp>
#include <module-gui/gui/widgets/ListView.hpp>
#include <module-gui/gui/input/InputEvent.hpp>

namespace app::notes
{
    NotesListItemProvider::NotesListItemProvider(ApplicationCommon *app) : DatabaseModel(app)
    {}

    NotesListModel::NotesListModel(app::ApplicationCommon *app,
                                   std::shared_ptr<AbstractNotesRepository> notesRepository)
        : NotesListItemProvider(app), notesRepository{std::move(notesRepository)}
    {}

    unsigned int NotesListModel::requestRecordsCount()
    {
        return recordsCount;
    }

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

    unsigned int NotesListModel::getMinimalItemSpaceRequired() const
    {
        return style::list::item::Height;
    }

    gui::ListItem *NotesListModel::getItem(gui::Order order)
    {
        std::shared_ptr<NotesRecord> note = getRecord(order);
        if (!note) {
            return nullptr;
        }

        auto item               = new gui::NotesItem(note);
        item->activatedCallback = [this, note](gui::Item &) {
            application->switchWindow(gui::name::window::note_preview, std::make_unique<NoteSwitchData>(note));
            return true;
        };
        item->inputCallback = [this, note = note.get()](gui::Item &, const gui::InputEvent &event) {
            if (event.isShortRelease(gui::KeyCode::KEY_LF)) {
                application->switchWindow(
                    window::name::option_window,
                    std::make_unique<gui::OptionsWindowOptions>(noteListOptions(application, *note, *notesRepository)));
            }
            return false;
        };
        return item;
    }

    void NotesListModel::requestRecords(uint32_t offset, uint32_t limit)
    {
        notesRepository->get(
            offset, limit, [this](const std::vector<NotesRecord> &records, unsigned int notesRepoCount) {
                return onNotesRetrieved(records, notesRepoCount);
            });
    }

    bool NotesListModel::onNotesRetrieved(const std::vector<NotesRecord> &records, unsigned int notesRepoCount)
    {
        if (recordsCount != notesRepoCount) {
            recordsCount = notesRepoCount;
            list->reSendLastRebuildRequest();
            return false;
        }
        return updateRecords(records);
    }
} // namespace app::notes