~aleteoryx/muditaos

ref: 53e2fae8fbf21cd87194a0ba39b37dbe36761231 muditaos/module-apps/application-settings/widgets/network/ApnInputWidget.cpp -rw-r--r-- 8.7 KiB
53e2fae8 — Lefucjusz [MOS-178] Add input mode displaying timeout 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
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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApnInputWidget.hpp"

#include <application-settings/widgets/SettingsStyle.hpp>

#include <Label.hpp>
#include <i18n/i18n.hpp>

namespace gui
{
    ApnInputWidget::ApnInputWidget(settingsInternals::ListItemName listItemName,
                                   const std::function<void(const UTF8 &)> &navBarTemporaryMode,
                                   const std::function<void()> &navBarRestoreFromTemporaryMode,
                                   const std::function<void()> &selectSpecialCharacter,
                                   const std::function<void(std::function<void()> restoreFunction)> &restoreInputMode,
                                   const std::function<void(const std::string &text)> &contentChanged,
                                   unsigned int lines)
        : listItemName(listItemName), checkTextContent(contentChanged)
    {
        setMinimumSize(style::settings::widget::apnInputWidget::w,
                       style::settings::widget::apnInputWidget::title_label_h +
                           style::settings::widget::apnInputWidget::span_size +
                           style::settings::widget::apnInputWidget::input_text_h * lines);

        setMargins(gui::Margins(0, style::margins::huge, 0, 0));

        vBox = new VBox(this, 0, 0, 0, 0);
        vBox->setEdges(RectangleEdge::None);

        titleLabel = new Label(vBox);
        titleLabel->setMinimumSize(style::settings::widget::apnInputWidget::w,
                                   style::settings::widget::apnInputWidget::title_label_h);
        titleLabel->setEdges(RectangleEdge::None);
        titleLabel->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
        titleLabel->setFont(style::window::font::verysmall);
        titleLabel->activeItem = false;

        inputText = new TextFixedSize(vBox, 0, 0, 0, 0);
        inputText->setMinimumSize(style::settings::widget::apnInputWidget::w,
                                  style::settings::widget::apnInputWidget::input_text_h * lines);
        inputText->setMargins(Margins(0, style::settings::widget::apnInputWidget::span_size, 0, 0));
        inputText->setUnderlinePadding(style::settings::widget::apnInputWidget::underline_padding);

        inputText->setEdges(RectangleEdge::None);
        inputText->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
        inputText->setFont(style::window::font::medium);
        inputText->setInputMode(new InputMode(
            {InputMode::Abc, InputMode::ABC, InputMode::abc, InputMode::digit},
            [=](const UTF8 &text) { navBarTemporaryMode(text); },
            [=]() { navBarRestoreFromTemporaryMode(); },
            [=]() { selectSpecialCharacter(); },
            [=](std::function<void()> restoreFunction) { restoreInputMode(std::move(restoreFunction)); }));
        inputText->setPenFocusWidth(style::window::default_border_focus_w);
        inputText->setPenWidth(style::window::default_border_no_focus_w);
        inputText->setEditMode(EditMode::Edit);

        applyItemNameSpecificSettings();

        focusChangedCallback = [&](Item &item) {
            setFocusItem(focus ? vBox : nullptr);

            if (focus) {
                inputText->setFont(style::window::font::mediumbold);
            }
            else {
                inputText->setFont(style::window::font::medium);
            }
            return true;
        };

        inputCallback = [&](Item &item, const InputEvent &event) {
            auto result = inputText->onInput(event);
            if (checkTextContent != nullptr) {
                checkTextContent(inputText->getText());
            }
            return result;
        };

        dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            vBox->setArea({0, 0, newDim.w, newDim.h});
            return true;
        };

        setEdges(RectangleEdge::None);
    }

    void ApnInputWidget::applyItemNameSpecificSettings()
    {
        switch (listItemName) {

        case settingsInternals::ListItemName::Name:
            nameHandler();
            break;

        case settingsInternals::ListItemName::APN:
            apnHandler();
            break;

        case settingsInternals::ListItemName::Username:
            usernameHandler();
            break;

        case settingsInternals::ListItemName::Password:
            passwordNumberHandler();
            break;

        case settingsInternals::ListItemName::AuthType:
            authtypeHandler();
            break;

        case settingsInternals::ListItemName::ApnType:
            apntypeHandler();
            break;

        case settingsInternals::ListItemName::ApnProtocol:
            protocolHandler();
            break;

        default:
            LOG_ERROR("Incorrect List Item Name!");
            break;
        }
    }

    void ApnInputWidget::nameHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_name"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->apn = inputText->getText();
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->apn);
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

    void ApnInputWidget::apnHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_APN"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->ip = inputText->getText();
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->ip);
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

    void ApnInputWidget::usernameHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_username"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->username = inputText->getText();
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->username);
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

    void ApnInputWidget::passwordNumberHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_password"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->password = inputText->getText();
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->password);
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

    void ApnInputWidget::authtypeHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_authtype"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->setAuthMethod(inputText->getText());
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->getAuthMethod());
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

    void ApnInputWidget::apntypeHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_apntype"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->setApnType(inputText->getText());
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->getApnType());
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }
    void ApnInputWidget::protocolHandler()
    {
        titleLabel->setText(utils::translate("app_settings_apn_apnprotocol"));
        inputText->setTextType(TextType::SingleLine);
        onSaveCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            apnRecord->setApnProtocol(inputText->getText());
        };
        onLoadCallback = [&](std::shared_ptr<packet_data::APN::Config> apnRecord) {
            inputText->setText(apnRecord->getApnProtocol());
        };
        onEmptyCallback = [&]() { return inputText->isEmpty(); };
    }

} /* namespace gui */