~aleteoryx/muditaos

ref: b16523e32502757ec6ae11ff7716f639de921a54 muditaos/module-apps/application-calendar/widgets/CalendarTimeItem.cpp -rw-r--r-- 3.1 KiB
b16523e3 — Michał Kamoń [EGD-6222] Fix the App Calendar memory leaks 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalendarDateItem.hpp"
#include "CalendarTimeItem.hpp"
#include <module-apps/widgets/DateAndTimeStyle.hpp>
#include <module-apps/widgets/WidgetsUtils.hpp>
#include <module-gui/gui/input/InputEvent.hpp>

namespace date_and_time = style::window::date_and_time;

namespace gui
{
    CalendarTimeItem::CalendarTimeItem(const std::string &description,
                                       TimeWidget::Type type,
                                       const std::function<void(const UTF8 &text)> &bottomBarTemporaryMode,
                                       const std::function<void()> &bottomBarRestoreFromTemporaryMode,
                                       TimePoint dateFrom,
                                       TimePoint dateTill)
    {
        setMinimumSize(style::window::default_body_width, date_and_time::height);
        setEdges(RectangleEdge::None);
        setMargins(gui::Margins(date_and_time::leftMargin, date_and_time::topMargin, 0, 0));

        vBox = new VBox(this, 0, 0, 0, 0);
        vBox->setEdges(RectangleEdge::None);

        timeWidget = new TimeWidget(vBox, description, type, bottomBarTemporaryMode, bottomBarRestoreFromTemporaryMode);
        timeWidget->loadData(std::chrono::hours(TimePointToHour24H(dateFrom)),
                             std::chrono::minutes(TimePointToMin(dateFrom)),
                             std::chrono::hours(TimePointToHour24H(dateTill)),
                             std::chrono::minutes(TimePointToMin(dateTill)));

        onLoadCallback = [&](std::shared_ptr<EventsRecord> event) {
            timeWidget->loadData(std::chrono::hours(TimePointToHour24H(event->date_from)),
                                 std::chrono::minutes(TimePointToMin(event->date_from)),
                                 std::chrono::hours(TimePointToHour24H(event->date_till)),
                                 std::chrono::minutes(TimePointToMin(event->date_till)));
        };

        onSaveCallback = [&](std::shared_ptr<EventsRecord> record) -> bool {
            auto fromTillDate = std::make_shared<utils::time::FromTillDate>(record->date_from, record->date_till);
            if (timeWidget->saveData(fromTillDate)) {
                record->date_from = fromTillDate->from;
                record->date_till = fromTillDate->till;
                return true;
            }
            return false;
        };
        passDefaultCallbacksFromListItem(this, vBox);
    }

    auto CalendarTimeItem::getFromTillDate() const -> std::shared_ptr<utils::time::FromTillDate>
    {
        auto fromTillDate = std::make_shared<utils::time::FromTillDate>();
        timeWidget->saveData(fromTillDate);
        return fromTillDate;
    }

    void CalendarTimeItem::setConnectionToSecondItem(CalendarTimeItem *item)
    {
        timeWidget->setConnectionToSecondItem(item->timeWidget);
    }

    void CalendarTimeItem::setConnectionToDateItem(CalendarDateItem *item)
    {
        timeWidget->setConnectionToDateItem(item->getDateWidget());
    }
} /* namespace gui */