~aleteoryx/muditaos

ref: 4942167c0a410a504edde0347e2484fbd3b9bc10 muditaos/module-apps/Application.cpp -rw-r--r-- 24.6 KiB
4942167c — Tomas Rogala [EGD-3713] Rework repeat option data flow 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include <sstream>
#include <iomanip>
// module-utils
#include "log/log.hpp"
// module-services
#include "service-gui/messages/DrawMessage.hpp"
#include "service-appmgr/messages/APMMessage.hpp"
#include "service-evtmgr/messages/EVMessages.hpp"
#include "service-appmgr/ApplicationManager.hpp"
#include "service-db/api/DBServiceAPI.hpp"
#include "service-cellular/ServiceCellular.hpp"
#include "service-cellular/api/CellularServiceAPI.hpp"
// module-gui
#include "gui/core/DrawCommand.hpp"
#include "gui/input/InputEvent.hpp"
// module-sys
#include "SystemManager/SystemManager.hpp"

#include "Application.hpp"
#include "MessageType.hpp"
#include "messages/AppMessage.hpp"
#include "windows/AppWindow.hpp"
#include <Text.hpp>
#include <cassert>

#include "common_data/EventStore.hpp"

namespace app
{

    const char *Application::stateStr(Application::State st)
    {
        switch (st) {
        case State::NONE:
            return "NONE";
        case State::DEACTIVATED:
            return "DEACTIVATED";
        case State::INITIALIZING:
            return "INITIALIZING";
        case State::ACTIVATING:
            return "ACTIVATING";
        case State::ACTIVE_FORGROUND:
            return "ACTIVE_FORGROUND";
        case State::ACTIVE_BACKGROUND:
            return "ACTIVE_BACKGROUND";
        case State::DEACTIVATING:
            return "DEACTIVATING";
        default:
            return "FixIt";
        }
    }

    Application::Application(
        std::string name, std::string parent, bool startBackground, uint32_t stackDepth, sys::ServicePriority priority)
        : Service(name, parent, stackDepth, priority),
          longPressTimer(CreateAppTimer(
              key_timer_ms, true, [&]() { longPressTimerCallback(); }, "longPressTimer")),
          startBackground{startBackground}
    {
        keyTranslator = std::make_unique<gui::KeyInputSimpleTranslation>();
        busChannels.push_back(sys::BusChannels::ServiceCellularNotifications);
        if (startBackground) {
            setState(State::ACTIVE_BACKGROUND);
        }
        longPressTimer.restart();
    }

    Application::~Application()
    {
        for (auto it = windows.begin(); it != windows.end(); it++) {
            delete it->second;
        }
        windows.clear();
    }

    Application::State Application::getState()
    {
        return state;
    }

    void Application::setState(State st)
    {
#if DEBUG_APPLICATION_MANAGEMENT == 1
        LOG_DEBUG("[%s] (%s) -> (%s)", GetName().c_str(), stateStr(state), stateStr(st));
#endif
        state = st;
    }

    void Application::TickHandler(uint32_t id)
    {
        auto appTimer = std::find(appTimers.begin(), appTimers.end(), id);
        if (appTimer != appTimers.end()) {
            appTimer->runCallback();
        }
        else {
            LOG_ERROR("Requested timer doesn't exist here (ID: %s)\n",
                      std::to_string(id).c_str()); // either timer was deleted or this id should not arrive.
        }
    }

    void Application::DeleteTimer(AppTimer &timer)
    {
        Service::DeleteTimer(timer.getID()); // remove the real FreeRTOS timer
        auto timerOnTheList = std::find(appTimers.begin(), appTimers.end(), timer);
        if (timerOnTheList != appTimers.end()) {
            appTimers.erase(timerOnTheList);
        }
    }

    [[deprecated("only for compatibility")]] void Application::DeleteTimer(uint32_t id)
    {
        auto found = std::find(appTimers.begin(), appTimers.end(), id);
        if (found != appTimers.end()) {
            DeleteTimer(*found);
        }
    }

