~aleteoryx/muditaos

ref: b7f4ba33e3f5a35eeb431b67d7fdd3a304a32543 muditaos/module-apps/apps-common/popups/presenter/AlarmPresenter.cpp -rw-r--r-- 5.6 KiB
b7f4ba33 — Mateusz Piesta [BH-765] Turn off the device 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
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "AlarmPresenter.hpp"
#include "Constants.hpp"
#include "service-time/Constants.hpp"
#include "service-time/AlarmMessage.hpp"
#include "time/dateCommon.hpp"

namespace app::popup
{
    AlarmPopupContract::AlarmModel::AlarmModel(ApplicationCommon *app) : AsyncCallbackReceiver(app), app(app)
    {}

    void AlarmPopupContract::AlarmModel::attach(Presenter *p)
    {
        presenter = p;
    }

    void AlarmPopupContract::AlarmModel::set(std::shared_ptr<AlarmEventRecord> record)
    {
        this->record = std::move(record);
    }

    void AlarmPopupContract::AlarmModel::setSnoozed(std::vector<SingleEventRecord> snoozed)
    {
        this->snoozedRecord = snoozed;
    }

    void AlarmPopupContract::AlarmModel::reset()
    {
        if (!snoozedRecord.empty()) {
            snoozedRecord.clear();
        }
        snoozedTill    = std::string{};
        isSnoozedAlarm = false;
    }

    auto async = app::AsyncRequest::createFromMessage;

    template <typename requestType, typename responseType> void AlarmPopupContract::AlarmModel::snoozeAlarm()
    {
        auto request =
            std::make_unique<requestType>(record->ID, TimePointNow() + std::chrono::minutes(record->snoozeDuration));
        auto task = async(std::move(request), service::name::service_time);
        auto cb   = [&](auto response) {
            auto result = dynamic_cast<responseType *>(response);
            assert(result);
            assert(result->success);
            if (!result) {
                return false;
            }
            presenter->processIfSnoozed();
            return true;
        };
        task->execute(app, this, cb);
    }

    template <typename requestType, typename responseType> void AlarmPopupContract::AlarmModel::stopAlarm()
    {
        auto request = std::make_unique<requestType>(record->ID);
        auto task    = async(std::move(request), service::name::service_time);
        auto cb      = [&](auto response) {
            auto result = dynamic_cast<responseType *>(response);
            assert(result);
            assert(result->success);
            if (!result) {
                return false;
            }
            presenter->processIfSnoozed();
            return true;
        };
        task->execute(app, this, cb);
    }

    void AlarmPopupContract::AlarmModel::setRefreshWindowCallback(std::function<void()> callback)
    {
        refreshWindowCallback = callback;
    }

    void AlarmPopupContract::AlarmModel::processIfSnoozed()
    {
        if (!snoozedRecord.empty()) {
            isSnoozedAlarm = true;
            snoozedTill    = utils::time::TimestampFactory()
                              .createTimestamp(utils::time::TimestampType::Clock,
                                               TimePointToTimeT(snoozedRecord.front().startDate))
                              ->str();

            auto request = std::make_unique<alarms::AlarmGetRequestMessage>(snoozedRecord.front().parent->ID);
            auto task    = async(std::move(request), service::name::service_time);
            auto cb      = [&](auto response) {
                auto result = dynamic_cast<alarms::AlarmGetResponseMessage *>(response);
                assert(result);
                if (!result) {
                    return false;
                }
                record = std::make_shared<AlarmEventRecord>(result->alarm);
                refreshWindowCallback();
                return true;
            };
            task->execute(app, this, cb);
            snoozedRecord.erase(snoozedRecord.begin());
        }
        else {
            isSnoozedAlarm = false;
            presenter->handleAlarmSnoozed();
        }
    }

    std::uint32_t AlarmPopupPresenter::getSnoozeTime()
    {
        return this->getModel()->record->snoozeDuration;
    }

    bool AlarmPopupPresenter::isSnoozeAble()
    {
        return this->getModel()->record != nullptr && getSnoozeTime() != 0;
    }

    void AlarmPopupPresenter::snoozeHit()
    {
        if (isSnoozed()) {
            this->getModel()
                ->snoozeAlarm<alarms::PostponeSnoozeRequestMessage, alarms::PostponeSnoozeResponseMessage>();
        }
        else {
            this->getModel()
                ->snoozeAlarm<alarms::RingingAlarmSnoozeRequestMessage, alarms::RingingAlarmSnoozeResponseMessage>();
        }
        LOG_DEBUG("Snoozed!");
    }

    void AlarmPopupPresenter::stopAlarm()
    {
        if (isSnoozed()) {
            this->getModel()->stopAlarm<alarms::TurnOffSnoozeRequestMessage, alarms::TurnOffSnoozeResponseMessage>();
        }
        else {
            this->getModel()
                ->stopAlarm<alarms::RingingAlarmTurnOffRequestMessage, alarms::RingingAlarmTurnOffResponseMessage>();
        }
        LOG_DEBUG("Stopped!");
    }

    void AlarmPopupPresenter::handleAlarmSnoozed()
    {
        getApp()->returnToPreviousWindow();
    }

    void AlarmPopupPresenter::handleAlarmTurnedOff()
    {
        getApp()->returnToPreviousWindow();
    }

    bool AlarmPopupPresenter::isSnoozed()
    {
        return this->getModel()->isSnoozedAlarm;
    }

    std::string AlarmPopupPresenter::snoozedTill()
    {
        return this->getModel()->snoozedTill;
    }

    std::string AlarmPopupPresenter::startedAt()
    {
        return utils::time::TimestampFactory()
            .createTimestamp(utils::time::TimestampType::Clock, TimePointToTimeT(this->getModel()->record->startDate))
            ->str();
    }

    AlarmPopupPresenter::AlarmPopupPresenter(ApplicationCommon *app) : AlarmPopupContract::Presenter(app)
    {}
} // namespace app::popup