~aleteoryx/muditaos

ref: 2cd0e4721405c0bf263f311f27d38aadae6ac89f muditaos/module-apps/application-desktop/models/ActiveNotificationsListPresenter.cpp -rw-r--r-- 12.4 KiB
2cd0e472 — Lefucjusz [BH-000] Update Harmony 2.10.0 changelog 2 months 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "ActiveNotificationsListPresenter.hpp"
#include "ApplicationDesktop.hpp"

#include <application-call/data/CallSwitchData.hpp>
#include <application-messages/Constants.hpp>
#include <application-messages/data/SMSdata.hpp>
#include <gsl/assert>
#include <module-db/queries/notifications/QueryNotificationsClear.hpp>
#include <queries/messages/threads/QueryThreadGetByNumber.hpp>
#include <service-appmgr/ServiceApplicationManagerName.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-appmgr/messages/SwitchRequest.hpp>
#include <service-time/include/service-time/AlarmServiceAPI.hpp>
#include <service-time/AlarmMessage.hpp>
#include <service-time/ServiceTimeName.hpp>
#include <apps-common/popups/data/AlarmPopupRequestParams.hpp>

using namespace gui;

namespace
{
    using Notification = const notifications::NotificationWithContact *;
    void setSMSFocusChangedCallback(NotificationListItem *item, ActiveNotificationsListPresenter *presenter)
    {
        item->focusChangedCallback = [presenter](gui::Item &_item) {
            if (_item.focus) {
                presenter->setParentNavBar(
                    {}, utils::translate("app_desktop_show"), utils::translate("app_desktop_clear"));
                return true;
            }
            return false;
        };
    }

    auto createSMSActivatedCallback(app::ApplicationCommon *app)
    {
        return [app]([[maybe_unused]] gui::Item &_item) {
            return app::manager::Controller::sendAction(
                app, app::manager::actions::Launch, std::make_unique<app::ApplicationLaunchData>(app::name_messages));
        };
    }

