~aleteoryx/muditaos

ref: b525fc20cd4b4b63d74bb667b5d127e703a68e24 muditaos/module-apps/application-meditation/widgets/IntervalBox.cpp -rw-r--r-- 5.9 KiB
b525fc20 — Marek Niepieklo [EGD-7877] Call errors while MTP file transfer 4 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "IntervalBox.hpp"
#include "Style.hpp"
#include "TimerProperty.hpp"

#include <gui/input/InputEvent.hpp>
#include <i18n/i18n.hpp>

#include <cassert>

using namespace gui;

namespace
{
    using minutes = std::chrono::minutes;
    const std::vector<minutes> chimeIntervals{
        minutes{0}, minutes{2}, minutes{5}, minutes{10}, minutes{15}, minutes{30}};
} // namespace

IntervalBox::IntervalBox(Item *parent, uint32_t x, uint32_t y, uint32_t w, uint32_t h, TimerProperty *timerSetter)
    : BoxLayout(parent, x, y, w, h), timerSetter(timerSetter)
{
    assert(timerSetter);
    build();
}

void IntervalBox::build()
{
    namespace boxStyle = style::meditation::intervalBox;

    auto topLabel = new Label(this,
                              boxStyle::topLabel::X,
                              boxStyle::topLabel::Y,
                              boxStyle::topLabel::Width,
                              boxStyle::topLabel::Height,
                              utils::translate("app_meditation_interval_chime"));
    topLabel->setAlignment(Alignment(Alignment::Horizontal::Left, Alignment::Vertical::Bottom));
    topLabel->setFont(style::window::font::small);
    topLabel->setEdges(RectangleEdge::None);

    bottomLabel = new Label(this,
                            boxStyle::bottomLabel::X,
                            boxStyle::bottomLabel::Y,
                            boxStyle::bottomLabel::Width,
                            boxStyle::bottomLabel::Height);
    bottomLabel->setAlignment(Alignment(Alignment::Horizontal::Center, Alignment::Vertical::Center));
    bottomLabel->setFont(style::window::font::small);
    bottomLabel->setEdges(RectangleEdge::Bottom);
    bottomLabel->setPenWidth(style::window::default_border_rect_no_focus);

    leftSwitchArrow  = new gui::Image(bottomLabel,
                                     boxStyle::arrow::LeftX,
                                     boxStyle::arrow::Y,
                                     boxStyle::arrow::Width,
                                     boxStyle::arrow::Height,
                                     "left_label_arrow");
    rightSwitchArrow = new gui::Image(bottomLabel,
                                      boxStyle::arrow::RightX,
                                      boxStyle::arrow::Y,
                                      boxStyle::arrow::Width,
                                      boxStyle::arrow::Height,
                                      "right_label_arrow");

    updateIntervals(ChimeIntervalList::Direction::Next);
    leftSwitchArrow->setVisible(false);
    rightSwitchArrow->setVisible(false);
}

bool IntervalBox::onFocus(bool state)
{
    if (state) {
        rescaleIntervals();
        bottomLabel->setPenWidth(style::window::default_border_focus_w);
        bottomLabel->setFont(style::window::font::smallbold);
    }
    else {
        bottomLabel->setPenWidth(style::window::default_border_rect_no_focus);
        bottomLabel->setFont(style::window::font::small);
    }
    auto currentMeditationTime = timerSetter->getTime();
    leftSwitchArrow->setVisible(state &&
                                chimeIntervals.hasNext(ChimeIntervalList::Direction::Previous, currentMeditationTime));
    rightSwitchArrow->setVisible(state &&
                                 chimeIntervals.hasNext(ChimeIntervalList::Direction::Next, currentMeditationTime));
    return true;
}

bool IntervalBox::onInput(const InputEvent &inputEvent)
{
    if (inputEvent.isShortRelease()) {
        if (inputEvent.is(KeyCode::KEY_LEFT)) {
            updateIntervals(ChimeIntervalList::Direction::Previous);
            return true;
        }
        else if (inputEvent.is(KeyCode::KEY_RIGHT)) {
            updateIntervals(ChimeIntervalList::Direction::Next);
            return true;
        }
    }
    return false;
}

void IntervalBox::updateIntervals(ChimeIntervalList::Direction direction)
{
    auto currentMeditationTime = timerSetter->getTime();
    if (!chimeIntervals.moveToNext(direction, currentMeditationTime)) {
        return;
    }
    intervalValue = chimeIntervals.getCurrent();
    bottomLabel->setText(ChimeIntervalList::toPrintableInterval(intervalValue));

    leftSwitchArrow->setVisible(chimeIntervals.hasNext(ChimeIntervalList::Direction::Previous, currentMeditationTime));
    rightSwitchArrow->setVisible(chimeIntervals.hasNext(ChimeIntervalList::Direction::Next, currentMeditationTime));
}

void IntervalBox::rescaleIntervals()
{
    while (intervalValue >= timerSetter->getTime() && intervalValue != minutes{0}) {
        updateIntervals(ChimeIntervalList::Direction::Previous);
    }
}

IntervalBox::ChimeIntervalList::ChimeIntervalList() : intervals(::chimeIntervals), current(intervals.begin())
{}

bool IntervalBox::ChimeIntervalList::moveToNext(Direction direction, std::chrono::minutes meditationTime) noexcept
{
    if (!hasNext(direction, meditationTime)) {
        return false;
    }
    if (direction == Direction::Next) {
        current++;
    }
    else {
        current--;
    }
    return true;
}

bool IntervalBox::ChimeIntervalList::hasNext(Direction direction, std::chrono::minutes meditationTime) const noexcept
{
    if (direction == Direction::Previous) {
        return current != intervals.begin();
    }
    auto result = std::next(current) != intervals.end();
    if (result) {
        result = *std::next(current) < meditationTime;
    }
    return result;
}

std::string IntervalBox::ChimeIntervalList::toPrintableInterval(std::chrono::minutes value)
{
    if (value.count() == 0) {
        return utils::translate("app_meditation_interval_none");
    }
    const std::string toReplace = "%0";
    std::string temp            = utils::translate("app_meditation_interval_every_x_minutes");
    temp.replace(temp.find(toReplace), toReplace.size(), std::to_string(static_cast<int>(value.count())));
    return temp;
}