~aleteoryx/muditaos

ref: 6b329ce52257773e3a027fc90a15b352129a35b8 muditaos/module-apps/apps-common/widgets/ProgressTimerWithBarGraphAndCounter.cpp -rw-r--r-- 2.5 KiB
6b329ce5 — Marcin Zieliński [MOS-242] Fix windows flow after PIN mistakes 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
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ProgressTimerWithBarGraphAndCounter.hpp"
#include <Text.hpp>
#include <ProgressBar.hpp>
#include <ApplicationCommon.hpp>
#include <apps-common/GuiTimer.hpp>
#include <gsl/assert>

namespace
{
    inline constexpr auto increasingModePrefix = "-";
}
namespace app
{
    void ProgressTimerWithBarGraphAndCounter::update()
    {
        updateText();
        updateProgress();
        updateTimeWidget();
        ProgressTimer::update();
    }

    void ProgressTimerWithBarGraphAndCounter::updateText()
    {
        using utils::time::Duration;
        if (text == nullptr) {
            return;
        }
        const auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(duration - elapsed);
        const Duration remainingDuration{secondsRemaining};
        UTF8 timerText;
        if (countdownMode == ProgressCountdownMode::Increasing && secondsRemaining != std::chrono::seconds::zero()) {
            timerText += increasingModePrefix;
        }
        timerText += remainingDuration.str(displayFormat);
        text->setText(std::move(timerText));
    }

    void ProgressTimerWithBarGraphAndCounter::updateTimeWidget()
    {
        if (timeWidget == nullptr) {
            return;
        }
        const auto secondsRemaining = std::chrono::duration_cast<std::chrono::seconds>(duration - elapsed);
        timeWidget->setMinutesBox(secondsRemaining.count() / 60);
        timeWidget->setSecondsBox(secondsRemaining.count() % 60);
    }

    void ProgressTimerWithBarGraphAndCounter::updateProgress()
    {
        if (progress != nullptr) {
            const auto percentage =
                static_cast<float>(std::chrono::duration_cast<std::chrono::seconds>(elapsed).count()) /
                duration.count();
            const auto currentStep = percentage * progress->getMaximum();
            progress->setValue(std::ceil(currentStep));
        }
    }

    void ProgressTimerWithBarGraphAndCounter::attach(gui::Progress *_progress)
    {
        Expects(_progress != nullptr);
        progress = _progress;
    }

    void ProgressTimerWithBarGraphAndCounter::attach(gui::Text *_text)
    {
        Expects(_text != nullptr);
        text = _text;
    }

    void ProgressTimerWithBarGraphAndCounter::attach(gui::TimeFixedWidget *_timeWidget)
    {
        Expects(_timeWidget != nullptr);
        timeWidget = _timeWidget;
    }
} // namespace app