    auto createSMSActivatedCallback(app::ApplicationCommon *app, const ContactRecord &record)
    {
        Expects(not record.numbers.empty());
        return [app, number = record.numbers[0].number]([[maybe_unused]] gui::Item &_item) {
            auto query = std::make_unique<db::query::ThreadGetByNumber>(number);
            auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::SMSThread);

            auto queryCallback = [app](db::QueryResult *msg) -> bool {
                Expects(typeid(*msg) == typeid(db::query::ThreadGetByNumberResult));
                auto result  = static_cast<db::query::ThreadGetByNumberResult *>(msg);
                auto data    = std::make_unique<SMSThreadData>(std::make_shared<ThreadRecord>(result->getThread()));
                auto request = std::make_shared<app::manager::SwitchRequest>(
                    app->GetName(), app::name_messages, gui::name::window::thread_view, std::move(data));
                return app->bus.sendUnicast(std::move(request), service::name::appmgr);
            };
            task->setCallback(std::move(queryCallback));
            task->execute(app, static_cast<app::ApplicationDesktop *>(app));
            return true;
        };
    }

    void setSMSActivatedCallback(NotificationListItem *item, Notification provider, app::ApplicationCommon *app)
    {
        if (provider->hasRecord() && not provider->getRecord().numbers.empty()) {
            item->activatedCallback = createSMSActivatedCallback(app, provider->getRecord());
        }
        else {
            item->activatedCallback = createSMSActivatedCallback(app);
        }
    }

    void clearSMSNotifications(app::ApplicationCommon *app)
    {
        DBServiceAPI::GetQuery(app,
                               db::Interface::Name::Notifications,
                               std::make_unique<db::query::notifications::Clear>(NotificationsRecord::Key::Sms));
    }

    void setSMSOnInputCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->inputCallback = [app]([[maybe_unused]] Item &item, const InputEvent &inputEvent) {
            if (inputEvent.isShortRelease(KeyCode::KEY_RF)) {
                clearSMSNotifications(app);
                return true;
            }
            return false;
        };
    }

    void setSMSDismissCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->dismissCallback = [app]() { clearSMSNotifications(app); };
    }

    void setCallFocusChangedCallback(NotificationListItem *item,
                                     Notification provider,
                                     ActiveNotificationsListPresenter *presenter)
    {
        item->focusChangedCallback = [presenter, canCall = provider->hasRecord()](gui::Item &_item) {
            if (_item.focus) {
                UTF8 navBarLeftText = canCall ? UTF8{utils::translate("common_call")} : UTF8{};
                presenter->setParentNavBar(
                    navBarLeftText, utils::translate("app_desktop_show"), utils::translate("app_desktop_clear"));
            }
            return true;
        };
    }

    void setCallActivatedCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->activatedCallback = [app]([[maybe_unused]] gui::Item &_item) {
            return app::manager::Controller::sendAction(app, app::manager::actions::ShowCallLog);
        };
    }

    auto createCallOnRightFunctionalCallback(app::ApplicationCommon *app) -> std::function<void()>
    {
        return [app]() {
            DBServiceAPI::GetQuery(app,
                                   db::Interface::Name::Notifications,
                                   std::make_unique<db::query::notifications::Clear>(NotificationsRecord::Key::Calls));
        };
    }
    auto createCallOnLeftFunctionalCallback(app::ApplicationCommon *app, Notification provider) -> std::function<void()>
    {
        if (!provider->hasRecord()) {
            return nullptr;
        }
        if (const auto &record = provider->getRecord(); !record.numbers.empty()) {
            return [app, number = record.numbers[0].number]() {
                app::manager::Controller::sendAction(
                    app, app::manager::actions::Call, std::make_unique<app::ExecuteCallData>(number));
            };
        }
        return nullptr;
    }

    void setCallOnInputCallback(NotificationListItem *item, Notification provider, app::ApplicationCommon *app)
    {
        auto onRightFunctionalKeyCallback = createCallOnRightFunctionalCallback(app);
        auto onLeftFunctionalKeyCallback  = createCallOnLeftFunctionalCallback(app, provider);

        item->inputCallback = [keyRightFunctionalCb = std::move(onRightFunctionalKeyCallback),
                               keyLeftFunctionalCb  = std::move(onLeftFunctionalKeyCallback)](
                                  [[maybe_unused]] Item &item, const InputEvent &inputEvent) {
            if (inputEvent.isShortRelease()) {
                if (inputEvent.is(KeyCode::KEY_RF) && keyRightFunctionalCb != nullptr) {
                    keyRightFunctionalCb();
                    return true;
                }
                else if (inputEvent.is(KeyCode::KEY_LF) && keyLeftFunctionalCb != nullptr) {
                    keyLeftFunctionalCb();
                    return true;
                }
            }
            return false;
        };
    }

    void setCallDismissCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->dismissCallback = createCallOnRightFunctionalCallback(app);
    }

    void setTetheringActivatedCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->activatedCallback = [app]([[maybe_unused]] gui::Item &_item) {
            app->switchWindow(gui::popup::window::tethering_off_window);
            return true;
        };
    }

    void setTetheringFocusChangedCallback(NotificationListItem *item, ActiveNotificationsListPresenter *presenter)
    {
        item->focusChangedCallback = [presenter](gui::Item &_item) {
            if (_item.focus) {
                presenter->setParentNavBar({}, utils::translate("common_disconnect"), {});
                return true;
            }
            return false;
        };
    }

    void setSnoozeFocusChangedCallback(NotificationListItem *item, ActiveNotificationsListPresenter *presenter)
    {
        item->focusChangedCallback = [presenter](gui::Item &_item) {
            if (_item.focus) {
                presenter->setParentNavBar({}, utils::translate("app_desktop_show"), utils::translate("common_stop"));
            }
            return true;
        };
    }

    void setSnoozeActivatedCallback(NotificationListItem *item,
                                    ActiveNotificationsListPresenter *presenter,
                                    app::ApplicationCommon *app)
    {
        item->activatedCallback = [presenter, app]([[maybe_unused]] gui::Item &_item) {
            auto request = std::make_unique<alarms::GetSnoozedAlarmsRequestMessage>();
            auto task    = app::AsyncRequest::createFromMessage(std::move(request), service::name::service_time);
            auto cb      = [&](auto response) {
                auto result = dynamic_cast<alarms::GetSnoozedAlarmsResponseMessage *>(response);
                assert(result);
                if (!result || result->snoozedAlarms.empty()) {
                    return false;
                }
                app::manager::Controller::sendAction(
                    app,
                    app::manager::actions::ShowPopup,
                    std::make_unique<gui::AlarmPopupRequestParams>(AlarmPopupType::SnoozeCheck,
                                                                   std::move(result->snoozedAlarms)));
                return true;
            };
            task->execute(app, presenter, cb);
            return true;
        };
    }

    void setSnoozeOnInputCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->inputCallback = [app]([[maybe_unused]] Item &item, const InputEvent &inputEvent) {
            if (inputEvent.isShortRelease() && inputEvent.is(KeyCode::KEY_RF)) {
                alarms::AlarmServiceAPI::requestStopAllSnoozedAlarms(app);
                return true;
            }
            return false;
        };
    }

    void setSnoozeDismissCallback(NotificationListItem *item, app::ApplicationCommon *app)
    {
        item->dismissCallback = [app]() { alarms::AlarmServiceAPI::requestStopAllSnoozedAlarms(app); };
    }
} // namespace

