~aleteoryx/muditaos

ref: 29f455d46bbddee9a8f0ea860e84c9b6e5ab38b5 muditaos/module-apps/application-phonebook/windows/PhonebookNewContact.cpp -rw-r--r-- 9.8 KiB
29f455d4 — Przemyslaw Brudny [EGD-7998] Pure image assets cleanup and update 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PhonebookNewContact.hpp"

#include "DialogMetadata.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"

#include <Dialog.hpp>
#include <service-db/DBServiceAPI.hpp>
#include <messages/DialogMetadataMessage.hpp>

namespace gui
{

    PhonebookNewContact::PhonebookNewContact(app::ApplicationCommon *app)
        : AppWindow(app, gui::window::name::new_contact), newContactModel{std::make_shared<NewContactModel>(app)}
    {
        buildInterface();
    }

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

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

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

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

        list = new gui::ListView(this,
                                 phonebookStyle::newContactWindow::newContactsList::x,
                                 phonebookStyle::newContactWindow::newContactsList::y,
                                 phonebookStyle::newContactWindow::newContactsList::w,
                                 phonebookStyle::newContactWindow::newContactsList::h,
                                 newContactModel,
                                 listview::ScrollBarType::PreRendered);
        setFocusItem(list);
    }

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

    void PhonebookNewContact::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (mode != ShowMode::GUI_SHOW_RETURN) {
            newContactModel->clearData();
            newContactModel->loadData(contact);
        }

        if (mode == ShowMode::GUI_SHOW_INIT) {
            list->rebuildList();
        }

        switch (contactAction) {
        case ContactAction::None:
            break;
        case ContactAction::Add:
        case ContactAction::EditTemporary:
            setTitle(utils::translate("app_phonebook_contact_title"));
            break;
        case ContactAction::Edit:
            setTitle(utils::translate("app_phonebook_options_edit"));
            break;
        }

        !newContactModel->emptyData() ? setSaveButtonVisible(true) : setSaveButtonVisible(false);
    }

    auto PhonebookNewContact::handleSwitchData(SwitchData *data) -> bool
    {
        if (data == nullptr) {
            return false;
        }

        auto *item = dynamic_cast<PhonebookItemData *>(data);
        if (item == nullptr) {
            return false;
        }

        contact = item->getContact();
        if (contact == nullptr) {
            contactAction = ContactAction::Add;
            contact       = std::make_shared<ContactRecord>();
            return true;
        }

        if (contact->ID == DB_ID_NONE) {
            contactAction = ContactAction::Add;
        }
        else if (contact->isTemporary()) {
            contactAction = ContactAction::EditTemporary;
        }
        else {
            contactAction = ContactAction::Edit;
        }

        return true;
    }

    void PhonebookNewContact::setSaveButtonVisible(bool visible)
    {
        navBar->setActive(nav_bar::Side::Center, visible);
    }

    auto PhonebookNewContact::onInput(const InputEvent &inputEvent) -> bool
    {
        auto ret = AppWindow::onInput(inputEvent);

        !newContactModel->emptyData() ? setSaveButtonVisible(true) : setSaveButtonVisible(false);

        if (inputEvent.isShortRelease(gui::KeyCode::KEY_ENTER) && !newContactModel->emptyData() &&
            newContactModel->verifyData()) {
            auto tmpId  = contact->ID;
            contact     = std::make_shared<ContactRecord>();
            contact->ID = tmpId;

            newContactModel->saveData(contact);
            verifyAndSave();

            return true;
        }

        return ret;
    }

    auto PhonebookNewContact::verifyAndSave() -> bool
    {
        LOG_DEBUG("%s", __FUNCTION__);
        if (!contact->isTemporary()) {
            auto result = DBServiceAPI::verifyContact(application, *contact);
            switch (result) {
            case DBServiceAPI::ContactVerificationResult::success:
                break;
            case DBServiceAPI::ContactVerificationResult::emptyContact:
                return false;
            case DBServiceAPI::ContactVerificationResult::primaryNumberDuplicate:
                showDialogDuplicatedNumber(contact->numbers[0].number);
                return false;
            case DBServiceAPI::ContactVerificationResult::secondaryNumberDuplicate:
                showDialogDuplicatedNumber(contact->numbers[1].number);
                return false;
            case DBServiceAPI::ContactVerificationResult::speedDialDuplicate:
                showDialogDuplicatedSpeedDialNumber();
                return false;
            case DBServiceAPI::ContactVerificationResult::temporaryContactExists:
                LOG_DEBUG("Temporary contact exists. Let's update the contact and unbind the phone numbers from the "
                          "temporary one");
                break;
            }
        }
        else {
            contact->removeFromGroup(ContactsDB::temporaryGroupId());
        }

        // perform actual add/update operation
        if (contactAction == ContactAction::Add) {
            auto returnedContact = DBServiceAPI::ContactAdd(application, *contact);
            if (!returnedContact.has_value()) {
                LOG_ERROR("verifyAndSave failed to ADD contact");
                return false;
            }
            *contact = returnedContact.value();
        }
        else if (contactAction == ContactAction::Edit || contactAction == ContactAction::EditTemporary) {
            contact->groups.erase(ContactsDB::temporaryGroupId());
            if (!DBServiceAPI::ContactUpdate(application, *contact)) {
                LOG_ERROR("verifyAndSave failed to UPDATE contact");
                return false;
            }
        }

        std::unique_ptr<gui::SwitchData> data = std::make_unique<PhonebookItemData>(contact);
        data->ignoreCurrentWindowOnStack      = true;
        application->switchWindow(gui::window::name::contact, std::move(data));
        return true;
    } // namespace gui

    void PhonebookNewContact::showDialogDuplicatedNumber(const utils::PhoneNumber::View &duplicatedNumber)
    {
        auto matchedContact   = DBServiceAPI::MatchContactByPhoneNumber(application, duplicatedNumber);
        auto oldContactRecord = (matchedContact != nullptr) ? *matchedContact : ContactRecord{};
        auto metaData         = std::make_unique<gui::DialogMetadataMessage>(
            gui::DialogMetadata{duplicatedNumber.getFormatted(),
                                "info_128px_W_G",
                                text::RichTextParser()
                                    .parse(utils::translate("app_phonebook_duplicate_numbers"),
                                           nullptr,
                                           gui::text::RichTextParser::TokenMap(
                                               {{"$CONTACT_FORMATTED_NAME$", oldContactRecord.getFormattedName()}}))
                                    ->getText(),
                                "",
                                [=]() -> bool {
                                    if (contactAction == ContactAction::Add) {
                                        contact->ID = oldContactRecord.ID;
                                    }
                                    if (!DBServiceAPI::ContactUpdate(application, *contact)) {
                                        LOG_ERROR("Contact id=%" PRIu32 " update failed", contact->ID);
                                        return false;
                                    }
                                    application->switchWindow(gui::name::window::main_window);
                                    return true;
                                }});
        application->switchWindow(gui::window::name::dialog_yes_no, std::move(metaData));
    }

    void PhonebookNewContact::showDialogDuplicatedSpeedDialNumber()
    {
        auto contactRecordsPtr = DBServiceAPI::ContactGetBySpeeddial(application, contact->speeddial);
        auto oldContactRecord  = !contactRecordsPtr->empty() ? contactRecordsPtr->front() : ContactRecord{};

        if (contactAction == ContactAction::Add) {
            contact->ID = oldContactRecord.ID;
        }

        auto metaData = std::make_unique<gui::DialogMetadataMessage>(gui::DialogMetadata{
            text::RichTextParser()
                .parse(utils::translate("app_phonebook_duplicate_speed_dial_title"),
                       nullptr,
                       gui::text::RichTextParser::TokenMap(
                           {{"$CONTACT_SPEED_DIAL$", oldContactRecord.speeddial},
                            {"$CONTACT_FORMATTED_NAME$", oldContactRecord.getFormattedName()}}))
                ->getText(),
            "phonebook_empty_grey_circle_speed_dial",
            text::RichTextParser()
                .parse(utils::translate("app_phonebook_duplicate_speed_dial"),
                       nullptr,
                       gui::text::RichTextParser::TokenMap({{"$CONTACT_SPEED_DIAL$", oldContactRecord.speeddial}}))
                ->getText(),
            contact->speeddial,
            [=]() -> bool {
                if (!DBServiceAPI::ContactUpdate(application, *contact)) {
                    LOG_ERROR("Contact id=%" PRIu32 " update failed", contact->ID);
                    return false;
                }
                application->switchWindow(gui::name::window::main_window);
                return true;
            }});
        application->switchWindow(gui::window::name::dialog_yes_no_icon_txt, std::move(metaData));
    }

} // namespace gui