~aleteoryx/muditaos

ref: 585c612f5d880ed715c768bf8290dfb9f7e76f81 muditaos/module-apps/application-calendar/windows/EventReminderWindow.cpp -rw-r--r-- 5.4 KiB
585c612f — marek303 [EGD-3315]Review changes: EventReminderWindow - localized date and time; unit tests for SelectFirstUpcoming query 5 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
#include "EventReminderWindow.hpp"
#include "application-calendar/widgets/CalendarStyle.hpp"
#include "module-apps/application-calendar/data/CalendarData.hpp"
#include <gui/widgets/Window.hpp>
#include <time/time_conversion.hpp>
#include "service-appmgr/ApplicationManager.hpp"

namespace gui
{
    constexpr static const int reminderLifeDuration = 20000;

    EventReminderWindow::EventReminderWindow(app::Application *app, std::string name)
        : AppWindow(app, style::window::calendar::name::event_reminder_window)
    {
        buildInterface();

        reminderTimer = std::make_unique<sys::Timer>(
            "CalendarReminderTimer", app, reminderLifeDuration, sys::Timer::Type::SingleShot);
        reminderTimer->connect([=](sys::Timer &) { reminderTimerCallback(); });
    }

    EventReminderWindow::~EventReminderWindow()
    {
        destroyTimer();
    }

    void EventReminderWindow::onClose()
    {
        destroyTimer();
    }

    void EventReminderWindow::rebuild()
    {
        erase();
        buildInterface();
    }

    void EventReminderWindow::buildInterface()
    {
        AppWindow::buildInterface();

        topBar->setActive(TopBar::Elements::BATTERY, true);
        topBar->setActive(TopBar::Elements::SIGNAL, true);
        topBar->setActive(gui::TopBar::Elements::TIME, true);

        bottomBar->setActive(gui::BottomBar::Side::CENTER, true);
        bottomBar->setText(gui::BottomBar::Side::CENTER, utils::localize.get(style::strings::common::ok));
        bottomBar->setBorderColor(ColorNoColor);

        const uint32_t w = this->getWidth() - style::window::default_left_margin - style::window::default_right_margin;
        const uint32_t h = this->getHeight() - bottomBar->getHeight();
        body             = new gui::VBox(this, style::window::default_left_margin, topBar->getHeight(), w, h);
        body->setBorderColor(gui::ColorNoColor);

        topImage = new gui::Image(body,
                                  style::window::calendar::imageCircleTop::x,
                                  style::window::calendar::imageCircleTop::y,
                                  0,
                                  0,
                                  style::window::calendar::imageCircleTop::name);
        topImage->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center));

        dateLabel = new Label(body, 0, 0, w, style::window::label::default_h);
        dateLabel->setFont(style::window::font::smallbold);
        dateLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        dateLabel->setBorderColor(ColorNoColor);

        timeLabel = new Label(body, 0, 0, w, style::window::label::default_h);
        timeLabel->setFont(style::window::font::largelight);
        timeLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        timeLabel->setBorderColor(ColorNoColor);

        bottomImage = new gui::Image(body,
                                     style::window::calendar::imageCircleBottom::x,
                                     style::window::calendar::imageCircleBottom::y,
                                     0,
                                     0,
                                     style::window::calendar::imageCircleBottom::name);
        bottomImage->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center));

        descriptionLabel = new Label(body, 0, 0, w, 2 * style::window::label::default_h);
        descriptionLabel->setFont(style::window::font::big);
        descriptionLabel->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
        descriptionLabel->setBorderColor(ColorNoColor);

        setFocusItem(body);
    }

    void EventReminderWindow::destroyInterface()
    {
        destroyTimer();
        erase();
    }

    void EventReminderWindow::startTimer()
    {
        reminderTimer->connect([=](sys::Timer &) { reminderTimerCallback(); });
        reminderTimer->reload();
    }

    void EventReminderWindow::destroyTimer()
    {
        reminderTimer->stop();
    }

    auto EventReminderWindow::handleSwitchData(SwitchData *data) -> bool
    {
        if (data == nullptr) {
            return false;
        }

        auto *item = dynamic_cast<EventRecordData *>(data);
        if (item == nullptr) {
            return false;
        }

        eventRecord    = item->getData();
        dateLabel->setText(TimePointToLocalizedDateString(eventRecord->date_from));
        timeLabel->setText(TimePointToLocalizedTimeString(eventRecord->date_from));
        descriptionLabel->setText(eventRecord->title);

        startTimer();

        return true;
    }

    bool EventReminderWindow::onInput(const gui::InputEvent &inputEvent)
    {
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }

        if (!inputEvent.isShortPress()) {
            return false;
        }

        if (inputEvent.keyCode == gui::KeyCode::KEY_ENTER) {
            closeReminder();
            return true;
        }

        return false;
    }

    void EventReminderWindow::reminderTimerCallback()
    {
        closeReminder();
    }

    void EventReminderWindow::closeReminder()
    {
        LOG_DEBUG("Switch to previous window");
        destroyTimer();

        sapm::ApplicationManager::messageSwitchApplication(
            application, "ApplicationDesktop", gui::name::window::main_window, nullptr);
    }

} /* namespace gui */