    void Application::longPressTimerCallback()
    {
        // TODO if(check widget type long press trigger)
        uint32_t time = xTaskGetTickCount();
        if (keyTranslator->timeout(time)) {
            // previous key press was over standard keypress timeout - send long press
            gui::InputEvent iev = keyTranslator->translate(time);
            messageInputEventApplication(this, this->GetName(), iev);
            // clean previous key
            keyTranslator->prev_key_press = {};
        }
    }

    void Application::render(gui::RefreshModes mode)
    {
        if (getCurrentWindow() == nullptr) {
            LOG_ERROR("Current window is not defined");
            return;
        }

        // send drawing commands only when if application is in active and visible.
        if (state == State::ACTIVE_FORGROUND) {
            auto currwin = getCurrentWindow();
            if (Store::Battery::get().state == Store::Battery::State::Charging) {
                currwin->batteryCharging(true);
            }
            else {
                currwin->updateBatteryLevel(Store::Battery::get().level);
            }
            currwin->setSIM();
            currwin->updateSignalStrength();

            std::list<gui::DrawCommand *> commandsList = currwin->buildDrawList();

            if (shutdownInProgress) {
                auto msg =
                    std::make_shared<sgui::DrawMessage>(commandsList, mode, sgui::DrawMessage::DrawCommand::SHUTDOWN);
                sys::Bus::SendUnicast(msg, "ServiceGUI", this);
            }
            else if (suspendInProgress) {
                auto msg =
                    std::make_shared<sgui::DrawMessage>(commandsList, mode, sgui::DrawMessage::DrawCommand::SUSPEND);
                sys::Bus::SendUnicast(msg, "ServiceGUI", this);
            }
            else {
                auto msg =
                    std::make_shared<sgui::DrawMessage>(commandsList, mode, sgui::DrawMessage::DrawCommand::NORMAL);
                sys::Bus::SendUnicast(msg, "ServiceGUI", this);
            }
        }

        if (suspendInProgress)
            suspendInProgress = false;
    }
    void Application::blockEvents(bool isBlocked)
    {
        acceptInput = isBlocked;
    }
    void Application::switchWindow(const std::string &windowName,
                                   gui::ShowMode cmd,
                                   std::unique_ptr<gui::SwitchData> data)
    {

        std::string window;
#if DEBUG_APPLICATION_MANAGEMENT == 1
        LOG_INFO("switching [%s] to window: %s data description: %s",
                 GetName().c_str(),
                 windowName.length() ? windowName.c_str() : gui::name::window::main_window.c_str(),
                 data ? data->getDescription().c_str() : "");
#endif

        // case to handle returning to previous application
        if (windowName == "LastWindow") {
            window = getCurrentWindow()->getName();
            auto msg =
                std::make_shared<AppSwitchWindowMessage>(window, getCurrentWindow()->getName(), std::move(data), cmd);
            sys::Bus::SendUnicast(msg, this->GetName(), this);
        }
        else {
            window   = windowName.empty() ? gui::name::window::main_window : windowName;
            auto msg = std::make_shared<AppSwitchWindowMessage>(
                window, getCurrentWindow() ? getCurrentWindow()->getName() : "", std::move(data), cmd);
            sys::Bus::SendUnicast(msg, this->GetName(), this);
        }
    }

    void Application::returnToPreviousWindow()
    {
        auto prevWindow = getPrevWindow();
        if (prevWindow == gui::name::window::no_window) {
            LOG_INFO("Back to previous application");
            cleanPrevWindw();
            sapm::ApplicationManager::messageSwitchPreviousApplication(this);
        }
        else {
            LOG_INFO("Back to previous window %s", prevWindow.c_str());
            switchWindow(prevWindow, gui::ShowMode::GUI_SHOW_RETURN);
        }
    }

    void Application::refreshWindow(gui::RefreshModes mode)
    {
        auto msg = std::make_shared<AppRefreshMessage>(mode);
        sys::Bus::SendUnicast(msg, this->GetName(), this);
    }

