~aleteoryx/muditaos

ref: 521cadc0bca7d9e984018f472487114b8be2554d muditaos/module-apps/application-messages/models/ThreadsModel.cpp -rw-r--r-- 2.5 KiB
521cadc0 — kkleczkowski [EGD-3768] Fixed items sizes and positions in Date Time window. (#728) 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
#include <module-services/service-db/messages/DBThreadMessage.hpp>
#include <module-services/service-db/api/DBServiceAPI.hpp>
#include "ThreadsModel.hpp"
#include "OptionWindow.hpp"
#include "application-messages/windows/ThreadWindowOptions.hpp"
#include "application-messages/widgets/ThreadItem.hpp"
#include "application-messages/data/SMSdata.hpp"

ThreadsModel::ThreadsModel(app::Application *app) : BaseThreadsRecordModel(app)
{}

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

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

    if (thread.get() == nullptr) {
        return nullptr;
    }

    auto item = gui::ThreadItem::makeThreadItem(this, thread);
    item->setThreadItem(thread);
    item->activatedCallback = [=](gui::Item &item) {
        LOG_INFO("ThreadItem ActivatedCallback");
        if (application) {
            application->switchWindow(gui::name::window::thread_view, std::make_unique<SMSThreadData>(thread));
        }
        else {
            LOG_ERROR("No application!");
        }
        return true;
    };

    item->inputCallback = [this, item](gui::Item &, const gui::InputEvent &event) {
        auto app = dynamic_cast<app::ApplicationMessages *>(application);
        assert(app);
        if (event.state != gui::InputEvent::State::keyReleasedShort) {
            return false;
        }
        if (event.keyCode == gui::KeyCode::KEY_LF) {
            app->windowOptions->clearOptions();
            app->windowOptions->addOptions(threadWindowOptions(app, item->getThreadItem().get()));
            app->switchWindow(app->windowOptions->getName(), nullptr);
        }
        return false;
    };
    return item;
}

void ThreadsModel::requestRecords(uint32_t offset, uint32_t limit)
{
    auto query = std::make_unique<db::query::SMSThreadsGet>(offset, limit);
    query->setQueryListener(
        db::QueryCallback::fromFunction([this](auto response) { return handleQueryResponse(response); }));
    DBServiceAPI::GetQuery(getApplication(), db::Interface::Name::SMSThread, std::move(query));
}

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

    auto records_data = msgResponse->getResults();
    auto records      = std::make_unique<std::vector<ThreadRecord>>(records_data.begin(), records_data.end());
    return this->updateRecords(std::move(records));
}