ActiveNotificationsListPresenter::ActiveNotificationsListPresenter(AppWindow *parent)
    : AsyncCallbackReceiver(parent->getApplication()), parent(parent)
{}

void ActiveNotificationsListPresenter::setParentNavBar(const UTF8 &left, const UTF8 &center, const UTF8 &right)
{
    parent->setNavBarText(left, nav_bar::Side::Left);
    parent->setNavBarText(center, nav_bar::Side::Center);
    parent->setNavBarText(right, nav_bar::Side::Right);
}

auto ActiveNotificationsListPresenter::create(const notifications::NotSeenSMSNotification *notification)
    -> NotificationListItem *
{
    auto item = NotificationsListPresenter::create(notification);
    setSMSFocusChangedCallback(item, this);
    setSMSActivatedCallback(item, notification, parent->getApplication());
    setSMSOnInputCallback(item, parent->getApplication());
    setSMSDismissCallback(item, parent->getApplication());
    item->setDismissible(true);
    return item;
}
auto ActiveNotificationsListPresenter::create(const notifications::NotSeenCallNotification *notification)
    -> NotificationListItem *
{
    auto item = NotificationsListPresenter::create(notification);
    setCallFocusChangedCallback(item, notification, this);
    setCallActivatedCallback(item, parent->getApplication());
    setCallOnInputCallback(item, notification, parent->getApplication());
    setCallDismissCallback(item, parent->getApplication());
    item->setDismissible(true);
    return item;
}

auto ActiveNotificationsListPresenter::create(const notifications::TetheringNotification *notification)
    -> NotificationListItem *
{
    auto item = NotificationsListPresenter::create(notification);
    setTetheringActivatedCallback(item, parent->getApplication());
    setTetheringFocusChangedCallback(item, this);
    return item;
}

auto ActiveNotificationsListPresenter::create(const notifications::AlarmSnoozeNotification *notification)
    -> NotificationListItem *
{
    auto item = NotificationsListPresenter::create(notification);
    setSnoozeFocusChangedCallback(item, this);
    setSnoozeActivatedCallback(item, this, parent->getApplication());
    setSnoozeOnInputCallback(item, parent->getApplication());
    setSnoozeDismissCallback(item, parent->getApplication());
    item->setDismissible(false);
    return item;
}