~aleteoryx/muditaos

724505bb92c96a2ba42b2cbbeba410957a265cde — Piotr Tański 4 years ago 46d3588
[EGD-2653] Update list of call logs in chunks

Automatically update the list of call logs in chunks.
M module-apps/application-calllog/ApplicationCallLog.cpp => module-apps/application-calllog/ApplicationCallLog.cpp +14 -22
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "ApplicationCallLog.hpp"


@@ 40,36 40,28 @@ namespace app
    // Invoked upon receiving data message
    sys::MessagePointer ApplicationCallLog::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp)
    {

        auto retMsg = Application::DataReceivedHandler(msgl);
        // if message was handled by application's template there is no need to process further.
        if ((reinterpret_cast<sys::ResponseMessage *>(retMsg.get())->retCode == sys::ReturnCodes::Success)) {
        auto retMsg = Application::DataReceivedHandler(msgl);
        if (auto responseMsg = dynamic_cast<sys::ResponseMessage *>(retMsg.get());
            responseMsg != nullptr && responseMsg->retCode == sys::ReturnCodes::Success) {
            return retMsg;
        }

        auto handled = false;
        if (msgl->messageType == MessageType::DBServiceNotification) {
            for (auto &[name, window] : windowsStack.windows) {
                window->onDatabaseMessage(msgl);
            }
            return msgHandled();
        }

        // handle database response
        if (resp != nullptr) {
            handled = true;
            switch (resp->responseTo) {
            case MessageType::DBCalllogGetLimitOffset: {
                if (getCurrentWindow()->onDatabaseMessage(resp)) {
                    refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
                }
                break;
            }
            default:
                break;
            if (auto command = callbackStorage->getCallback(resp); command->execute()) {
                refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
                return msgHandled();
            }
        }

        if (handled) {
            return std::make_shared<sys::ResponseMessage>();
        }
        else {
            return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Unresolved);
        }
        return msgNotHandled();
    }

    // Invoked during initialization

M module-apps/application-calllog/CalllogModel.cpp => module-apps/application-calllog/CalllogModel.cpp +26 -11
@@ 2,32 2,50 @@
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "CalllogModel.hpp"

#include "InputEvent.hpp"
#include "ListView.hpp"
#include "data/CallLogInternals.hpp"
#include "data/CallLogSwitchData.hpp"
#include "widgets/CalllogItem.hpp"
#include <module-utils/time/DateAndTimeSettings.hpp>
#include <module-utils/Utils.hpp>

#include <service-db/DBServiceAPI.hpp>
#include <service-appmgr/Controller.hpp>
#include "application-call/data/CallSwitchData.hpp"
#include <module-db/queries/calllog/QueryCalllogGet.hpp>

using namespace calllog;

CalllogModel::CalllogModel(app::Application *app) : DatabaseModel(app)
CalllogModel::CalllogModel(app::Application *app) : DatabaseModel(app), app::AsyncCallbackReceiver(app)
{}

unsigned int CalllogModel::requestRecordsCount()
{
    recordsCount = DBServiceAPI::CalllogGetCount(application);
    return recordsCount;
}

void CalllogModel::requestRecords(const uint32_t offset, const uint32_t limit)
void CalllogModel::requestRecords(uint32_t offset, uint32_t limit)
{
    DBServiceAPI::CalllogGetLimitOffset(application, offset, limit);
    auto query = std::make_unique<db::query::CalllogGet>(limit, offset);
    auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Calllog);
    task->setCallback([this](auto response) {
        auto result = dynamic_cast<db::query::CalllogGetResult *>(response);
        if (result == nullptr) {
            return false;
        }
        return onCalllogRetrieved(result->getRecords(), result->getTotalCount());
    });
    task->execute(application, this);
}

bool CalllogModel::onCalllogRetrieved(const std::vector<CalllogRecord> &records, unsigned int repoCount)
{
    if (recordsCount != repoCount) {
        recordsCount = repoCount;
        list->reSendLastRebuildRequest();
        return false;
    }
    return updateRecords(records);
}

bool CalllogModel::updateRecords(std::vector<CalllogRecord> records)


@@ 42,27 60,24 @@ bool CalllogModel::updateRecords(std::vector<CalllogRecord> records)

    DatabaseModel::updateRecords(std::move(records));
    list->onProviderDataUpdate();

    return true;
}

unsigned int CalllogModel::getMinimalItemHeight() const
{

    return gui::clItemStyle::h;
}

