~aleteoryx/muditaos

muditaos/module-apps/apps-common/notifications/NotificationsModel.cpp -rw-r--r-- 3.2 KiB
a405cad6Aleteoryx trim readme 6 days 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
// 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 "NotificationsModel.hpp"
#include <ListView.hpp>

using namespace gui;

namespace
{
    bool hasTetheringNotification(app::manager::actions::NotificationsChangedParams *params)
    {
        const auto &notifications = params->getNotifications();
        const auto it = std::find_if(std::begin(notifications), std::end(notifications), [](const auto &notification) {
            return notification->getType() == notifications::NotificationType::Tethering;
        });
        return it != std::end(notifications);
    }

    bool hasPhoneLockTime(app::manager::actions::NotificationsChangedParams *params)
    {
        const auto &notifications = params->getNotifications();
        const auto it = std::find_if(std::begin(notifications), std::end(notifications), [](const auto &notification) {
            return notification->getType() == notifications::NotificationType::PhoneLock;
        });
        return it != std::end(notifications);
    }
} // namespace

NotificationsModel::NotificationsModel(std::shared_ptr<NotificationsPresenter> notificationsPresenter,
                                       NotificationsListPlacement listPlacement)
    : notificationsPresenter{std::move(notificationsPresenter)}, listPlacement{listPlacement}
{}

NotificationsModel::NotificationsModel(NotificationsListPlacement listPlacement) : listPlacement{listPlacement}
{}

void NotificationsModel::attachPresenter(std::shared_ptr<NotificationsPresenter> notificationsPresenter)
{
    this->notificationsPresenter = std::move(notificationsPresenter);
}

bool NotificationsModel::hasDismissibleNotification() const noexcept
{
    if (notificationsPresenter == nullptr) {
        LOG_ERROR("Presenter not attached!");
        return false;
    }
    return notificationsPresenter->hasDismissibleNotification();
}

void NotificationsModel::dismissAll()
{
    if (notificationsPresenter == nullptr) {
        LOG_ERROR("Presenter not attached!");
        return;
    }
    notificationsPresenter->dismissAll();
}

bool NotificationsModel::isEmpty() const noexcept
{
    if (notificationsPresenter == nullptr) {
        LOG_ERROR("Presenter not attached!");
        return false;
    }
    return notificationsPresenter->isEmpty();
}

void NotificationsModel::updateData(app::manager::actions::NotificationsChangedParams *params)
{
    if (notificationsPresenter == nullptr) {
        LOG_ERROR("Presenter not attached!");
        return;
    }
    if (params == nullptr) {
        LOG_ERROR("Params are not provided");
        return;
    }

    const auto showOnLocked =
        (listPlacement == NotificationsListPlacement::LockedScreen) && params->showNotificationsWhenLocked();
    phoneTimeLock = hasPhoneLockTime(params);
    tetheringOn   = hasTetheringNotification(params);
    const auto callAndSMSVisibility =
        ((listPlacement == NotificationsListPlacement::Desktop) || showOnLocked) && not tetheringOn;

    notificationsPresenter->updateData(params, callAndSMSVisibility);
}

bool NotificationsModel::isTetheringOn() const noexcept
{
    return tetheringOn;
}

bool NotificationsModel::isPhoneTimeLock() const noexcept
{
    return phoneTimeLock;
}