~aleteoryx/muditaos

ref: 29f455d46bbddee9a8f0ea860e84c9b6e5ab38b5 muditaos/module-apps/application-settings/windows/bluetooth/AllDevicesWindow.cpp -rw-r--r-- 7.9 KiB
29f455d4 — Przemyslaw Brudny [EGD-7998] Pure image assets cleanup and update 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AllDevicesWindow.hpp"

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

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

namespace gui
{

    AllDevicesWindow::AllDevicesWindow(app::ApplicationCommon *app,
                                       std::shared_ptr<BluetoothSettingsModel> bluetoothSettingsModel)
        : BaseSettingsWindow(app, window::name::all_devices), bluetoothSettingsModel(bluetoothSettingsModel)
    {
        buildInterface();
    }

    void AllDevicesWindow::buildInterface()
    {
        setTitle(utils::translate("app_settings_bluetooth_all_devices"));

        optionsList = new gui::ListView(this,
                                        option::window::optionsListX,
                                        option::window::optionsListY,
                                        option::window::optionsListW,
                                        option::window::optionsListH,
                                        optionsModel,
                                        listview::ScrollBarType::Proportional);

        optionsList->prepareRebuildCallback = [this]() { recreateOptions(); };
        optionsModel->createData(options);
        setFocusItem(optionsList);

        header->navigationIndicatorAdd(new gui::header::AddElementAction(), gui::header::BoxSelection::Left);
    }

    void AllDevicesWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (mode == ShowMode::GUI_SHOW_RETURN) {
            bluetoothSettingsModel->stopScan();
        }
        if (const auto bondedDevicesData = dynamic_cast<BondedDevicesData *>(data); bondedDevicesData != nullptr) {
            bluetoothSettingsModel->replaceDevicesList(bondedDevicesData->getDevices());
        }
        refreshOptionsList();
    }

    auto AllDevicesWindow::onInput(const InputEvent &inputEvent) -> bool
    {
        if (!inputEvent.isShortRelease()) {
            return AppWindow::onInput(inputEvent);
        }
        if (inputEvent.is(KeyCode::KEY_LEFT)) {
            bluetoothSettingsModel->requestScan();
            application->switchWindow(gui::window::name::dialog_settings,
                                      gui::ShowMode::GUI_SHOW_INIT,
                                      std::make_unique<gui::DialogMetadataMessage>(gui::DialogMetadata{
                                          utils::translate("app_settings_bluetooth_add_device"),
                                          "search_128px_W_G",
                                          utils::translate("app_settings_bluetooth_searching_devices")}));
            return true;
        }
        if (inputEvent.is(KeyCode::KEY_LF) && !bluetoothSettingsModel->isDeviceListEmpty()) {
            navBar->setActive(nav_bar::Side::Left, false);
            navBar->setActive(nav_bar::Side::Center, false);
            auto selectedDevice = bluetoothSettingsModel->getSelectedDevice();
            if (selectedDevice.has_value()) {
                bluetoothSettingsModel->requestDeviceUnpair(selectedDevice.value().get());
            }
            refreshOptionsList();
            return true;
        }
        return AppWindow::onInput(inputEvent);
    }

    auto AllDevicesWindow::buildOptionsList() -> std::list<Option>
    {
        navBar->setActive(nav_bar::Side::Center, !bluetoothSettingsModel->getDevices().empty());
        std::list<gui::Option> optionsList;

        if (bluetoothSettingsModel->isDeviceListEmpty()) {
            this->navBar->setActive(nav_bar::Side::Left, false);
            return optionsList;
        }

        for (const auto &device : bluetoothSettingsModel->getDevices()) {
            UTF8 textOnCenter                  = getTextOnCenter(device.deviceState);
            option::SettingRightItem rightItem = getRightItem(device.deviceState);
            UTF8 textOnRight                   = getTextOnRight(device.deviceState);

            optionsList.emplace_back(std::make_unique<gui::option::OptionSettings>(
                device.name.data(),
                [=](gui::Item & /*item*/) { return handleDeviceAction(device); },
                [=](gui::Item &item) {
                    if (item.focus) {
                        this->setNavBarText(textOnCenter, nav_bar::Side::Center);
                        if (device.deviceState != DeviceState::Pairing) {
                            this->setNavBarText(utils::translate("common_forget"), nav_bar::Side::Left);
                            this->navBar->setActive(nav_bar::Side::Left, true);
                        }
                        bluetoothSettingsModel->setSelectedDevice(device);
                    }
                    return true;
                },
                nullptr,
                rightItem,
                false,
                std::move(textOnRight)));
        }
        return optionsList;
    }

    UTF8 AllDevicesWindow::getTextOnCenter(const DeviceState &state) const
    {
        switch (state) {
        case DeviceState::ConnectedAudio:
            [[fallthrough]];
        case DeviceState::ConnectedVoice:
            [[fallthrough]];
        case DeviceState::ConnectedBoth:
            return utils::translate("common_disconnect");
        case DeviceState::Paired:
            if (bluetoothSettingsModel->isDeviceConnecting()) {
                break;
            }
            return utils::translate("common_connect");
        case DeviceState::Pairing:
            [[fallthrough]];
        case DeviceState::Connecting:
            [[fallthrough]];
        case DeviceState::Unknown:
            break;
        }
        return UTF8();
    }

    UTF8 AllDevicesWindow::getTextOnRight(const DeviceState &state) const
    {
        switch (state) {
        case DeviceState::ConnectedBoth:
            return utils::translate("app_settings_option_connected_both");
        case DeviceState::Connecting:
            return utils::translate("app_settings_option_connecting");
        case DeviceState::Pairing:
            return utils::translate("app_settings_option_pairing");
        case DeviceState::ConnectedAudio:
            return utils::translate("app_settings_option_connected_audio");
        case DeviceState::ConnectedVoice:
            return utils::translate("app_settings_option_connected_voice");
        case DeviceState::Paired:
            [[fallthrough]];
        case DeviceState::Unknown:
            break;
        }
        return UTF8();
    }

    option::SettingRightItem AllDevicesWindow::getRightItem(const DeviceState &state) const
    {
        switch (state) {
        case DeviceState::ConnectedBoth:
            [[fallthrough]];
        case DeviceState::ConnectedAudio:
            [[fallthrough]];
        case DeviceState::ConnectedVoice:
            [[fallthrough]];
        case DeviceState::Connecting:
            [[fallthrough]];
        case DeviceState::Pairing:
            return option::SettingRightItem::Text;
        case DeviceState::Paired:
            return option::SettingRightItem::Bt;
        case DeviceState::Unknown:
            break;
        }
        return option::SettingRightItem::Disabled;
    }

    auto AllDevicesWindow::handleDeviceAction(const Devicei &device) -> bool
    {
        if (device.deviceState == DeviceState::ConnectedBoth || device.deviceState == DeviceState::ConnectedAudio ||
            device.deviceState == DeviceState::ConnectedVoice) {
            bluetoothSettingsModel->requestDisconnection();
            refreshOptionsList();
        }
        else if (device.deviceState == DeviceState::Paired && !bluetoothSettingsModel->isDeviceConnecting()) {
            bluetoothSettingsModel->requestConnection(device);
            refreshOptionsList();
        }
        return true;
    }

} // namespace gui