~aleteoryx/muditaos

ref: 255d03a5da8506d06667d26c79f44ec6ca774961 muditaos/module-apps/application-settings/windows/network/NewApnWindow.cpp -rw-r--r-- 3.2 KiB
255d03a5 — Lefucjusz [MOS-677] Fix crash on importing unprocessable contact 3 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "NewApnWindow.hpp"

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

namespace gui
{

    NewApnWindow::NewApnWindow(app::ApplicationCommon *app)
        : AppWindow(app, gui::window::name::new_apn),
          apn(std::make_shared<packet_data::APN::Config>()), newApnModel{std::make_shared<NewApnModel>(app)}
    {
        buildInterface();
    }

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

    void NewApnWindow::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_settings_new_edit_apn"));

        list = new gui::ListView(this,
                                 style::settings::window::newApn::x,
                                 style::settings::window::newApn::y,
                                 style::settings::window::newApn::w,
                                 style::settings::window::newApn::h,
                                 newApnModel);
        setFocusItem(list);
        apnSettingsModel = new ApnSettingsModel(application);
    }

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

    void NewApnWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (mode != ShowMode::GUI_SHOW_RETURN) {
            newApnModel->clearData();
        }

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

        if (apn != nullptr) {
            newApnModel->loadData(apn);
        }
    }

    auto NewApnWindow::handleSwitchData(SwitchData *data) -> bool
    {
        setSaveButtonVisible(false);

        if (data == nullptr) {
            return false;
        }

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

        auto apnItem = item->getApn();
        if (apnItem != nullptr) {
            apn = apnItem;
        }

        if (!apn->apn.empty()) {
            setSaveButtonVisible(true);
        }

        return true;
    }

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

    auto NewApnWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (!inputEvent.isShortRelease()) {
            return false;
        }
        if (inputEvent.is(gui::KeyCode::KEY_ENTER)) {
            newApnModel->saveData(apn);
            LOG_DEBUG("APN: \"%s\" ", apn->apn.c_str());
            if (apn != nullptr && !apn->apn.empty()) {
                apnSettingsModel->saveAPN(apn);
                LOG_INFO("APN record saved: \"%s\" ", apn->apn.c_str());
                application->returnToPreviousWindow();
            }
            else {
                LOG_WARN("APN not saved, name is empty!");
            }
            return true;
        }

        return AppWindow::onInput(inputEvent);
    }
} // namespace gui