~aleteoryx/muditaos

ref: 1b00c14ead8d8ecdf7723989e2703bac2ee37695 muditaos/module-apps/application-messages/windows/NewMessage.hpp -rw-r--r-- 3.1 KiB
1b00c14e — Michał Kamoń [EGD-4190] NewMessage fixes (#975) 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <chrono>
#include <string>

#include <AppWindow.hpp>
#include <PhoneNumber.hpp>
#include <widgets/Text.hpp>
#include <module-db/Interface/SMSRecord.hpp>
#include <module-db/Interface/ContactRecord.hpp>

namespace gui
{
    class NewMessageWindow : public AppWindow
    {
      public:
        explicit NewMessageWindow(app::Application *app);

        bool onInput(const InputEvent &inputEvent) override;
        void onBeforeShow(ShowMode mode, SwitchData *data) override;
        void buildInterface() override;
        void onClose() override;

      private:
        bool selectContact();
        bool sendSms();
        bool switchToThreadWindow(const utils::PhoneNumber::View &number);
        void updateBottomBar();
        auto getPhoneNumber() const -> utils::PhoneNumber;
        auto handleMessageText() -> bool;
        auto addDraft(const ContactRecord &contactRecord) -> bool;
        auto addDraft(const utils::PhoneNumber &number) -> bool;
        auto addDraftToExistingThread(unsigned int threadId, const utils::PhoneNumber::View &number, const UTF8 &text)
            -> bool;
        void storeMessageDraft(const utils::PhoneNumber::View &number, const UTF8 &text);
        void storeMessageDraft(SMSRecord &sms, const UTF8 &text);
        static inline constexpr std::chrono::milliseconds getThreadTimeout{1000};
        gui::Text *recipient = nullptr;
        gui::Text *message   = nullptr;
        gui::VBox *body      = nullptr;
        std::shared_ptr<ContactRecord> contact;
        utils::PhoneNumber::View phoneNumber;

        /// MessageMemento class provides buffer-like functionality for message field of NewMessageWindow.
        /// MessageMemento shall be used whenever there is a need for temporary window switch in normal (uninterrupted)
        /// flow of new message preparation, such as using options or recipient selection.
        class MessageMemento : public Item
        {
            gui::Text *state = nullptr;

          public:
            /// sets memento and leaves the origin pointing to default-constructed object
            void setState(gui::Text *&_state)
            {
                assert(_state);
                LOG_DEBUG("setting memento for NewMessageWindow");
                if (state == nullptr) {
                    state         = new gui::Text();
                    state->parent = this;
                }
                std::swap(state->parent, _state->parent);
                std::swap(state, _state);
            }
            void getState(gui::Text *&_state)
            {
                if (state == nullptr) {
                    return;
                }
                assert(_state);
                LOG_DEBUG("getting memento for NewMessageWindow");
                std::swap(state->parent, _state->parent);
                std::swap(state, _state);
                delete state;
                state = nullptr;
            }
        };
        static std::unique_ptr<MessageMemento> memento;
    };
} /* namespace gui */