~aleteoryx/muditaos

ref: 42ca53a732487f7dabf5e06ee4c03f73c329882b muditaos/module-apps/apps-common/widgets/ProgressTimer.hpp -rw-r--r-- 3.0 KiB
42ca53a7 — Maciej-Mudita [MOS-686] Fix the accessibility of user files by MTP 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
79
80
81
82
83
84
85
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once

#include "TimerWithCallbacks.hpp"
#include <Timers/TimerHandle.hpp>
#include <time/time_conversion.hpp>
#include <atomic>
#include <chrono>
#include <string>
namespace
{
    constexpr auto baseTickDefault = std::chrono::milliseconds{1000};
} // namespace
namespace gui
{
    class Item;
} // namespace gui

namespace app
{
    class ApplicationCommon;

    enum class ProgressCountdownMode
    {
        Decreasing,
        Increasing
    };
    /** ProgressTimer connects Timer's features with UI representation.
     * The Timer's features consists of:
     * 1) counting time down,
     * 2) ability to perform action (via onIntervalCallback) periodically on reaching an interval
     * 3) ability to perform action (via onFinishedCallback) on reaching countdown end.
     * The UI representation consist of:
     * 1) ability to present time left on attached Text
     * 2) ability to present timer's progress on attached class realising Progress interface.
     */
    class ProgressTimer : public app::TimerWithCallbacks
    {
        app::ApplicationCommon *app = nullptr;
        gui::Item &parent;
        const std::string name;

      protected:
        std::atomic_bool isRunning{false};
        std::chrono::seconds duration{};
        std::chrono::milliseconds elapsed{};
        std::chrono::seconds interval{};
        std::chrono::milliseconds baseTickInterval{};
        bool hasInterval = false;

        sys::TimerHandle timerTask;

        std::function<void()> onFinishedCallback = nullptr;
        std::function<void()> onIntervalCallback = nullptr;
        ProgressCountdownMode countdownMode;
        utils::time::Duration::DisplayedFormat displayFormat;

        void startTimer();

        [[nodiscard]] auto onTimerTimeout(sys::Timer &timerTask) -> bool;
        [[nodiscard]] auto isFinished() const noexcept -> bool;
        [[nodiscard]] auto intervalReached() const noexcept -> bool;

        virtual void update();

      public:
        ProgressTimer(
            app::ApplicationCommon *app,
            gui::Item &parent,
            std::string timerName,
            std::chrono::milliseconds baseTick,
            ProgressCountdownMode countdownMode                    = ProgressCountdownMode::Decreasing,
            utils::time::Duration::DisplayedFormat displayedFormat = utils::time::Duration::DisplayedFormat::AutoM);
        void reset(std::chrono::seconds _duration,
                   std::chrono::seconds _interval = std::chrono::seconds::zero()) override;
        void start() override;
        void stop() override;
        std::chrono::milliseconds getElapsed() override;
        void registerOnFinishedCallback(std::function<void()> cb) override;
        void registerOnIntervalCallback(std::function<void()> cb) override;
        [[nodiscard]] auto isStopped() const noexcept -> bool override;
    };

} // namespace app