~aleteoryx/muditaos

ref: c8ad07c3c00c5c41e81af4b54a5965f9b3551559 muditaos/module-apps/application-phonebook/windows/PhonebookMainWindow.cpp -rw-r--r-- 8.0 KiB
c8ad07c3 — Marcin Zieliński [MOS-578] Fix misleading SMS notification text 2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PhonebookMainWindow.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"
#include "application-phonebook/data/PhonebookItemData.hpp"
#include "application-phonebook/data/PhonebookStyle.hpp"

#include <queries/phonebook/QueryContactGet.hpp>

#include <header/AddElementAction.hpp>
#include <header/SearchAction.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-db/DBNotificationMessage.hpp>

namespace gui
{
    PhonebookMainWindow::PhonebookMainWindow(app::ApplicationCommon *app,
                                             std::shared_ptr<SearchRequestModel> model,
                                             std::shared_ptr<PhonebookModel> phonebookModel)
        : AppWindow(app, gui::name::window::main_window),
          model(std::move(model)), phonebookModel{std::move(phonebookModel)}
    {
        buildInterface();
    }

    void PhonebookMainWindow::rebuild()
    {
        contactsList->rebuildList(gui::listview::RebuildType::InPlace);
    }

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

        setTitle(utils::translate("app_phonebook_title_main"));
        header->navigationIndicatorAdd(new gui::header::AddElementAction(), gui::header::BoxSelection::Left);
        header->navigationIndicatorAdd(new gui::header::SearchAction(), gui::header::BoxSelection::Right);

        navBar->setText(nav_bar::Side::Left, utils::translate(style::strings::common::call));
        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::open));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

        contactsList = new gui::PhonebookListView(this,
                                                  phonebookStyle::mainWindow::contactsList::x,
                                                  phonebookStyle::mainWindow::contactsList::y,
                                                  phonebookStyle::mainWindow::contactsList::w,
                                                  phonebookStyle::mainWindow::contactsList::h,
                                                  phonebookModel);
        contactsList->setBoundaries(Boundaries::Continuous);
        setFocusItem(contactsList);

        emptyListIcon =
            new gui::Icon(this,
                          0,
                          ::style::window::default_vertical_pos,
                          ::style::window_width,
                          ::style::window_height - ::style::window::default_vertical_pos - ::style::nav_bar::height,
                          "empty_list_add_W_G",
                          utils::translate("app_phonebook_no_contacts"));
        emptyListIcon->setVisible(false);

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

        phonebookModel->setDisplayMode(static_cast<std::uint32_t>(ContactDisplayMode::SortedByLetter));
        contactsList->rebuildList(gui::listview::RebuildType::Full);

        const auto app = application;
        inputMode      = std::make_unique<InputMode>(
            std::list<InputMode::Mode>{InputMode::ABC, InputMode::abc},
            [app](const UTF8 &text) { app->getCurrentWindow()->navBarTemporaryMode(text); },
            [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
            [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
            [app](std::function<void()> restoreFunction) {
                app->getCurrentWindow()->startInputModeRestoreTimer(std::move(restoreFunction));
            });
    }

    void PhonebookMainWindow::destroyInterface()
    {
        erase();
    }

    PhonebookMainWindow::~PhonebookMainWindow()
    {
        destroyInterface();
    }

    void PhonebookMainWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        auto contactRequest = dynamic_cast<PhonebookSearchRequest *>(data);
        model->setRequested(contactRequest != nullptr);
        if (model->requestedSearch()) {
            enableNewContact = false;

            navBar->setActive(nav_bar::Side::Left, false);
            navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::add));
            navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

            header->navigationIndicatorRemove(gui::header::BoxSelection::Left);
        }
    }

    void PhonebookMainWindow::HandleFilteringByLetter(const InputEvent &inputEvent)
    {
        auto code = translator.handle(inputEvent.getRawKey(), inputMode ? inputMode->get() : "");
        if (code != Profile::none_key) {
            LOG_INFO("char=' %c'", static_cast<char>(code));
            char letter = static_cast<char>(code);
            std::string filterLetter;
            filterLetter.push_back(letter);

            LOG_DEBUG("Number of favourites contacts : %" PRIu32, phonebookModel->letterMap.favouritesCount);
            uint32_t dataOffset = phonebookModel->letterMap.firstLetterDictionary[filterLetter];
            if (dataOffset != phonebookContactsMap::NO_MATCH_FOUND) {
                LOG_DEBUG("PhoneBook Data Offset : %" PRIu32, dataOffset);
                phonebookModel->setDisplayMode(static_cast<uint32_t>(ContactDisplayMode::SortedByLetter));
                contactsList->rebuildList(gui::listview::RebuildType::OnOffset, dataOffset);
            }
        }
    }

    bool PhonebookMainWindow::onInput(const InputEvent &inputEvent)
    {
        if (inputEvent.isShortRelease()) {
            if (inputEvent.is(gui::KeyCode::KEY_LEFT)) {
                if (enableNewContact) {
                    std::unique_ptr<gui::SwitchData> data = std::make_unique<PhonebookItemData>();
                    application->switchWindow(
                        gui::window::name::new_contact, gui::ShowMode::GUI_SHOW_INIT, std::move(data));
                    return true;
                }
            }
            else if (inputEvent.is(gui::KeyCode::KEY_RIGHT) &&
                     header->navigationIndicatorVisible(gui::header::BoxSelection::Right)) {
                application->switchWindow(gui::window::name::search);
                return true;
            }
            else {
                HandleFilteringByLetter(inputEvent);
            }
        }
        return AppWindow::onInput(inputEvent);
    }

    bool PhonebookMainWindow::onDatabaseMessage(sys::Message *msgl)
    {
        auto *msgNotification = dynamic_cast<db::NotificationMessage *>(msgl);
        if (msgNotification != nullptr) {
            if (msgNotification->interface == db::Interface::Name::Contact) {

                if (msgNotification->dataModified()) {

                    phonebookModel->letterMap = phonebookModel->requestLetterMap();
                    rebuild();

                    return true;
                }
            }
        }

        return false;
    }

    void PhonebookMainWindow::onEmptyList()
    {
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        if (model->requestedSearch()) {
            emptyListIcon->text->setRichText(utils::translate("app_phonebook_no_contacts_yet"));
            emptyListIcon->image->setVisible(false);
        }
        emptyListIcon->setVisible(true);
        header->navigationIndicatorRemove(gui::header::BoxSelection::Right);
        application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP);
    }

    void PhonebookMainWindow::onListFilled()
    {
        if (not model->requestedSearch()) {
            navBar->setActive(gui::nav_bar::Side::Left, true);
            navBar->setActive(gui::nav_bar::Side::Center, true);
            header->navigationIndicatorAdd(new gui::header::SearchAction(), gui::header::BoxSelection::Right);
        }

        emptyListIcon->setVisible(false);
        application->refreshWindow(RefreshModes::GUI_REFRESH_DEEP);
    }
} /* namespace gui */