~aleteoryx/muditaos

ref: 3578ea98a42d909d04fdb280834a4c341c535053 muditaos/module-apps/application-phonebook/windows/PhonebookContactOptions.cpp -rw-r--r-- 7.1 KiB
3578ea98 — Radoslaw Wicik [EGD-5249] Add Clang Tidy packages in docker 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "PhonebookContactOptions.hpp"
#include "DialogMetadata.hpp"
#include "DialogMetadataMessage.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"
#include "application-phonebook/data/PhonebookItemData.hpp"
#include "Dialog.hpp"

#include <memory>
#include <service-db/DBServiceAPI.hpp>

namespace gui
{

    PhonebookContactOptions::PhonebookContactOptions(app::Application *app)
        : OptionWindow(app, gui::window::name::contact_options)
    {
        buildInterface();
    }

    auto PhonebookContactOptions::handleSwitchData(SwitchData *data) -> bool
    {
        auto *item = dynamic_cast<PhonebookItemData *>(data);
        if (item == nullptr) {
            LOG_WARN("Received null pointer");
            return false;
        }
        contact = item->getContact();
        clearOptions();
        addOptions(contactOptionsList());

        return true;
    }

    auto PhonebookContactOptions::contactOptionsList() -> std::list<gui::Option>
    {
        std::list<gui::Option> options;
        options.emplace_back(gui::Option{utils::localize.get("app_phonebook_options_edit"), [=](gui::Item &item) {
                                             LOG_INFO("Editing contact!");
                                             std::unique_ptr<gui::SwitchData> data =
                                                 std::make_unique<PhonebookItemData>(contact);
                                             this->application->switchWindow(gui::window::name::new_contact,
                                                                             gui::ShowMode::GUI_SHOW_INIT,
                                                                             std::move(data));
                                             return true;
                                         }});
        options.emplace_back(gui::Option{utils::localize.get("app_phonebook_options_forward_namecard"),
                                         [=](gui::Item &item) {
                                             LOG_INFO("Forwarding namecard!");
                                             std::unique_ptr<gui::SwitchData> data =
                                                 std::make_unique<PhonebookItemData>(contact);
                                             this->application->switchWindow(gui::window::name::namecard_options,
                                                                             gui::ShowMode::GUI_SHOW_INIT,
                                                                             std::move(data));
                                             return true;
                                         },
                                         gui::Arrow::Enabled});
        if (contact->isOnBlocked()) {
            options.emplace_back(
                gui::Option{utils::localize.get("app_phonebook_options_unblock"), [=](gui::Item &item) {
                                LOG_INFO("Unblocking contact!");
                                return contactBlock(false);
                            }});
        }
        else {
            options.emplace_back(gui::Option{utils::localize.get("app_phonebook_options_block"), [=](gui::Item &item) {
                                                 LOG_INFO("Blocking contact!");
                                                 return contactBlock(true);
                                             }});
        }
        options.emplace_back(gui::Option{utils::localize.get("app_phonebook_options_delete"), [=](gui::Item &item) {
                                             LOG_INFO("Deleting contact!");
                                             return contactRemove();
                                         }});
        return options;
    }

    auto PhonebookContactOptions::contactBlock(bool shouldBeBlocked) -> bool
    {
        LOG_DEBUG("Blocking contact: %" PRIu32, contact->ID);
        DialogMetadata meta;
        meta.action = [=]() -> bool {
            contact->addToBlocked(shouldBeBlocked);
            DBServiceAPI::ContactUpdate(this->application, *contact);
            if (shouldBeBlocked) {
                showNotification(NotificationType::Block);
            }
            else {
                showNotification(NotificationType::Unblock);
            }

            return true;
        };
        if (shouldBeBlocked) {
            meta.text = utils::localize.get("app_phonebook_options_block_confirm");
        }
        else {
            meta.text = utils::localize.get("app_phonebook_options_unblock_confirm");
        }

        auto contactRec = DBServiceAPI::ContactGetByID(this->application, contact->ID);
        auto cont       = !contactRec->empty() ? contactRec->front() : ContactRecord{};
        meta.title      = cont.getFormattedName();
        meta.icon       = "phonebook_contact_delete_trashcan";
        application->switchWindow(gui::window::name::dialog_yes_no, std::make_unique<gui::DialogMetadataMessage>(meta));
        return true;
    }

    auto PhonebookContactOptions::contactRemove() -> bool
    {
        LOG_DEBUG("Removing contact: %" PRIu32, contact->ID);
        DialogMetadata meta;
        meta.action = [=]() -> bool {
            if (!DBServiceAPI::ContactRemove(this->application, contact->ID)) {
                LOG_ERROR("Contact id=%" PRIu32 "  remove failed", contact->ID);
                return false;
            }
            showNotification(NotificationType::Delete);
            return true;
        };
        meta.text       = utils::localize.get("app_phonebook_options_delete_confirm");
        auto contactRec = DBServiceAPI::ContactGetByID(this->application, contact->ID);
        auto cont       = !contactRec->empty() ? contactRec->front() : ContactRecord{};
        meta.title      = cont.getFormattedName();
        meta.icon       = "phonebook_contact_delete_trashcan";
        application->switchWindow(gui::window::name::dialog_yes_no, std::make_unique<DialogMetadataMessage>(meta));
        return true;
    }

    auto PhonebookContactOptions::showNotification(NotificationType notificationType) -> bool
    {
        DialogMetadata meta;
        meta.icon = "info_big_circle_W_G";
        switch (notificationType) {
        case NotificationType::Block:
            meta.text = utils::localize.get("app_phonebook_options_block_notification");
            break;
        case NotificationType::Delete:
            meta.text = utils::localize.get("app_phonebook_options_delete_notification");
            break;
        case NotificationType::Unblock:
            meta.text = utils::localize.get("app_phonebook_options_unblock_notification");
            break;
        }
        meta.action = [=]() -> bool {
            this->application->switchWindow(gui::name::window::main_window);
            return true;
        };
        meta.title = contact->getFormattedName(ContactRecord::NameFormatType::Title);
        application->switchWindow(gui::window::name::dialog_confirm,
                                  std::make_unique<gui::DialogMetadataMessage>(meta));
        return true;
    }
} // namespace gui