    sys::Message_t Application::DataReceivedHandler(sys::DataMessage *msgl)
    {
        auto msg = dynamic_cast<CellularNotificationMessage *>(msgl);
        if (msg != nullptr && msg->type == CellularNotificationMessage::Type::SignalStrengthUpdate) {
            return handleSignalStrengthUpdate(msgl);
        }
        else if (msgl->messageType == MessageType::AppInputEvent) {
            return handleInputEvent(msgl);
        }
        else if (msgl->messageType == MessageType::KBDKeyEvent) {
            return handleKBDKeyEvent(msgl);
        }
        else if (msgl->messageType == MessageType::EVMBatteryLevel) {
            return handleBatteryLevel(msgl);
        }
        else if (msgl->messageType == MessageType::EVMChargerPlugged) {
            return handleChargerPlugged(msgl);
        }
        else if (msgl->messageType == MessageType::EVMMinuteUpdated) {
            return handleMinuteUpdated(msgl);
        }
        else if (msgl->messageType == MessageType::AppSwitch) {
            return handleApplicationSwitch(msgl);
        }
        else if (msgl->messageType == MessageType::AppSwitchWindow) {
            return handleSwitchWindow(msgl);
        }
        else if (msgl->messageType == MessageType::AppClose) {
            return handleAppClose(msgl);
        }
        else if (msgl->messageType == MessageType::AppRebuild) {
            return handleAppRebuild(msg);
        }
        else if (msgl->messageType == MessageType::AppRefresh) {
            return handleAppRefresh(msgl);
        }
        else if (dynamic_cast<sevm::SIMMessage *>(msgl) != nullptr) {
            return handleSIMMessage(msgl);
        }
        return msgNotHandled();
    }

