~aleteoryx/muditaos

ref: 5c2032ae929d48b62c2bcd9c0a27a38970350bfb muditaos/module-apps/apps-common/popups/WindowWithTimer.cpp -rw-r--r-- 2.3 KiB
5c2032ae — Maciej-Mudita [MOS-836] Fix for selecting SIM during onboarding 3 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "WindowWithTimer.hpp"
#include "ApplicationCommon.hpp"
#include "GuiTimer.hpp"

namespace gui
{
    namespace popup
    {
        constexpr auto timerName = "PopupTimer";
    } // namespace popup

    WindowWithTimer::WindowWithTimer(app::ApplicationCommon *app,
                                     const std::string &name,
                                     const std::chrono::milliseconds timeout)
        : AppWindow{app, name}, timeout{timeout}
    {
        popupTimer    = app::GuiTimerFactory::createSingleShotTimer(application, this, popup::timerName, timeout);
        timerCallback = [this, name](Item &, sys::Timer &timer) {
            if (application->getCurrentWindow()->getName() == name) {
                LOG_DEBUG("Delayed exit timer callback from: %s", name.c_str());
                application->returnToPreviousWindow();
                return true;
            }
            else {
                LOG_DEBUG("Delayed exit from: %s not succeeded, different window displayed already", name.c_str());
                return false;
            }
        };
    }

    void WindowWithTimer::resetTimer(const std::chrono::seconds newTimeout)
    {
        if (newTimeout != noTimeoutChange) {
            timeout = newTimeout;
        }
        if (!popupTimer.isValid()) {
            popupTimer = app::GuiTimerFactory::createSingleShotTimer(application, this, popup::timerName, timeout);
        }
        popupTimer.restart(timeout);
    }

    void WindowWithTimer::detachTimerIfExists()
    {
        if (popupTimer.isValid()) {
            popupTimer.stop();
            popupTimer.reset(nullptr);
        }
    }

    void WindowWithTimer::destroyInterface()
    {
        erase();
    }

    void WindowWithTimer::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        resetTimer();
    }

    void WindowWithTimer::onClose([[maybe_unused]] CloseReason reason)
    {
        detachTimerIfExists();
    }

    WindowWithTimer::~WindowWithTimer()
    {
        destroyInterface();
        detachTimerIfExists();
    }

    bool WindowWithTimer::onInput(const gui::InputEvent &inputEvent)
    {
        return false;
    }
} // namespace gui