~aleteoryx/muditaos

ref: d45c23d8c2c7c31903e2d1e74157c03bafe580ae muditaos/module-apps/application-settings/windows/network/ApnSettingsWindow.cpp -rw-r--r-- 4.7 KiB
d45c23d8 — Maciej Janicki [BH-1037] Fix popup not working 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApnSettingsWindow.hpp"

#include <application-settings/data/ApnListData.hpp>
#include <application-settings/data/SettingsItemData.hpp>
#include <application-settings/windows/WindowNames.hpp>

#include <OptionSetting.hpp>
#include <header/AddElementAction.hpp>

namespace gui
{

    ApnSettingsWindow::ApnSettingsWindow(app::ApplicationCommon *app)
        : OptionWindow(app, gui::window::name::apn_settings)
    {
        buildInterface();
    }

    void ApnSettingsWindow::buildInterface()
    {
        setTitle(utils::translate("app_settings_network_apn_settings"));
        header->navigationIndicatorAdd(new gui::header::AddElementAction(), gui::header::BoxSelection::Left);

        emptyListIcon = new Icon(this,
                                 0,
                                 style::window::default_vertical_pos,
                                 style::window_width,
                                 style::window_height - style::window::default_vertical_pos - style::footer::height,
                                 "empty_list_add_W_G",
                                 utils::translate("app_settings_apn_settings_no_apns"));
        emptyListIcon->setVisible(false);

        navBar->setText(nav_bar::Side::Left, utils::translate(style::strings::common::options));

        activeApn        = std::make_shared<packet_data::APN::Config>();
        apnSettingsModel = std::make_shared<ApnSettingsModel>(application);
        apnSettingsModel->requestAPNList();
    }
    auto ApnSettingsWindow::handleSwitchData(SwitchData *data) -> bool
    {
        if (data == nullptr) {
            LOG_ERROR("Received nullptr");
            return false;
        }

        const auto newData = dynamic_cast<ApnListData *>(data);
        if (newData == nullptr) {
            LOG_ERROR("Received nullptr");
            return false;
        }

        apns = newData->getAPNs();
        if (apns.empty()) {
            emptyListIcon->setVisible(true);
            return false;
        }

        return true;
    }

    void ApnSettingsWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        clearOptions();
        navBar->setActive(gui::nav_bar::Side::Left, false);
        navBar->setActive(gui::nav_bar::Side::Center, false);
        emptyListIcon->setVisible(false);

        if (apns.empty()) {
            return;
        }

        addOptions(optionsList(apns));

        if (mode == gui::ShowMode::GUI_SHOW_RETURN) {
            apnSettingsModel->requestAPNList();
        }

        navBar->setActive(gui::nav_bar::Side::Left, true);
        navBar->setActive(gui::nav_bar::Side::Center, true);
    }

    auto ApnSettingsWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }
        if (!inputEvent.isShortRelease()) {
            return false;
        }
        if (inputEvent.is(gui::KeyCode::KEY_LEFT)) {
            auto apnRecord                        = std::make_shared<packet_data::APN::Config>();
            std::unique_ptr<gui::SwitchData> data = std::make_unique<ApnItemData>(apnRecord);
            application->switchWindow(gui::window::name::new_apn, gui::ShowMode::GUI_SHOW_INIT, std::move(data));
            return true;
        }
        if (inputEvent.is(gui::KeyCode::KEY_LF)) {
            std::unique_ptr<gui::SwitchData> data = std::make_unique<ApnItemData>(activeApn);
            application->switchWindow(gui::window::name::apn_options, gui::ShowMode::GUI_SHOW_INIT, std::move(data));
            return true;
        }

        return false;
    }

    auto ApnSettingsWindow::optionsList(std::vector<std::shared_ptr<packet_data::APN::Config>> apnsList)
        -> std::list<Option>
    {
        std::list<gui::Option> optionsList;

        for (const auto &apn : apnsList) {
            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                (apn->apnType == packet_data::APN::APNType::Default) ? "<b>" + apn->apn + "</b>" : apn->apn,
                [=](gui::Item &item) {
                    LOG_DEBUG("APN: %s", apn->apn.c_str());
                    std::unique_ptr<gui::SwitchData> apnData = std::make_unique<ApnItemData>(apn);
                    application->switchWindow(
                        gui::window::name::new_apn, gui::ShowMode::GUI_SHOW_INIT, std::move(apnData));
                    return true;
                },
                [=](gui::Item &item) {
                    activeApn = apn;
                    return true;
                },
                nullptr));
        }

        return optionsList;
    }

} // namespace gui