~aleteoryx/muditaos

ref: fe8671698432b6877da6b8e911b8df77fc439922 muditaos/module-apps/windows/Options.cpp -rw-r--r-- 5.6 KiB
fe867169 — Piotr Tanski [EGD-4153] Use actions instead explicit applications switch. (#1032) 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Options.hpp"
#include "Text.hpp"
#include "tools/Common.hpp"
#include <cassert>
#include <i18/i18.hpp>
#include <utility>
#include <FontManager.hpp>

#include <service-appmgr/Controller.hpp>
#include <application-call/data/CallSwitchData.hpp>
#include <module-apps/application-phonebook/data/PhonebookItemData.hpp>

namespace style::option
{
    const inline gui::Length text_left_padding = 10;
}

namespace gui::option
{
    /// builder for call option
    /// creates text with caller in bold
    class Call : public Base
    {
      private:
        app::Application *app = nullptr;
        ContactRecord contact;

      public:
        Call(app::Application *app, ContactRecord contact) : app(app), contact(std::move(contact))
        {}

        [[nodiscard]] auto build() const -> Item * override
        {
            auto *rect     = new gui::Rect(nullptr,
                                       style::window::default_left_margin,
                                       0,
                                       style::window_width - 2 * style::window::default_right_margin,
                                       style::window::label::big_h);
            auto font      = FontManager::getInstance().getFont(style::window::font::medium);
            auto font_bold = FontManager::getInstance().getFont(style::window::font::mediumbold);
            auto text      = new Text(nullptr, style::option::text_left_padding, 0, 0, 0);
            text->setMaximumSize(rect->getWidth(), rect->getHeight());
            text->setEditMode(EditMode::BROWSE);
            text->setText(std::make_unique<TextDocument>(std::list<TextBlock>(
                {{utils::localize.get("sms_call_text"), font}, {contact.getFormattedName(), font_bold}})));
            style::window::decorate(rect);
            auto l_app              = app;
            auto l_contact          = contact;
            rect->activatedCallback = [l_app, l_contact](gui::Item &item) {
                if (!l_contact.numbers.empty()) {
                    const auto &phoneNumber = l_contact.numbers.front().number;
                    return app::manager::Controller::sendAction(
                        l_app, app::manager::actions::Dial, std::make_unique<app::ExecuteCallData>(phoneNumber));
                }
                return false;
            };
            rect->addWidget(text);
            center(rect, text, Axis::Y);
            return rect;
        }
    };
} // namespace gui::option

namespace gui::options
{
    namespace
    {
        bool onContactOptionClick(app::Application *app,
                                  ContactOperation contactOperation,
                                  const ContactRecord &contactRecord)
        {
            auto data = std::make_unique<PhonebookItemData>(std::make_shared<ContactRecord>(contactRecord));

            switch (contactOperation) {
            case ContactOperation::Add: {
                data->ignoreCurrentWindowOnStack = true;
                return app::manager::Controller::sendAction(app,
                                                            app::manager::actions::AddContact,
                                                            std::move(data),
                                                            app::manager::OnSwitchBehaviour::RunInBackground);
            }
            case ContactOperation::Details: {
                return app::manager::Controller::sendAction(app,
                                                            app::manager::actions::ShowContactDetails,
                                                            std::move(data),
                                                            app::manager::OnSwitchBehaviour::RunInBackground);
            }
            case ContactOperation::Edit: {
                return app::manager::Controller::sendAction(app,
                                                            app::manager::actions::EditContact,
                                                            std::move(data),
                                                            app::manager::OnSwitchBehaviour::RunInBackground);
            }
            }
            LOG_ERROR("ContactOperation not supported %" PRIu32, static_cast<uint32_t>(contactOperation));
            return false;
        }
    } // namespace

    Option call(app::Application *app, const ContactRecord &contact)
    {
        assert(app != nullptr);
        return Option{std::make_unique<gui::option::Call>(app, contact)};
    }

    Option contact(app::Application *app,
                   ContactOperation contactOperation,
                   const ContactRecord &contactRec,
                   gui::Arrow arrow)
    {
        assert(app != nullptr);

        std::string optionName;
        switch (contactOperation) {
        case ContactOperation::Details:
            optionName = utils::localize.get("app_options_contact_details");
            break;
        case ContactOperation::Add:
            optionName = utils::localize.get("app_options_contact_add");
            break;
        case ContactOperation::Edit:
            optionName = utils::localize.get("app_options_contact_edit");
            break;
        default:
            optionName = utils::localize.get("app_options_invalid_option");
            LOG_WARN("ContactOperation %d not supported", static_cast<int>(contactOperation));
            break;
        }

        return Option{
            optionName, [=](gui::Item &) { return onContactOptionClick(app, contactOperation, contactRec); }, arrow};
    }
} // namespace gui::options