~aleteoryx/muditaos

ref: 258ef2222872469f116b9e63f075f7f1834d6538 muditaos/module-apps/application-phonebook/windows/PhonebookMainWindow.cpp -rw-r--r-- 6.9 KiB
258ef222 — Mateusz Grzywacz [EGD-3482] torch sevm - glue layer 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
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
#include "PhonebookMainWindow.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"
#include "application-phonebook/data/PhonebookItemData.hpp"
#include "application-phonebook/data/PhonebookStyle.hpp"

#include <messages/QueryMessage.hpp>
#include <queries/phonebook/QueryContactGet.hpp>

#include <service-appmgr/ApplicationManager.hpp>

namespace gui
{
    PhonebookMainWindow::PhonebookMainWindow(app::Application *app)
        : AppWindow(app, gui::name::window::main_window), phonebookModel{
                                                              std::make_shared<PhonebookModel>(this->application)}
    {
        buildInterface();
    }

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

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

        topBar->setActive(TopBar::Elements::TIME, true);
        setTitle(utils::localize.get("app_phonebook_title_main"));
        leftArrowImage  = new gui::Image(this,
                                        phonebookStyle::mainWindow::leftArrowImage::x,
                                        phonebookStyle::mainWindow::leftArrowImage::y,
                                        phonebookStyle::mainWindow::leftArrowImage::w,
                                        phonebookStyle::mainWindow::leftArrowImage::h,
                                        "arrow_left");
        rightArrowImage = new gui::Image(this,
                                         phonebookStyle::mainWindow::rightArrowImage::x,
                                         phonebookStyle::mainWindow::rightArrowImage::y,
                                         phonebookStyle::mainWindow::rightArrowImage::w,
                                         phonebookStyle::mainWindow::rightArrowImage::h,
                                         "arrow_right");
        newContactImage = new gui::Image(this,
                                         phonebookStyle::mainWindow::newContactImage::x,
                                         phonebookStyle::mainWindow::newContactImage::y,
                                         phonebookStyle::mainWindow::newContactImage::w,
                                         phonebookStyle::mainWindow::newContactImage::h,
                                         "cross");
        searchImage     = new gui::Image(this,
                                     phonebookStyle::mainWindow::searchImage::x,
                                     phonebookStyle::mainWindow::searchImage::y,
                                     phonebookStyle::mainWindow::searchImage::w,
                                     phonebookStyle::mainWindow::searchImage::h,
                                     "search");

        contactsList = new gui::PhonebookListView(this,
                                                  phonebookStyle::mainWindow::contactsList::x,
                                                  phonebookStyle::mainWindow::contactsList::y,
                                                  phonebookStyle::mainWindow::contactsList::w,
                                                  phonebookStyle::mainWindow::contactsList::h,
                                                  phonebookModel);
        contactsList->setPenFocusWidth(phonebookStyle::mainWindow::contactsList::penFocusWidth);
        contactsList->setPenWidth(phonebookStyle::mainWindow::contactsList::penWidth);

        bottomBar->setActive(BottomBar::Side::LEFT, true);
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::LEFT, utils::localize.get(style::strings::common::call));
        bottomBar->setText(BottomBar::Side::CENTER, utils::localize.get(style::strings::common::open));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back));
    }

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

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

    void PhonebookMainWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        LOG_INFO("onBeforeShow");
        setFocusItem(contactsList);

        contactsList->clear();
        contactsList->setProvider(phonebookModel);

        auto contactRequest = dynamic_cast<PhonebookSearchReuqest *>(data);
        requestedSearch     = contactRequest != nullptr;
        if (requestedSearch) {
            enableNewContact                       = false;
            phonebookModel->messagesSelectCallback = [=](gui::PhonebookItem *item) {
                std::unique_ptr<PhonebookSearchReuqest> data = std::make_unique<PhonebookSearchReuqest>();
                data->result                                 = item->contact;
                data->setDescription("PhonebookSearchRequest");
                return sapm::ApplicationManager::messageSwitchPreviousApplication(
                    application, std::make_unique<sapm::APMSwitchPrevApp>(application->GetName(), std::move(data)));
            };

            leftArrowImage->setVisible(false);
            newContactImage->setVisible(false);
        }
    }

    bool PhonebookMainWindow::onInput(const InputEvent &inputEvent)
    {
        // process only if key is released
        if (inputEvent.state == InputEvent::State::keyReleasedShort) {
            switch (inputEvent.keyCode) {

            case 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;

            case KeyCode::KEY_RIGHT:
                application->switchWindow("Search");
                return true;

            default:
                break;
            }
        }

        // check if any of the lower inheritance onInput methods catch the event
        return AppWindow::onInput(inputEvent);
    }

    bool PhonebookMainWindow::onDatabaseMessage(sys::Message *msgl)
    {
        auto respMsg = dynamic_cast<sys::ResponseMessage *>(msgl);

        assert(respMsg != nullptr);
        assert(respMsg->responseTo == MessageType::DBQuery);

        auto queryResponse = dynamic_cast<db::QueryResponse *>(respMsg);
        if (queryResponse == nullptr) {
            LOG_ERROR("Unexpected message.");
            return false;
        }

        auto contactsResponse = dynamic_cast<db::query::ContactGetResult *>(queryResponse->getResult());
        assert(contactsResponse != nullptr);

        auto records = std::make_unique<std::vector<ContactRecord>>(contactsResponse->getRecords());

        return phonebookModel->updateRecords(std::move(records));
    }

    bool PhonebookMainWindow::isSearchRequested() const
    {
        return requestedSearch;
    }
} /* namespace gui */