gui::ListItem *CalllogModel::getItem(gui::Order order)
{

    std::shared_ptr<CalllogRecord> call = getRecord(order);
    if (call.get() == nullptr) {
    if (!call) {
        return nullptr;
    }

    auto item = new gui::CalllogItem(this, !(utils::dateAndTimeSettings.isTimeFormat12()));

    auto callCallback = [this, item](gui::Item &, const gui::InputEvent &event) {
    auto callCallback = [this, item](gui::Item & /*item*/, const gui::InputEvent &event) {
        if (event.state != gui::InputEvent::State::keyReleasedShort) {
            return false;
        }

M module-apps/application-calllog/CalllogModel.hpp => module-apps/application-calllog/CalllogModel.hpp +13 -16
@@ 1,32 1,29 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <vector>

#include "Interface/CalllogRecord.hpp"
#include "DatabaseModel.hpp"
#include "CalllogRecord.hpp"
#include "Application.hpp"
#include "ListItemProvider.hpp"
#include <service-db/Settings.hpp>

/*
 *
 */
class CalllogModel : public app::DatabaseModel<CalllogRecord>, public gui::ListItemProvider
class CalllogModel : public app::DatabaseModel<CalllogRecord>,
                     public gui::ListItemProvider,
                     public app::AsyncCallbackReceiver
{
  public:
    CalllogModel() = delete;
    CalllogModel(app::Application *app);
    explicit CalllogModel(app::Application *app);

    // virtual methods
    bool updateRecords(std::vector<CalllogRecord> records) override;
    void requestRecords(const uint32_t offset, const uint32_t limit) override;

    // virtual methods for ListViewProvider
    unsigned int getMinimalItemHeight() const override;
    gui::ListItem *getItem(gui::Order order) override;
    [[nodiscard]] unsigned int requestRecordsCount() override;
    [[nodiscard]] bool updateRecords(std::vector<CalllogRecord> records) override;
    void requestRecords(uint32_t offset, uint32_t limit) override;

    [[nodiscard]] unsigned int getMinimalItemHeight() const override;
    [[nodiscard]] gui::ListItem *getItem(gui::Order order) override;

  private:
    bool onCalllogRetrieved(const std::vector<CalllogRecord> &records, unsigned int repoCount);
};

M module-apps/application-calllog/windows/CallLogMainWindow.cpp => module-apps/application-calllog/windows/CallLogMainWindow.cpp +12 -6
@@ 18,6 18,7 @@
#include <cassert>
#include <functional>
#include <memory>
#include <module-services/service-db/service-db/DBNotificationMessage.hpp>

using namespace style;
using namespace callLogStyle;


@@ 34,8 35,10 @@ namespace gui

    void CallLogMainWindow::rebuild()
    {
        destroyInterface();
        buildInterface();
        if (list == nullptr) {
            return;
        }
        list->rebuildList(style::listview::RebuildType::InPlace);
    }

    void CallLogMainWindow::buildInterface()


@@ 74,11 77,14 @@ namespace gui
        app->setAllEntriesRead();
    }

    bool CallLogMainWindow::onDatabaseMessage(sys::Message *msgl)
    bool CallLogMainWindow::onDatabaseMessage(sys::Message *msg)
    {
        auto *msg = dynamic_cast<DBCalllogResponseMessage *>(msgl);
        if (msg != nullptr) {
            return calllogModel->updateRecords(std::move(*msg->records));
        auto notification = dynamic_cast<db::NotificationMessage *>(msg);
        if (notification != nullptr) {
            if (notification->interface == db::Interface::Name::Calllog && notification->dataModified()) {
                rebuild();
                return true;
            }
        }
        return false;
    }

M module-apps/application-calllog/windows/CallLogMainWindow.hpp => module-apps/application-calllog/windows/CallLogMainWindow.hpp +3 -1
@@ 24,7 24,7 @@ namespace gui
        gui::ListView *list                        = nullptr;

      public:
        CallLogMainWindow(app::Application *app);
        explicit CallLogMainWindow(app::Application *app);

        // virtual methods
        void onBeforeShow(ShowMode mode, SwitchData *data) override;


@@ 32,6 32,8 @@ namespace gui
        void rebuild() override;
        void buildInterface() override;
        void destroyInterface() override;

      private:
        bool onDatabaseMessage(sys::Message *msg) override;
    };


M module-db/Interface/CalllogRecord.cpp => module-db/Interface/CalllogRecord.cpp +2 -1
@@ 274,7 274,8 @@ std::unique_ptr<db::QueryResult> CalllogRecordInterface::getQuery(std::shared_pt
        record.ID           = calllog.ID;
        recordVector.emplace_back(record);
    }
    auto response = std::make_unique<db::query::CalllogGetResult>(recordVector);

    auto response = std::make_unique<db::query::CalllogGetResult>(std::move(recordVector), GetCount());
    response->setRequestQuery(query);
    return response;
}

M module-db/queries/calllog/QueryCalllogGet.cpp => module-db/queries/calllog/QueryCalllogGet.cpp +10 -4
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "QueryCalllogGet.hpp"


@@ 10,15 10,21 @@ using namespace db::query;
CalllogGet::CalllogGet(std::size_t limit, std::size_t offset) : RecordQuery(limit, offset)
{}

CalllogGetResult::CalllogGetResult(const std::vector<CalllogRecord> &records) : RecordQueryResult(records)
{}

[[nodiscard]] auto CalllogGet::debugInfo() const -> std::string
{
    return "CalllogGet";
}

CalllogGetResult::CalllogGetResult(std::vector<CalllogRecord> &&records, unsigned int dbRecordsCount)
    : RecordQueryResult(std::move(records)), dbRecordsCount{dbRecordsCount}
{}

[[nodiscard]] auto CalllogGetResult::debugInfo() const -> std::string
{
    return "CalllogGetResult";
}

auto CalllogGetResult::getTotalCount() const noexcept -> unsigned int
{
    return dbRecordsCount;
}

M module-db/queries/calllog/QueryCalllogGet.hpp => module-db/queries/calllog/QueryCalllogGet.hpp +7 -2
@@ 1,4 1,4 @@
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once


@@ 21,8 21,13 @@ namespace db::query
    class CalllogGetResult : public RecordQueryResult<CalllogRecord>
    {
      public:
        CalllogGetResult(const std::vector<CalllogRecord> &records);
        CalllogGetResult(std::vector<CalllogRecord> &&records, unsigned int dbRecordsCount);
        [[nodiscard]] auto debugInfo() const -> std::string override;

        [[nodiscard]] auto getTotalCount() const noexcept -> unsigned int;

      private:
        unsigned int dbRecordsCount;
    };

}; // namespace db::query