    sys::Message_t Application::handleSignalStrengthUpdate(sys::DataMessage *msgl)
    {
        if ((state == State::ACTIVE_FORGROUND) && getCurrentWindow()->updateSignalStrength()) {
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        return msgHandled();
    }

    sys::Message_t Application::handleInputEvent(sys::DataMessage *msgl)
    {
        AppInputEventMessage *msg = reinterpret_cast<AppInputEventMessage *>(msgl);
        if (getCurrentWindow() != nullptr && getCurrentWindow()->onInput(msg->getEvent())) {
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        return msgHandled();
    }

    sys::Message_t Application::handleKBDKeyEvent(sys::DataMessage *msgl)
    {
        if (this->getState() != app::Application::State::ACTIVE_FORGROUND) {
            LOG_FATAL("!!! Terrible terrible damage! application with no focus grabbed key!");
        }
        sevm::KbdMessage *msg = static_cast<sevm::KbdMessage *>(msgl);
        gui::InputEvent iev   = keyTranslator->translate(msg->key);
        if (iev.keyCode != gui::KeyCode::KEY_UNDEFINED) {
            messageInputEventApplication(this, this->GetName(), iev);
        }
        return msgHandled();
    }

    sys::Message_t Application::handleBatteryLevel(sys::DataMessage *msgl)
    {
        auto msg = static_cast<sevm::BatteryLevelMessage *>(msgl);
        LOG_INFO("Application battery level: %d", msg->levelPercents);

        if (getCurrentWindow()->updateBatteryLevel(msg->levelPercents)) {
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        return msgHandled();
    }

    sys::Message_t Application::handleChargerPlugged(sys::DataMessage *msgl)
    {
        auto *msg = static_cast<sevm::BatteryPlugMessage *>(msgl);
        if (msg->plugged == true) {
            LOG_INFO("Application charger connected");
            getCurrentWindow()->batteryCharging(true);
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        else {
            LOG_INFO("Application charger disconnected");
            getCurrentWindow()->batteryCharging(false);
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }

        refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        return msgHandled();
    }

    sys::Message_t Application::handleMinuteUpdated(sys::DataMessage *msgl)
    {
        auto *msg = static_cast<sevm::RtcMinuteAlarmMessage *>(msgl);
        getCurrentWindow()->updateTime(msg->timestamp, !settings.timeFormat12);
        if (state == State::ACTIVE_FORGROUND) {
            refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }
        return msgHandled();
    }

    sys::Message_t Application::handleApplicationSwitch(sys::DataMessage *msgl)
    {
        AppSwitchMessage *msg = reinterpret_cast<AppSwitchMessage *>(msgl);
        bool handled          = false;
        LOG_DEBUG("AppSwitch: %s", msg->getTargetApplicationName().c_str());
        // Application is starting or it is in the background. Upon switch command if name if correct it goes
        // foreground
        if ((state == State::ACTIVATING) || (state == State::INITIALIZING) || (state == State::ACTIVE_BACKGROUND)) {

            if (msg->getTargetApplicationName() == this->GetName()) {
                setState(State::ACTIVE_FORGROUND);
                if (sapm::ApplicationManager::messageConfirmSwitch(this)) {
                    LOG_INFO("target Window: %s : target description: %s",
                             msg->getTargetWindowName().c_str(),
                             msg->getData() ? msg->getData()->getDescription().c_str() : "");
                    switchWindow(msg->getTargetWindowName(), std::move(msg->getData()));
                    handled = true;
                }
                else {
                    // TODO send to itself message to close
                    LOG_ERROR("Failed to communicate ");
                }
            }
            else {
                LOG_ERROR("Received switch message outside of activation flow");
            }
        }
        else if (state == State::ACTIVE_FORGROUND) {
            if (msg->getTargetApplicationName() == this->GetName()) {
                // if window name and data are null pointers this is a message informing
                // that application should go to background mode
                if ((msg->getTargetWindowName() == "") && (msg->getData() == nullptr)) {
                    setState(State::ACTIVE_BACKGROUND);
                    if (sapm::ApplicationManager::messageConfirmSwitch(this)) {
                        handled = true;
                    }
                    else {
                        // TODO send to itself message to close
                        LOG_ERROR("Failed to communicate ");
                    }
                }
                // if application is in front and receives message with defined window it should
                // change to that window.
                else {
                    switchWindow(msg->getTargetWindowName(), std::move(msg->getData()));
                    handled = true;
                }
            }
            else {
                LOG_ERROR("Received switch message outside of activation flow");
            }
        }
        else {
            LOG_ERROR("Wrong internal application %s switch to ACTIVE state form %s",
                      msg->getTargetApplicationName().c_str(),
                      stateStr(state));
        }
        if (handled) {
            return msgHandled();
        }
        return msgNotHandled();
    }

    sys::Message_t Application::handleSwitchWindow(sys::DataMessage *msgl)
    {
        auto msg = static_cast<AppSwitchWindowMessage *>(msgl);
        // check if specified window is in the application
        auto it = windows.find(msg->getWindowName());
        if (it != windows.end()) {
            auto switchData = std::move(msg->getData());
            if (switchData && switchData->ignoreCurrentWindowOnStack) {
                popToWindow(getPrevWindow());
            }
            getCurrentWindow()->onClose();
            setActiveWindow(msg->getWindowName());

            getCurrentWindow()->handleSwitchData(switchData.get());

            // check if this is case where application is returning to the last visible window.
            if ((switchData != nullptr) && (msg->LastSeenWindow)) {}
            else {
                getCurrentWindow()->onBeforeShow(msg->getCommand(), switchData.get());
                auto ret = dynamic_cast<gui::SwitchSpecialChar *>(switchData.get());
                if (ret != nullptr && switchData != nullptr) {
                    auto text = dynamic_cast<gui::Text *>(getCurrentWindow()->getFocusItem());
                    if (text != nullptr) {
                        text->addText(ret->getDescription());
                    }
                }
            }
            refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
        }
        else {
            LOG_ERROR("No such window: %s", msg->getWindowName().c_str());
        }
        return msgHandled();
    }

    sys::Message_t Application::handleAppClose(sys::DataMessage *msgl)
    {
        setState(State::DEACTIVATING);
        sapm::ApplicationManager::messageConfirmClose(this);
        return msgHandled();
    }

    sys::Message_t Application::handleAppRebuild(sys::DataMessage *msgl)
    {
        LOG_INFO("Application %s rebuilding gui", GetName().c_str());
        for (auto it = windows.begin(); it != windows.end(); it++) {
            LOG_DEBUG("Rebuild: %s", it->first.c_str());
            if (!it->second) {
                LOG_ERROR("NO SUCH WINDOW");
            }
            else {
                it->second->rebuild();
            }
        }
        LOG_INFO("Refresh app with focus!");
        if (state == State::ACTIVE_FORGROUND) {
            refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
        }
        LOG_INFO("App rebuild done");
        return msgHandled();
    }

    sys::Message_t Application::handleAppRefresh(sys::DataMessage *msgl)
    {
        AppRefreshMessage *msg = reinterpret_cast<AppRefreshMessage *>(msgl);
        render(msg->getMode());
        return msgHandled();
    }

    sys::Message_t Application::handleSIMMessage(sys::DataMessage *msgl)
    {
        getCurrentWindow()->setSIM();
        refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        return msgHandled();
    }

    sys::ReturnCodes Application::InitHandler()
    {
        bool initState = true;
        setState(State::INITIALIZING);
        //	uint32_t start = xTaskGetTickCount();
        settings = DBServiceAPI::SettingsGet(this);
        //	uint32_t stop = xTaskGetTickCount();
        //	LOG_INFO("DBServiceAPI::SettingsGet %d", stop-start);
        initState = (settings.dbID == 1);

        // send response to application manager true if successful, false otherwise.
        sapm::ApplicationManager::messageRegisterApplication(this, initState, startBackground);
        sys::ReturnCodes retCode = (initState ? sys::ReturnCodes::Success : sys::ReturnCodes::Failure);
        return retCode;
    }

    sys::ReturnCodes Application::DeinitHandler()
    {
        LOG_INFO("Closing an application: %s", GetName().c_str());
        for (const auto &[windowName, window] : windows) {
            LOG_INFO("Closing a window: %s", windowName.c_str());
            window->onClose();
        }
        return sys::ReturnCodes::Success;
    }

    void Application::setActiveWindow(const std::string &windowName)
    {
        pushWindow(windowName);
        acceptInput = true;
    }

    bool Application::setVolume(const audio::Volume &value,
                                const audio::Profile::Type &profileType,
                                const audio::PlaybackType &playbackType)
    {
        const auto ret = AudioServiceAPI::SetVolume(this, value, profileType, playbackType);
        return ret == audio::RetCode::Success;
    }

    bool Application::adjustCurrentVolume(const int step)
    {
        audio::Volume vol;
        auto ret = AudioServiceAPI::GetOutputVolume(this, vol);
        if (ret == audio::RetCode::Success) {
            ret = ((static_cast<int>(vol) + step) < 0) ? audio::RetCode::Success
                                                       : AudioServiceAPI::SetOutputVolume(this, vol + step);
        }
        return ret == audio::RetCode::Success;
    }

    void Application::messageSwitchApplication(sys::Service *sender,
                                               std::string application,
                                               std::string window,
                                               std::unique_ptr<gui::SwitchData> data)
    {
        auto msg = std::make_shared<AppSwitchMessage>(application, window, std::move(data));
        sys::Bus::SendUnicast(msg, application, sender);
    }

    void Application::messageRefreshApplication(sys::Service *sender,
                                                std::string application,
                                                std::string window,
                                                gui::SwitchData *data)
    {
        auto msg = std::make_shared<AppMessage>(MessageType::AppRefresh);
        sys::Bus::SendUnicast(msg, application, sender);
    }

    void Application::messageCloseApplication(sys::Service *sender, std::string application)
    {

        auto msg = std::make_shared<AppMessage>(MessageType::AppClose);
        sys::Bus::SendUnicast(msg, application, sender);
    }

    void Application::messageRebuildApplication(sys::Service *sender, std::string application)
    {
        auto msg = std::make_shared<AppRebuildMessage>();
        sys::Bus::SendUnicast(msg, application, sender);
    }

    void Application::messageInputEventApplication(sys::Service *sender,
                                                   std::string application,
                                                   const gui::InputEvent &event)
    {
        auto msg = std::make_shared<AppInputEventMessage>(event);
        sys::Bus::SendUnicast(msg, application, sender);
    }

    bool Application::popToWindow(const std::string &window)
    {
        if (window == gui::name::window::no_window) {
            bool ret = false;
            if (windowStack.size() <= 1) {
                windowStack.clear();
                ret = true;
            }
            return ret;
        }

        auto ret = std::find(windowStack.begin(), windowStack.end(), window);
        if (ret != windowStack.end()) {
            LOG_INFO(
                "Pop last window(s) [%d] :  %s", static_cast<int>(std::distance(ret, windowStack.end())), ret->c_str());
            windowStack.erase(std::next(ret), windowStack.end());
            return true;
        }
        return false;
    }

    void Application::pushWindow(const std::string &newWindow)
    {
        // handle if window was already on
        if (popToWindow(newWindow)) {
            return;
        }
        else {
            windowStack.push_back(newWindow);
        }
#if DEBUG_APPLICATION_MANAGEMENT == 1
        LOG_DEBUG("[%d] newWindow: %s", windowStack.size(), newWindow.c_str());
        for (auto &el : windowStack) {
            LOG_DEBUG("-> %s", el.c_str());
        }
        LOG_INFO("\n\n");
#endif
    };

    const std::string Application::getPrevWindow() const
    {
        if (this->windowStack.size() <= 1) {
            return gui::name::window::no_window;
        }
        return *std::prev(windowStack.end(), 2);
    }

    void Application::Application::cleanPrevWindw()
    {
        this->windowStack.clear();
    }

    gui::AppWindow *Application::getWindow(const std::string &window)
    {
        auto it = windows.find(window);
        if (it != windows.end()) {
            return it->second;
        }
        return nullptr;
    }

    gui::AppWindow *Application::getCurrentWindow()
    {
        std::string window = "";
        if (windowStack.size() == 0) {
            window = gui::name::window::main_window;
        }
        else {
            window = windowStack.back();
        }

        return getWindow(window);
    }

    AppTimer Application::CreateAppTimer(TickType_t interval,
                                         bool isPeriodic,
                                         std::function<void()> callback,
                                         const std::string &name)
    {
        auto id    = CreateTimer(interval, isPeriodic, name);
        auto timer = AppTimer(this, id, callback, name);
        appTimers.push_back(timer);
        return timer; // return ptr to the timer on the list
    }

    AppTimer::AppTimer(Application *parent, uint32_t id, std::function<void()> callback, const std::string &name)
        : parent(parent)
    {
        this->id = id;
        registerCallback(callback);
    }
    AppTimer::AppTimer() = default;

    AppTimer::~AppTimer()
    {
        callback = nullptr;
    }

    void AppTimer::registerCallback(std::function<void()> callback)
    {
        this->callback = callback;
    }
    void AppTimer::runCallback()
    {
        callback();
    }
    uint32_t AppTimer::getID()
    {
        return id;
    }
    void AppTimer::stop()
    {
        if (parent) {
            parent->stopTimer(getID());
        }
    }
    void AppTimer::restart()
    {
        if (parent) {
            parent->ReloadTimer(getID());
        }
    }
    bool AppTimer::operator==(const AppTimer &rhs) const
    {
        return this->id == rhs.id;
    }
    bool AppTimer::operator==(const uint32_t &rhs) const
    {
        return this->id == rhs;
    }
} /* namespace app */