~aleteoryx/muditaos

ref: dd178eb74adbecd76c7f3506520228d7073cc5c6 muditaos/module-apps/application-notes/windows/NoteMainWindow.cpp -rw-r--r-- 6.7 KiB
dd178eb7 — Maciej Janicki [EGD-6958] Fix rssibar oor exception 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NoteMainWindow.hpp"

#include <module-apps/application-notes/ApplicationNotes.hpp>
#include <module-apps/application-notes/data/NoteSwitchData.hpp>

#include <InputEvent.hpp>

#include <i18n/i18n.hpp>

#include <Style.hpp>
#include <module-apps/application-notes/style/NotesListStyle.hpp>
#include <module-apps/application-notes/style/NotesMainWindowStyle.hpp>

#include <module-services/service-db/service-db/DBNotificationMessage.hpp>

namespace app::notes
{
    NoteMainWindow::NoteMainWindow(app::Application *app,
                                   std::unique_ptr<NotesMainWindowContract::Presenter> &&windowPresenter)
        : AppWindow(app, gui::name::window::main_window), presenter{std::move(windowPresenter)}
    {
        presenter->attach(this);
        buildInterface();
    }

    NoteMainWindow::~NoteMainWindow() noexcept
    {
        destroyInterface();
    }

    void NoteMainWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void NoteMainWindow::buildInterface()
    {
        AppWindow::buildInterface();

        setTitle(utils::translate("app_notes_title_main"));

        bottomBar->setActive(gui::BottomBar::Side::LEFT, true);
        bottomBar->setText(gui::BottomBar::Side::LEFT, utils::translate(::style::strings::common::options));
        bottomBar->setActive(gui::BottomBar::Side::CENTER, true);
        bottomBar->setText(gui::BottomBar::Side::CENTER, utils::translate(::style::strings::common::open));
        bottomBar->setActive(gui::BottomBar::Side::RIGHT, true);
        bottomBar->setText(gui::BottomBar::Side::RIGHT, utils::translate(::style::strings::common::back));

        namespace windowStyle = app::notes::style::main_window;
        leftArrowImage        = new gui::Image(this,
                                        windowStyle::new_note_arrow::X,
                                        windowStyle::new_note_arrow::Y,
                                        0,
                                        0,
                                        windowStyle::new_note_arrow::ImageSource);
        rightArrowImage       = new gui::Image(this,
                                         windowStyle::search_arrow::X,
                                         windowStyle::search_arrow::Y,
                                         0,
                                         0,
                                         windowStyle::search_arrow::ImageSource);
        newNoteImage          = new gui::Image(this,
                                      windowStyle::new_note_image::X,
                                      windowStyle::new_note_image::Y,
                                      0,
                                      0,
                                      windowStyle::new_note_image::ImageSource);
        searchImage           = new gui::Image(this,
                                     windowStyle::search_image::X,
                                     windowStyle::search_image::Y,
                                     0,
                                     0,
                                     windowStyle::search_image::ImageSource);

        namespace listStyle = app::notes::style::list;
        list                = new gui::ListView(this,
                                 listStyle::X,
                                 listStyle::Y,
                                 listStyle::Width,
                                 listStyle::Height,
                                 presenter->getNotesItemProvider(),
                                 gui::listview::ScrollBarType::Fixed);
        list->setPenWidth(listStyle::PenWidth);
        list->setPenFocusWidth(listStyle::FocusedPenWidth);
        list->focusChangedCallback = [this]([[maybe_unused]] gui::Item &item) {
            onListFilled();
            return true;
        };
        list->setVisible(true);
        list->rebuildList();

        emptyListIcon =
            new gui::Icon(this,
                          0,
                          ::style::window::default_vertical_pos,
                          ::style::window_width,
                          ::style::window_height - ::style::window::default_vertical_pos - ::style::footer::height,
                          "phonebook_empty_grey_circle_W_G",
                          utils::translate("app_notes_no_notes"));
        emptyListIcon->focusChangedCallback = [this]([[maybe_unused]] gui::Item &item) {
            onEmptyList();
            return true;
        };

        list->emptyListCallback    = [this]() { onEmptyList(); };
        list->notEmptyListCallback = [this]() { onListFilled(); };

        setFocusItem(list);
    }

    void NoteMainWindow::destroyInterface()
    {
        erase();
        list            = nullptr;
        leftArrowImage  = nullptr;
        rightArrowImage = nullptr;
        newNoteImage    = nullptr;
        searchImage     = nullptr;
        emptyListIcon   = nullptr;
    }

    void NoteMainWindow::onEmptyList()
    {
        bottomBar->setActive(gui::BottomBar::Side::LEFT, false);
        bottomBar->setActive(gui::BottomBar::Side::CENTER, false);
        emptyListIcon->setVisible(true);
        rightArrowImage->setVisible(false);
        searchImage->setVisible(false);
    }

    void NoteMainWindow::onListFilled()
    {
        bottomBar->setActive(gui::BottomBar::Side::LEFT, true);
        bottomBar->setActive(gui::BottomBar::Side::CENTER, true);
        emptyListIcon->setVisible(false);
        rightArrowImage->setVisible(true);
        searchImage->setVisible(true);
    }

    bool NoteMainWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            if (inputEvent.is(gui::KeyCode::KEY_LEFT)) {
                application->switchWindow(gui::name::window::note_edit,
                                          std::make_unique<NoteSwitchData>(NotesRecord{}));
            }
            else if (inputEvent.is(gui::KeyCode::KEY_RIGHT)) {
                application->switchWindow(gui::name::window::notes_search);
            }
        }
        return AppWindow::onInput(inputEvent);
    }

    bool NoteMainWindow::onDatabaseMessage(sys::Message *msg)
    {
        auto *msgNotification = dynamic_cast<db::NotificationMessage *>(msg);
        if (msgNotification != nullptr) {
            if (msgNotification->interface == db::Interface::Name::Notes) {
                if (msgNotification->dataModified()) {

                    list->rebuildList(::gui::listview::RebuildType::InPlace);

                    return true;
                }
            }
        }
        return false;
    }
} // namespace app::notes