~aleteoryx/muditaos

muditaos/module-apps/application-messages/windows/MessagesMainWindow.cpp -rw-r--r-- 6.7 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
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
168
169
170
// 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 "ApplicationMessages.hpp"
#include "MessagesMainWindow.hpp"
#include "MessagesStyle.hpp"
#include "SearchStart.hpp"
#include "SMSdata.hpp"
#include "ThreadItem.hpp"

#include <application-phonebook/data/PhonebookItemData.hpp>
#include <header/AddElementAction.hpp>
#include <header/SearchAction.hpp>
#include <i18n/i18n.hpp>
#include <log/log.hpp>
#include <module-db/queries/messages/threads/QueryThreadGetByNumber.hpp>
#include <service-db/DBNotificationMessage.hpp>
#include <service-db/DBServiceAPI.hpp>
#include <Style.hpp>

#include <cassert>
#include <memory>

namespace gui
{
    MessagesMainWindow::MessagesMainWindow(app::ApplicationCommon *app)
        : AppWindow(app, gui::name::window::main_window), app::AsyncCallbackReceiver{app}
    {
        buildInterface();
    }

    void MessagesMainWindow::rebuild()
    {
        if (list == nullptr) {
            return;
        }
        list->rebuildList(gui::listview::RebuildType::InPlace);
    }

    void MessagesMainWindow::buildInterface()
    {
        namespace msgThreadStyle = style::messages::threads;

        AppWindow::buildInterface();

        threadsModel = std::make_shared<ThreadsModel>(application);

        list = new gui::ListView(this,
                                 msgThreadStyle::listPositionX,
                                 msgThreadStyle::ListPositionY,
                                 msgThreadStyle::listWidth,
                                 msgThreadStyle::listHeight,
                                 threadsModel,
                                 listview::ScrollBarType::Fixed);
        list->setBoundaries(Boundaries::Continuous);
        list->setScrollTopMargin(style::margins::small);

        navBar->setActive(nav_bar::Side::Left, true);
        navBar->setActive(nav_bar::Side::Center, true);
        navBar->setActive(nav_bar::Side::Right, true);
        navBar->setText(nav_bar::Side::Left, utils::translate(style::strings::common::options));
        navBar->setText(nav_bar::Side::Center, utils::translate(style::strings::common::open));
        navBar->setText(nav_bar::Side::Right, utils::translate(style::strings::common::back));

        setTitle(utils::translate("app_messages_title_main"));
        header->navigationIndicatorAdd(new gui::header::AddElementAction(), gui::header::BoxSelection::Left);
#if MESSAGE_TEXT_SEARCH == 1
        header->navigationIndicatorAdd(new gui::header::SearchAction(), gui::header::BoxSelection::Right);
#endif

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

        list->setVisible(true);
        list->focusChangedCallback = [this]([[maybe_unused]] gui::Item &item) {
            navBar->setActive(nav_bar::Side::Left, true);
            navBar->setActive(nav_bar::Side::Center, true);
            return true;
        };

        list->emptyListCallback = [this]() {
            emptyListIcon->setVisible(true);
            navBar->setActive(nav_bar::Side::Left, false);
            navBar->setActive(nav_bar::Side::Center, false);
            application->refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
        };

        list->notEmptyListCallback = [this]() {
            emptyListIcon->setVisible(false);
            navBar->setActive(nav_bar::Side::Left, true);
            navBar->setActive(nav_bar::Side::Center, true);
            application->refreshWindow(gui::RefreshModes::GUI_REFRESH_DEEP);
        };

        setFocusItem(list);
    }

    void MessagesMainWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        {
            auto pdata = dynamic_cast<PhonebookSearchRequest *>(data);
            if (pdata != nullptr) {
                using db::query::ThreadGetByNumber;
                using db::query::ThreadGetByNumberResult;
                auto primaryNumber = pdata->result->numbers[0].number;
                auto query         = std::make_unique<ThreadGetByNumber>(primaryNumber);
                auto task          = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Contact);
                task->setCallback([app = application](auto response) {
                    if (const auto result = dynamic_cast<ThreadGetByNumberResult *>(response); result != nullptr) {
                        if (auto thread = result->getThread(); thread.isValid()) {
                            app->switchWindow(gui::name::window::thread_view,
                                              std::make_unique<SMSThreadData>(std::make_unique<ThreadRecord>(thread)));
                            return true;
                        }
                    }
                    LOG_FATAL("No thread and thread not created!");
                    return false;
                });
                task->execute(application, this);
            }
        }

        list->rebuildList(listview::RebuildType::InPlace);
    }

    bool MessagesMainWindow::onInput(const InputEvent &inputEvent)
    {
        // check if any of the lower inheritance onInput methods catch the event
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }
        if (inputEvent.isShortRelease()) {
            switch (inputEvent.getKeyCode()) {
            case gui::KeyCode::KEY_LEFT:
                application->switchWindow(gui::name::window::new_sms, nullptr);
                return true;
#if MESSAGE_TEXT_SEARCH == 1
            case gui::KeyCode::KEY_RIGHT:
                application->switchWindow(gui::name::window::thread_sms_search, nullptr);
                return true;
#endif
            default:
                LOG_DEBUG("SMS main window not handled key: %s", c_str(inputEvent.getKeyCode()));
                break;
            }
        }
        return false;
    }

    bool MessagesMainWindow::onDatabaseMessage(sys::Message *msgl)
    {
        auto msgNotification = dynamic_cast<db::NotificationMessage *>(msgl);
        if (msgNotification != nullptr) {
            if (msgNotification->interface == db::Interface::Name::SMSThread ||
                msgNotification->interface == db::Interface::Name::SMS) {
                if (msgNotification->dataModified()) {
                    rebuild();
                    return true;
                }
            }
        }
        return false;
    }
} // namespace gui