~aleteoryx/muditaos

ref: 5cfd688f510cc32a965d5cb1f052d7025d1d84a3 muditaos/module-apps/application-messages/widgets/SMSInputWidget.cpp -rw-r--r-- 6.7 KiB
5cfd688f — Lefucjusz [MOS-1043] Enable VoLTE only for Poland and US 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
// 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 <AppWindow.hpp>
#include <Font.hpp>
#include <i18n/i18n.hpp>
#include <service-cellular/service-cellular/MessageConstants.hpp>
#include <SMSInputWidget.hpp>
#include <Style.hpp>
#include <parsers/TextParse.hpp>

#include <algorithm>
#include <utility>

namespace gui
{

    SMSInputWidget::SMSInputWidget(app::ApplicationCommon *application) : application(application)
    {
        setMinimumSize(style::window::default_body_width, style::messages::smsInput::min_h);
        setMargins(Margins(
            style::messages::smsInput::new_sms_left_margin, style::messages::smsInput::new_sms_vertical_spacer, 0, 0));
        setEdges(gui::RectangleEdge::None);

        body = new HBox(this, 0, 0, 0, 0);
        body->setEdges(RectangleEdge::Bottom);
        body->setMaximumSize(style::window::default_body_width, style::messages::smsInput::max_input_h);

        deleteByList = false;
        inputText    = new gui::Text(body, 0, 0, 0, 0, ExpandMode::Up);
        inputText->setMaximumSize(style::messages::smsInput::default_input_w, style::messages::smsInput::max_input_h);
        inputText->setMinimumSize(style::messages::smsInput::default_input_w,
                                  style::messages::smsInput::default_input_h);
        inputText->setTextLimitType(gui::TextLimitType::MaxSignsCount, msgConstants::maxConcatenatedLen);
        inputText->setFont(style::window::font::medium);
        inputText->setPadding(Padding(0, 0, 0, style::messages::smsInput::bottom_padding));
        inputText->setPenFocusWidth(style::window::default_border_focus_w);
        inputText->setPenWidth(style::window::default_border_focus_w);
        inputText->setEdges(gui::RectangleEdge::None);

        replyImage = new Image(body, 0, 0, "messages_reply_32px_W_M");
        replyImage->setAlignment(Alignment(gui::Alignment::Vertical::Bottom));
        replyImage->setMargins(Margins(0, 0, 0, style::messages::smsInput::reply_bottom_margin));
        replyImage->activeItem = false;

        inputCallback = [&]([[maybe_unused]] Item &item, const InputEvent &event) { return inputText->onInput(event); };

        focusChangedCallback = [this]([[maybe_unused]] Item &item) {
            setFocusItem(focus ? body : nullptr);
            return true;
        };

        inputText->setInputMode(new InputMode(
            {InputMode::Abc, InputMode::ABC, InputMode::abc, InputMode::digit},
            [=](const UTF8 &Text) { application->getCurrentWindow()->navBarTemporaryMode(Text); },
            [=]() { application->getCurrentWindow()->navBarRestoreFromTemporaryMode(); },
            [=]() { application->getCurrentWindow()->selectSpecialCharacter(); },
            [=](std::function<void()> restoreFunction) {
                application->getCurrentWindow()->startInputModeRestoreTimer(std::move(restoreFunction));
            }));

        inputText->inputCallback = [this, application]([[maybe_unused]] Item &, const InputEvent &event) {
            if (event.isShortRelease(KeyCode::KEY_LF)) {
                auto app = dynamic_cast<app::ApplicationMessages *>(application);
                assert(app != nullptr);
                return app->newMessageOptions(application->getCurrentWindow()->getName(), inputText);
            }
            return false;
        };

        inputText->focusChangedCallback = [this, application]([[maybe_unused]] Item &) -> bool {
            assert(body != nullptr);
            assert(application != nullptr);

            const auto threadViewWindow = application->getWindow(gui::name::window::thread_view);

            if (inputText->focus) {
                inputText->setCursorStartPosition(CursorStartPosition::DocumentEnd);
                threadViewWindow->setNavBarText(utils::translate(utils::translate(style::strings::common::send)),
                                                nav_bar::Side::Center);
            }
            else {
                threadViewWindow->setNavBarText(utils::translate(style::strings::common::reply), nav_bar::Side::Center);
            }

            return true;
        };

        dimensionChangedCallback = [&](gui::Item &, const BoundingBox &newDim) -> bool {
            if (newDim.w == style::listview::item_width_with_scroll - style::messages::smsInput::new_sms_left_margin) {
                inputText->setMinimumWidth(style::messages::smsInput::default_input_w);
                inputText->setMaximumWidth(style::messages::smsInput::default_input_w);
            }
            else {
                inputText->setMinimumWidth(style::messages::smsInput::default_input_w +
                                           style::listview::scroll::item_margin);
                inputText->setMaximumWidth(style::messages::smsInput::default_input_w +
                                           style::listview::scroll::item_margin);
            }
            body->setArea({0, 0, newDim.w, newDim.h});
            return true;
        };
    }

    void SMSInputWidget::handleDraftMessage()
    {
        const auto &text = inputText->getText();
        text.empty() ? clearDraftMessage() : updateDraftMessage(text);
    }

    void SMSInputWidget::clearDraftMessage()
    {
        if (!draft.has_value()) {
            displayDraftMessage();
            return;
        }

        auto app = dynamic_cast<app::ApplicationMessages *>(application);
        assert(app != nullptr);
        if (const auto removed = app->removeDraft(draft.value()); removed) {
            draft = std::nullopt;
            displayDraftMessage();
        }
    }

    void SMSInputWidget::displayDraftMessage() const
    {
        if (draft.has_value()) {
            inputText->setText(draft->body);
        }
        else {
            inputText->clear();
        }
    }

    void SMSInputWidget::updateDraftMessage(const UTF8 &inputText)
    {
        auto app = dynamic_cast<app::ApplicationMessages *>(application);
        assert(app != nullptr);
        assert(number != nullptr);

        if (draft.has_value()) {
            app->updateDraft(draft.value(), inputText);
        }
        else {
            app->createDraft(*number, inputText, [this](const SMSRecord &record) { draft = record; });
        }
    }

    auto SMSInputWidget::handleRequestResize([[maybe_unused]] const Item *child, Length request_w, Length request_h)
        -> Size
    {
        request_h =
            std::clamp((Length)request_h, style::messages::smsInput::min_h, style::messages::smsInput::max_input_h);

        setMinimumHeight(request_h);
        if (parent != nullptr) {
            requestSize(request_w, request_h);
        }

        return Size(request_w, request_h);
    }

} /* namespace gui */