~aleteoryx/muditaos

ref: 3cbfe43a95610ad53a00a32bbfc3bf842013966f muditaos/module-apps/application-alarm-clock/widgets/AlarmItem.cpp -rw-r--r-- 3.3 KiB
3cbfe43a — Marcin Zieliński [MOS-791] Introduce tri-state switch widget 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmItem.hpp"
#include "AlarmClockStyle.hpp"
#include "application-alarm-clock/data/AlarmsData.hpp"
#include <InputEvent.hpp>
#include <time/dateCommon.hpp>
#include <time/time_conversion.hpp>

namespace gui
{
    AlarmItem::AlarmItem(std::shared_ptr<app::alarmClock::AlarmRRulePresenter> presenter)
        : AlarmRRuleItem(std::move(presenter))
    {
        setMinimumSize(style::window::default_body_width, style::alarmClock::window::item::height);
        setMargins(gui::Margins(0, style::margins::small, 0, style::alarmClock::window::item::botMargin));

        hBox = new gui::HBox(this, 0, 0, 0, 0);
        hBox->setEdges(gui::RectangleEdge::None);

        vBox = new gui::VBox(hBox, 0, 0, 0, 0);
        vBox->setEdges(gui::RectangleEdge::None);
        vBox->setMaximumSize(style::window::default_body_width, style::alarmClock::window::item::height);
        vBox->setMargins(gui::Margins(style::widgets::leftMargin, 0, 0, 0));

        timeLabel = new gui::Label(vBox, 0, 0, 0, 0);
        timeLabel->setEdges(gui::RectangleEdge::None);
        timeLabel->setMaximumSize(style::window::default_body_width, style::alarmClock::window::item::timeHeight);
        timeLabel->setMargins(gui::Margins(0, style::margins::small, 0, 0));
        timeLabel->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center});
        timeLabel->setFont(style::window::font::largelight);

        periodLabel = new gui::Label(vBox, 0, 0, 0, 0);
        periodLabel->setEdges(gui::RectangleEdge::None);
        periodLabel->setMaximumSize(style::window::default_body_width, style::alarmClock::window::item::periodHeight);
        periodLabel->setAlignment(gui::Alignment{gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top});
        periodLabel->setFont(style::window::font::small);

        onOffImage = new gui::ButtonTriState(hBox, ButtonTriState::State::On);
        onOffImage->setMargins(gui::Margins(0, 0, style::widgets::rightMargin, 0));

        setAlarm();

        dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            hBox->setArea({0, 0, newDim.w, newDim.h});
            return true;
        };
    }

    void AlarmItem::setAlarm()
    {
        auto time = HHMMToLocalizedString(getPresenter()->getAlarm()->alarmTime.hourOfDay,
                                          getPresenter()->getAlarm()->alarmTime.minuteOfHour,
                                          utils::time::TimestampType::Time);
        timeLabel->setText(time);
        onOffImage->switchState(getPresenter()->getAlarm()->enabled ? ButtonTriState::State::On : ButtonTriState::State::Off);

        if (getPresenter()->hasRecurrence()) {
            periodLabel->setText(getPresenter()->getDescription());
        }

        if (periodLabel->getText().empty()) {
            periodLabel->setMaximumSize(0, 0);
            timeLabel->setMaximumSize(style::window::default_body_width,
                                      style::alarmClock::window::item::timeHeight +
                                          style::alarmClock::window::item::periodHeight);
            vBox->resizeItems();
        }
    }
} // namespace gui