~aleteoryx/muditaos

ref: 7e520b49a20285ca67a84e164bd3067995a2ea11 muditaos/module-apps/application-settings/windows/BtWindow.cpp -rw-r--r-- 3.9 KiB
7e520b49 — Piotr Tanski [EGD-3788] Fixes for message drafts feature after requirements elaboration. (#733) 5 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
#include <functional>
#include <memory>

#include "service-appmgr/ApplicationManager.hpp"

#include "../ApplicationSettings.hpp"

#include "i18/i18.hpp"

#include "BtWindow.hpp"
#include "Label.hpp"
#include "Margins.hpp"

#include <Style.hpp>

#include <service-bluetooth/messages/BluetoothMessage.hpp>

namespace gui
{

    BtWindow::BtWindow(app::Application *app) : AppWindow(app, "Bluetooth")
    {
        buildInterface();
    }

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

    void add_box_label(VBox *layout, UTF8 name, std::function<bool(Item &)> foo)
    {
        auto el = new gui::Label(nullptr, 0, 0, layout->getWidth(), style::window::label::default_h);
        style::window::decorateOption(el);
        el->setText(name);
        el->activatedCallback = foo;
        layout->addWidget(el);
    }

    sys::ReturnCodes message_bt(app::Application *app, BluetoothMessage::Request req)
    {
        std::shared_ptr<BluetoothMessage> msg = std::make_shared<BluetoothMessage>(req);
        auto ret                              = sys::Bus::SendUnicast(msg, "ServiceBluetooth", app, 5000);
        if (ret.first != sys::ReturnCodes::Success) {
            LOG_ERROR("err: %d", static_cast<int>(ret.first));
        }
        return ret.first;
    }

    void BtWindow::buildInterface()
    {
        AppWindow::buildInterface();
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::CENTER, utils::localize.get(style::strings::common::select));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::localize.get(style::strings::common::back));
        topBar->setActive(TopBar::Elements::SIGNAL, true);
        topBar->setActive(TopBar::Elements::BATTERY, true);

        setTitle(utils::localize.get("app_settings_bt"));

        LOG_INFO("Create box layout");
        box = new gui::VBox(this, 0, title->offset_h(), style::window_width, 5 * style::window::label::default_h);
        box->setPenWidth(style::window::default_border_no_focus_w);

        // TODO WIP: it's just for usability now
        // TODO scan should return async - handle that... (if in scan -> add to list and refresh if new on window)
        add_box_label(box, "Bluetooth on off", [=](Item &) {
            LOG_DEBUG("Callback Bluetooth on");
            message_bt(application, BluetoothMessage::Request::Start);
            for (auto &el : box->children) {
                el->visible = true;
            }
            box->setVisible(true);
            application->render(gui::RefreshModes::GUI_REFRESH_FAST);
            return true;
        });

        add_box_label(box, "  -> All devices", [=](Item &) {
            LOG_DEBUG("Callback all devices");
            message_bt(application, BluetoothMessage::Request::Scan);
            return true;
        });

        add_box_label(box, "  -> PAN", [=](Item &) {
            LOG_DEBUG("Callback start PAN");
            message_bt(application, BluetoothMessage::Request::PAN);
            return true;
        });

        add_box_label(box, "  -> Visibility", [=](Item &) {
            LOG_DEBUG("Callback visibility");
            message_bt(application, BluetoothMessage::Request::Visible);
            return true;
        });

        // hide all elements except button for `bluetooth on off` - this would cause infinite loop
        std::for_each(std::next(box->children.begin()), box->children.end(), [](auto &el) { el->visible = false; });
        setFocusItem(box);
    }

    bool BtWindow::onInput(const InputEvent &inputEvent)
    {
        return AppWindow::onInput(inputEvent);
    }

    void BtWindow::destroyInterface()
    {
        erase();
        invalidate();
    }

    void BtWindow::invalidate() noexcept
    {
        box = nullptr;
    }

    void BtWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {}

} // namespace gui