~aleteoryx/muditaos

ref: d9a1194e6f203247ebcef4b03f8ce5ebccc7c778 muditaos/module-apps/apps-common/popups/presenter/AlarmPresenter.cpp -rw-r--r-- 6.5 KiB
d9a1194e — Lukasz Mastalerz [BH-1688] Create a standard for logs 2 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) 2017-2023, 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/ServiceTimeName.hpp"
#include "service-time/AlarmMessage.hpp"
#include "time/dateCommon.hpp"

namespace
{
    constexpr auto ACTIVE_ALARM_TIMEOUT = 30min;
}

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 = std::move(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,
                                                     std::chrono::floor<std::chrono::minutes>(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>();
            if (timerHandle.isActive())
                timerHandle.stop();
        }
        LOG_DEBUG("Alarm snoozed!");
    }

    void AlarmPopupPresenter::stopAlarm()
    {
        if (isSnoozed()) {
            this->getModel()->stopAlarm<alarms::TurnOffSnoozeRequestMessage, alarms::TurnOffSnoozeResponseMessage>();
        }
        else {
            this->getModel()
                ->stopAlarm<alarms::RingingAlarmTurnOffRequestMessage, alarms::RingingAlarmTurnOffResponseMessage>();
            if (timerHandle.isActive())
                timerHandle.stop();
        }
        LOG_DEBUG("Alarm stopped!");
    }

    void AlarmPopupPresenter::skipToNextSnooze()
    {
        this->getModel()->processIfSnoozed();
        LOG_DEBUG("Snooze skipped");
    }

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

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

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

    bool AlarmPopupPresenter::haveSnoozedSkip()
    {
        return isSnoozed() && !snoozedTill().empty();
    }

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

    std::string AlarmPopupPresenter::startedAt()
    {
        return HHMMToLocalizedString(this->getModel()->record->alarmTime.hourOfDay,
                                     this->getModel()->record->alarmTime.minuteOfHour,
                                     utils::time::TimestampType::Clock);
    }

    AlarmPopupPresenter::AlarmPopupPresenter(ApplicationCommon *app) : AlarmPopupContract::Presenter(app)
    {
        timerHandle = sys::TimerFactory::createSingleShotTimer(
            app, "AlarmTimer", ACTIVE_ALARM_TIMEOUT, [this](sys::Timer &) { stopAlarm(); });
        timerHandle.start();
    }
} // namespace app::popup