~aleteoryx/muditaos

muditaos/module-apps/application-messages/models/ThreadsModel.cpp -rw-r--r-- 3.4 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
// 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 "MessagesStyle.hpp"
#include "SMSdata.hpp"
#include "ThreadItem.hpp"
#include "ThreadsModel.hpp"
#include "ThreadWindowOptions.hpp"

#include <InputEvent.hpp>
#include <ListView.hpp>
#include <log/log.hpp>
#include <module-db/queries/messages/threads/QueryThreadsGet.hpp>
#include <module-db/queries/messages/threads/QueryThreadsGetForList.hpp>
#include <OptionsWindow.hpp>
#include <OptionWindow.hpp>
#include <service-db/DBServiceAPI.hpp>

ThreadsModel::ThreadsModel(app::ApplicationCommon *app) : BaseThreadsRecordModel(app), app::AsyncCallbackReceiver{app}
{}

auto ThreadsModel::getMinimalItemSpaceRequired() const -> unsigned int
{
    return style::messages::threadItem::sms_thread_item_h;
}

auto ThreadsModel::getItem(gui::Order order) -> gui::ListItem *
{
    std::shared_ptr<ThreadListStruct> threadStruct = getRecord(order);

    if (!threadStruct) {
        return nullptr;
    }

    auto item = gui::ThreadItem::makeThreadItem(threadStruct);

    item->activatedCallback = [this, threadStruct](gui::Item &item) {
        if (application) {
            const auto &threadItem = static_cast<gui::ThreadItem &>(item);
            application->switchWindow(
                gui::name::window::thread_view,
                std::make_unique<SMSThreadData>(threadStruct->thread, threadItem.getThreadName()));
        }
        else {
            LOG_ERROR("No application!");
        }
        return true;
    };

    item->inputCallback = [this, item](gui::Item &, const gui::InputEvent &event) {
        if (!event.isShortRelease()) {
            return false;
        }
        if (event.is(gui::KeyCode::KEY_LF)) {
            application->switchWindow(gui::name::window::thread_options,
                                      std::make_unique<SMSThreadData>(item->getThreadItem()));
        }
        return false;
    };
    return item;
}

void ThreadsModel::requestRecords(uint32_t offset, uint32_t limit)
{
    auto query = std::make_unique<db::query::ThreadsGetForList>(offset, limit);
    auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::SMSThread);
    task->setCallback([this](auto response) { return handleQueryResponse(response); });
    task->execute(getApplication(), this);
}

auto ThreadsModel::handleQueryResponse(db::QueryResult *queryResult) -> bool
{
    auto msgResponse = dynamic_cast<db::query::ThreadsGetForListResults *>(queryResult);
    assert(msgResponse != nullptr);

    // If list record count has changed we need to rebuild list.
    if (recordsCount != (msgResponse->getCount())) {
        recordsCount = msgResponse->getCount();
        list->reSendLastRebuildRequest();
        return false;
    }

    auto threads  = msgResponse->getResults();
    auto contacts = msgResponse->getContacts();
    auto numbers  = msgResponse->getNumbers();

    std::vector<ThreadListStruct> records;

    assert(threads.size() == contacts.size() && threads.size() == numbers.size());

    for (unsigned int i = 0; i < threads.size(); i++) {
        records.emplace_back(std::make_shared<ThreadRecord>(threads[i]),
                             std::make_shared<ContactRecord>(contacts[i]),
                             std::make_shared<utils::PhoneNumber::View>(numbers[i]));
    }

    return this->updateRecords(std::move(records));
}