~aleteoryx/muditaos

ref: sign_test muditaos/module-apps/application-test/ApplicationTest.cpp -rw-r--r-- 4.5 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "application-test/ApplicationTest.hpp"
#include "log/log.hpp"
#include "presenters/TestPresenter.hpp"
#include "windows/TestPopup.hpp"
#include "windows/TestWindow.hpp"

namespace app
{
    ApplicationTest::ApplicationTest(const std::string &name,
                                     const std::string &parent,
                                     StatusIndicators statusIndicators,
                                     StartInBackground startInBackground)
        : Application(name, parent, statusIndicators, startInBackground)
    {
        LOG_INFO("Created!");
    }

    ApplicationTest::~ApplicationTest()
    {
        // this destructor is needed for unique_ptr forward declared TestPresenter
    }

    sys::ReturnCodes ApplicationTest::InitHandler()
    {
        LOG_INFO("Initializing!");
        // NOTE
        // this is very important! as it:
        // 1. sets state initializing on app
        // 2. initailizes settigns - every app have settings object by default
        const auto ret = Application::InitHandler();
        if (ret != sys::ReturnCodes::Success) {
            LOG_ERROR("Init app failure! %d", int(ret));
            return ret;
        }
        createUserInterface();
        return sys::ReturnCodes::Success;
    }

    sys::MessagePointer ApplicationTest::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp)
    {
        // NOTE
        // If return value here is `nullptr` - then application never responds
        // otherwise it will respond with ResponseMessage
        auto response = sys::MessagePointer{};

        // NOTE in this place we have all common application uses such as:
        // * passing key presses to windows
        // * updading top bar etc
        response = Application::DataReceivedHandler(msgl);
        // NOTE
        // here we would have handleAsyncResponse if this Applicaition would have any `AsyncTask`
        // if (response && (dynamic_cast<sys::ResponseMessage *>(response.get())->retCode == sys::ReturnCodes::Success))
        // {
        //      return response;
        // }
        // return handleAsyncResponse(resp);
        return response;
    }

    void ApplicationTest::createUserInterface()
    {
        // NOTE
        // create presenter first
        presenter = std::make_unique<gui::TestPresenter>(this);
        // NOTE
        // 1. we pass presenter to this application
        // 2. unfortunately any window requires access to app by design - so we pass it too...
        // 3. please note that window is not yet created - it will be created when and if needed
        // 4. please see that window name is set dynamically - this is proper way to set window name - these names are
        // used on windows stack and matters
        windowsFactory.attach(gui::name::window::main_window,
                              [this](ApplicationCommon * /*app*/, const std::string &name) {
                                  return std::make_unique<gui::TestMainWindow>(this, name, *presenter);
                              });

        // This is just a test popup to show on ServiceTest popup request
        windowsFactory.attach(gui::popup::window::test_popup,
                              [this](ApplicationCommon * /*app*/, const std::string &name) {
                                  return std::make_unique<gui::TestPopupWindow>(this, name);
                              });

        // NOTE
        // 1. attach popup
        // 2. then in apps-common/ApplicationCommonPopupBlueprints.cpp register blueprint for this popup
        // 3. if popup window is not attached - by default application will try to switch to this window
        //    but via popup handling
        // 4. popups are called via actions via sending message directly or via call:
        //        app::manager::Controller::sendAction(this, app::manager::actions::ShowPopup,
        //        std::make_unique<PowerOffPopupRequestParams>());
        //    or just directly doing what's done in controler
        //        return std::make_unique<app::manager::ActionRequest>(sender, app::manager::actions::ShowPopup,
        //        std::make_unique<gui::PopupRequestParams>(gui::popup::ID::AppTestPopup));
        attachPopups({gui::popup::ID::AppTestPopup});
    }

    void ApplicationTest::destroyUserInterface()
    {
        // NOTE: it should be empty - as it rather shouldn't be needed
        // but this is a place where i.e. you can set last exit state of this app
    }
} // namespace app