~aleteoryx/muditaos

ref: 902a3a315eb8fd14bc8cadc827300f90f43a5152 muditaos/module-apps/application-meditation/widgets/MeditationTimer.cpp -rw-r--r-- 3.2 KiB
902a3a31 — Mateusz Piesta [MOS-806] Prepare scripts dependencies 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
86
87
88
89
90
91
92
93
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "MeditationTimer.hpp"
#include "Style.hpp"

#include <GuiTimer.hpp>
#include <purefs/filesystem_paths.hpp>
#include <service-audio/AudioServiceAPI.hpp>
#include <apps-common/widgets/ProgressTimerWithBarGraphAndCounter.hpp>

#include <gsl/assert>

namespace
{
    inline constexpr auto meditationTimerName = "MeditationTimer";
    inline constexpr std::chrono::seconds timerTick{1};
} // namespace
namespace gui
{
    namespace
    {
        constexpr auto TimerInterval = std::chrono::milliseconds{1000};
    } // namespace

    MeditationTimer::MeditationTimer(std::uint32_t x,
                                     std::uint32_t y,
                                     std::uint32_t width,
                                     std::uint32_t height,
                                     app::ApplicationMeditation *app,
                                     Item *_parent)
        : HBox(_parent, x, y, width, height), application{app}
    {
        setEdges(RectangleEdge::None);
        build();
    }

    void MeditationTimer::build()
    {
        namespace timerStyle = style::meditation::timer;

        const Point boxCenter(getX() + (getWidth() / 2), getY() + (getHeight() / 2));
        Circle::ShapeParams params;
        params.setCenterPoint(boxCenter)
            .setRadius(timerStyle::Radius)
            .setBorderColor(timerStyle::BorderColor)
            .setFocusBorderColor(timerStyle::BorderColor)
            .setPenWidth(timerStyle::PenWidth)
            .setFocusPenWidth(timerStyle::PenWidth);
        progressBar = new CircularProgressBar(this, params);

        text = new Text(progressBar, 0, 0, getWidth(), getHeight());
        text->setEdges(RectangleEdge::None);
        text->setFont(style::window::font::supersizemelight);
        text->setAlignment(Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        text->setEditMode(EditMode::Browse);

        timer = std::make_unique<app::ProgressTimerWithBarGraphAndCounter>(
            application, *this, meditationTimerName, timerTick);
        timer->attach(progressBar);
        timer->attach(text);
        timer->registerOnIntervalCallback(std::bind(&MeditationTimer::playSound, this));
    }

    auto MeditationTimer::onDimensionChanged(const BoundingBox &oldDim, const BoundingBox &newDim) -> bool
    {
        setPosition(newDim.x, newDim.y);
        setSize(newDim.w, newDim.h);
        return true;
    }

    void MeditationTimer::setCounterVisible(bool isVisible) noexcept
    {
        text->setVisible(isVisible);
    }

    app::TimerWithCallbacks &MeditationTimer::getTimer() noexcept
    {
        Expects(timer != nullptr);
        return *timer;
    }
    gui::Progress &MeditationTimer::getProgress() noexcept
    {
        Expects(progressBar != nullptr);
        return *progressBar;
    }
    void MeditationTimer::playSound()
    {
        AudioServiceAPI::PlaybackStart(application,
                                       audio::PlaybackType::Meditation,
                                       purefs::dir::getCurrentOSPath() / "assets/audio/meditation/gong.mp3");
    }
} // namespace gui