~aleteoryx/muditaos

ref: 3cc3f50f7b9364ffac38349238cd6d9aa243dc23 muditaos/module-apps/apps-common/popups/presenter/AlarmPresenter.hpp -rw-r--r-- 5.5 KiB
3cc3f50f — Maciej Gibowicz [BH-1809][BH-1835] Add date format setting 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include "time/dateCommon.hpp"
#include "Timers/TimerFactory.hpp"
#include <AlarmEventRecord.hpp>
#include <ApplicationCommon.hpp>
#include <apps-common/BasePresenter.hpp>
#include <memory>
#include <utility>

namespace app::popup
{
    class AlarmPopupPresenter;
    class AlarmPopupContract
    {
      public:
        class Presenter;

        class AlarmModel : public AsyncCallbackReceiver
        {
            friend Presenter;
            friend AlarmPopupPresenter;

          private:
            void set(std::shared_ptr<AlarmEventRecord> record);
            void setSnoozed(std::vector<SingleEventRecord> snoozed);
            void reset();
            template <typename requestType, typename responseType>
            void snoozeAlarm();
            template <typename requestType, typename responseType>
            void stopAlarm();
            void setRefreshWindowCallback(std::function<void()> callback);
            void processIfSnoozed();

            std::shared_ptr<AlarmEventRecord> record = nullptr;
            std::vector<SingleEventRecord> snoozedRecord;
            Presenter *presenter                        = nullptr;
            std::function<void()> refreshWindowCallback = nullptr;
            std::string snoozedTill                     = std::string{};
            bool isSnoozedAlarm                         = false;
            ApplicationCommon *app                      = nullptr;

          public:
            explicit AlarmModel(ApplicationCommon *app);
            void attach(Presenter *p);
        };

        class View
        {
          private:
            std::shared_ptr<Presenter> presenter;

          protected:
            std::shared_ptr<Presenter> getPresenter()
            {
                return presenter;
            }

          public:
            explicit View(std::shared_ptr<Presenter> presenter) : presenter(std::move(presenter))
            {}
            virtual ~View() noexcept        = default;
        };

        class Presenter : public BasePresenter<AlarmPopupContract::View>
        {
          private:
            std::shared_ptr<AlarmModel> model;
            ApplicationCommon *app = nullptr;

          protected:
            std::shared_ptr<AlarmModel> &getModel()
            {
                return model;
            }

            ApplicationCommon *getApp()
            {
                return app;
            }

          public:
            explicit Presenter(ApplicationCommon *app) : model(std::make_shared<AlarmModel>(app)), app(app)
            {
                model->attach(this);
            }
            virtual ~Presenter() noexcept = default;
            virtual void setModel(std::shared_ptr<AlarmEventRecord> r)
            {
                this->model->set(std::move(r));
            }
            virtual void setSnoozedRecord(std::vector<SingleEventRecord> snoozed)
            {
                this->model->setSnoozed(snoozed);
            }
            virtual void setRefreshWindowCallback(std::function<void()> callback)
            {
                this->model->setRefreshWindowCallback(std::move(callback));
            }
            virtual void processIfSnoozed()
            {
                this->model->processIfSnoozed();
            }
            virtual void resetModel()
            {
                this->model->reset();
            }

            virtual std::uint32_t getSnoozeTime() = 0;
            virtual bool isSnoozeAble()           = 0;
            virtual void snoozeHit()              = 0;
            virtual void stopAlarm()              = 0;
            virtual void skipToNextSnooze()       = 0;
            virtual void handleAlarmSnoozed()     = 0;
            virtual void handleAlarmTurnedOff()   = 0;
            virtual bool isSnoozed()              = 0;
            virtual bool haveSnoozedSkip()        = 0;
            virtual std::string startedAt()       = 0;
            virtual std::string snoozedTill()     = 0;
        };
    };

    class AlarmPopupPresenter : public AlarmPopupContract::Presenter
    {
      public:
        explicit AlarmPopupPresenter(ApplicationCommon *app);

      private:
        bool noteChanged = false;
        sys::TimerHandle timerHandle{};
        /// returns how many seconds we can snooze the alarm
        virtual std::uint32_t getSnoozeTime() override;
        /// returns if alarm has snooze time available
        /// determines if we can snooze the alarm at all
        bool isSnoozeAble() override;
        /// action on when user hit snooze button
        void snoozeHit() override;
        /// action to stop alarm ringing
        void stopAlarm() override;
        /// action to skip to next snooze
        void skipToNextSnooze() override;
        /// action to call when we processed snoozing the alarm
        void handleAlarmSnoozed() override;
        /// action to call when we turned off alarm successfully
        void handleAlarmTurnedOff() override;
        /// option to determine if alarm was snoozed previously and is recurring
        bool isSnoozed() override;
        /// check if curent view should have option to skip alarm notification
        bool haveSnoozedSkip() override;
        /// value for UI to show time when alarm started
        std::string startedAt() override;
        /// value for UI to show if alarm `isSnoozed` till what hour it's snoozed exactly
        std::string snoozedTill() override;
    };
} // namespace app::popup