~aleteoryx/muditaos

ref: 5398907b6f137be5c1a35a622c7d51bc8a3595c0 muditaos/module-apps/application-phonebook/models/NewContactModel.cpp -rw-r--r-- 7.0 KiB
5398907b — Przemyslaw Brudny [EGD-7158] Added text paste into Phonebook 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NewContactModel.hpp"

#include "application-phonebook/widgets/ContactListItem.hpp"
#include "application-phonebook/widgets/InputBoxWithLabelAndIconWidget.hpp"
#include "application-phonebook/widgets/InputLinesWithLabelWidget.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"

#include <messages/DialogMetadataMessage.hpp>

NewContactModel::NewContactModel(app::ApplicationCommon *app) : application(app)
{}

auto NewContactModel::requestRecordsCount() -> unsigned int
{
    return internalData.size();
}

auto NewContactModel::getMinimalItemSpaceRequired() const -> unsigned int
{
    return phonebookStyle::inputLinesWithLabelWidget::h;
}

void NewContactModel::requestRecords(const uint32_t offset, const uint32_t limit)
{
    setupModel(offset, limit);
    list->onProviderDataUpdate();
}

auto NewContactModel::getItem(gui::Order order) -> gui::ListItem *
{
    return getRecord(order);
}

void NewContactModel::createData()
{
    auto app = application;

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::FirstName,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); }));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::SecondName,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); }));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::Number,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); }));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::SecondNumber,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); }));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::Email,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); }));

    internalData.push_back(new gui::InputBoxWithLabelAndIconWidget(
        phonebookInternals::ListItemName::AddToFavourites,
        [app](const UTF8 &text) { app->getCurrentWindow()->navBarTemporaryMode(text, false); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); }));

    internalData.back()->setMargins(gui::Margins(style::widgets::leftMargin, style::margins::very_big, 0, 0));

    internalData.push_back(new gui::InputBoxWithLabelAndIconWidget(
        phonebookInternals::ListItemName::AddToICE,
        [app](const UTF8 &text) { app->getCurrentWindow()->navBarTemporaryMode(text, false); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); }));

    internalData.back()->setMargins(
        gui::Margins(style::widgets::leftMargin, style::margins::big, 0, style::margins::very_small));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::Address,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); },
        2));

    internalData.push_back(new gui::InputLinesWithLabelWidget(
        phonebookInternals::ListItemName::Note,
        [app](const UTF8 &text, bool emptyOthers) { app->getCurrentWindow()->navBarTemporaryMode(text, emptyOthers); },
        [app]() { app->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
        [app]() { app->getCurrentWindow()->selectSpecialCharacter(); },
        [&](gui::Text *text) { openTextOptions(text); },
        2));

    for (auto item : internalData) {
        item->deleteByList = false;
    }
}

void NewContactModel::clearData()
{
    list->reset();

    eraseInternalData();

    createData();

    list->rebuildList();
}

bool NewContactModel::verifyData()
{
    for (auto item : internalData) {
        if (item->onVerifyCallback) {
            std::string errorMessage;
            if (!item->onVerifyCallback(errorMessage)) {
                auto metaData = std::make_unique<gui::DialogMetadataMessage>(
                    gui::DialogMetadata{errorMessage,
                                        "error_W_G",
                                        utils::translate("app_phonebook_new_contact_invalid_number"),
                                        "",
                                        [=]() -> bool {
                                            application->returnToPreviousWindow();
                                            return true;
                                        }});

                application->switchWindow(gui::window::name::dialog, std::move(metaData));

                return false;
            }
        }
    }
    return true;
}

void NewContactModel::saveData(std::shared_ptr<ContactRecord> contactRecord)
{
    for (auto item : internalData) {
        if (item->onSaveCallback) {
            item->onSaveCallback(contactRecord);
        }
    }
}

void NewContactModel::loadData(std::shared_ptr<ContactRecord> contactRecord)
{
    for (auto item : internalData) {
        if (item->onLoadCallback) {
            item->onLoadCallback(contactRecord);
        }
    }
}

bool NewContactModel::emptyData()
{
    for (auto item : internalData) {
        if (item->onEmptyCallback) {
            if (!item->onEmptyCallback()) {
                return false;
            }
        }
    }
    return true;
}

void NewContactModel::openTextOptions(gui::Text *text)
{
    std::unique_ptr<gui::SwitchData> data = std::make_unique<PhonebookInputOptionData>(text);
    application->switchWindow(gui::window::name::input_options, std::move(data));
}