~aleteoryx/muditaos

ref: 78a499e3bf220a8168d3600e3368669032b8a6a3 muditaos/module-apps/application-messages/windows/SMSTemplatesWindow.cpp -rw-r--r-- 4.7 KiB
78a499e3 — Lefucjusz [MOS-29] Fix invalid screen after rejecting with template 3 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
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApplicationMessages.hpp"
#include "MessagesStyle.hpp"
#include "SMSTemplateItem.hpp"
#include "SMSTemplatesWindow.hpp"

#include <i18n/i18n.hpp>
#include <log/log.hpp>
#include <service-appmgr/Controller.hpp>
#include <Style.hpp>

#include <cassert>
#include <memory>

namespace gui
{
    SMSTemplatesWindow::SMSTemplatesWindow(app::ApplicationCommon *app)
        : AppWindow(app, name::window::sms_templates), smsTemplateModel{std::make_shared<SMSTemplateModel>(app)}
    {
        buildInterface();
    }

    SMSTemplatesWindow::~SMSTemplatesWindow()
    {
        destroyInterface();
    }

    void SMSTemplatesWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
    }

    void SMSTemplatesWindow::buildInterface()
    {
        AppWindow::buildInterface();

        setTitle(utils::translate("app_messages_templates"));

        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::use));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

        namespace style = style::messages::templates::list;

        list = new gui::ListView(
            this, style::x, style::y, style::w, style::h, smsTemplateModel, listview::ScrollBarType::Fixed);
        list->setBoundaries(Boundaries::Continuous);

        using namespace ::style;
        emptyListIcon = new Icon(this,
                                 0,
                                 window::default_vertical_pos,
                                 window_width,
                                 window_height - window::default_vertical_pos - ::style::nav_bar::height,
                                 "info_128px_W_G",
                                 utils::translate("app_messages_no_templates"));
        emptyListIcon->setVisible(false);

        setFocusItem(list);
    }

    void SMSTemplatesWindow::destroyInterface()
    {
        erase();
    }

    void SMSTemplatesWindow::smsTemplateRequestHandler(const SMSTemplateRequest *const switchData)
    {
        auto app = dynamic_cast<app::ApplicationMessages *>(application);
        assert(app != nullptr);

        auto requestingWindow  = switchData->requestingWindow;
        app->templatesCallback = [=](std::shared_ptr<SMSTemplateRecord> templ) {
            LOG_DEBUG("SMS template id = %" PRIu32 "chosen", templ->ID);
            std::unique_ptr<gui::SwitchData> data =
                std::make_unique<SMSTextData>(templ->text, SMSTextData::Concatenate::True);
            application->switchWindow(requestingWindow, std::move(data));
            return true;
        };
    }

    void SMSTemplatesWindow::smsSendTemplateRequestHandler(const SMSSendTemplateRequest *const switchData)
    {
        preventsAutoLock = switchData->isAutoLockPrevented();
        auto app         = dynamic_cast<app::ApplicationMessages *>(application);
        assert(app != nullptr);

        app->templatesCallback = [=](std::shared_ptr<SMSTemplateRecord> templ) {
            LOG_DEBUG("SMS template id = %" PRIu32 "sent", templ->ID);
            app::manager::Controller::switchBack(
                app,
                std::make_unique<app::manager::SwitchBackRequest>(application->GetName(),
                                                                  std::make_unique<SMSTemplateSent>(templ->text)));
            app->popCurrentWindow();
            return true;
        };
    }

    void SMSTemplatesWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        preventsAutoLock = false;
        if (mode == ShowMode::GUI_SHOW_INIT) {
            list->rebuildList();
        }

        navBar->setActive(nav_bar::Side::Center, !list->isEmpty());
        emptyListIcon->setVisible(list->isEmpty());

        if (auto switchData = dynamic_cast<SMSTemplateRequest *>(data)) {
            smsTemplateRequestHandler(switchData);
        }

        if (auto switchData = dynamic_cast<SMSSendTemplateRequest *>(data); switchData != nullptr) {
            ignoreWindowsOfThisAppOnSwitchBack = data->ignoreCurrentWindowOnStack;
            appNameToSwitchBack                = switchData->getNameOfSenderApp();
            smsSendTemplateRequestHandler(switchData);
        }
    }

    bool SMSTemplatesWindow::onInput(const InputEvent &inputEvent)
    {
        if (!inputEvent.isShortRelease(KeyCode::KEY_RF) || !ignoreWindowsOfThisAppOnSwitchBack ||
            !appNameToSwitchBack.has_value()) {
            return AppWindow::onInput(inputEvent);
        }

        return app::manager::Controller::switchBack(
            application, std::make_unique<app::manager::SwitchBackRequest>(appNameToSwitchBack.value(), nullptr, true));
    }
} /* namespace gui */