~aleteoryx/muditaos

f28d8cf936d83131f26a5448077ce43ec2e7106d — Mateusz Grzegorzek 5 years ago 2748125
[EGD-6193] Connect Categories window with agent

Connect Categories window with agent
M module-apps/application-settings-new/ApplicationSettings.cpp => module-apps/application-settings-new/ApplicationSettings.cpp +1 -6
@@ 89,11 89,6 @@ namespace app
            auto repo = std::make_unique<QuotesJsonRepository>(settings::quotesPath);
            return std::make_unique<QuotesModel>(app, std::move(repo));
        }

        auto getCategoriesModel(Application *app) -> std::unique_ptr<CategoriesModel>
        {
            return std::make_unique<CategoriesModel>(app);
        }
    } // namespace settings

    ApplicationSettingsNew::ApplicationSettingsNew(std::string name,


@@ 469,7 464,7 @@ namespace app
            return std::make_unique<gui::EditQuotesWindow>(app);
        });
        windowsFactory.attach(gui::window::name::quote_categories, [](Application *app, const std::string &name) {
            return std::make_unique<gui::QuoteCategoriesWindow>(app, std::move(settings::getCategoriesModel(app)));
            return std::make_unique<gui::QuoteCategoriesWindow>(app, std::make_unique<Quotes::CategoriesModel>(app));
        });

        attachPopups({gui::popup::ID::Volume});

M module-apps/application-settings-new/models/CategoriesModel.cpp => module-apps/application-settings-new/models/CategoriesModel.cpp +50 -11
@@ 4,6 4,7 @@
#include "application-settings-new/windows/QuoteCategoriesWindow.hpp"
#include "QuotesRepository.hpp"
#include "CategoriesModel.hpp"
#include <service-db/QuotesMessages.hpp>

namespace style::quotes::list
{


@@ 11,7 12,7 @@ namespace style::quotes::list
    constexpr auto max_quotes  = 100;
} // namespace style::quotes::list

namespace app
namespace Quotes
{
    CategoriesModel::CategoriesModel(app::Application *app) : application(app)
    {}


@@ 39,18 40,32 @@ namespace app

    void CategoriesModel::createData()
    {
        // fake categories for testing
        // matter to be changed with the final connection of the appropriate agent
        std::vector<app::QuoteCategory> categories{{0, "Category 1"},
                                                   {1, "Category 2"},
                                                   {2, "Category 3"},
                                                   {3, "Category 4"},
                                                   {4, "Category 5"},
                                                   {5, "Category 6"}};
        for (const auto &category : categories) {
        const auto categoryList = getCategoryList();
        if (!categoryList) {
            return;
        }

        for (const auto &category : *categoryList) {
            auto app  = application;
            auto item = new gui::CategoryWidget(
                category,
                [app, category](bool enable) -> bool {
                    LOG_DEBUG(
                        "Sending enable category request: category_id = %d, enable = %d", category.category_id, enable);
                    auto request = std::make_shared<Messages::EnableCategoryByIdRequest>(category.category_id, enable);
                    auto result  = app->bus.sendUnicast(request, service::name::db, DBServiceAPI::DefaultTimeoutInMs);

                    if (result.first != sys::ReturnCodes::Success) {
                        LOG_WARN("Enable category request failed! error code = %d", static_cast<int>(result.first));
                        return false;
                    }

                    auto response      = std::dynamic_pointer_cast<Messages::EnableCategoryByIdResponse>(result.second);
                    const auto success = response && response->success;
                    if (!success)
                        LOG_WARN("Enable category request failed!");
                    return success;
                },
                [app](const UTF8 &text) {
                    app->getCurrentWindow()->bottomBarTemporaryMode(text, gui::BottomBar::Side::CENTER, false);
                },


@@ 68,4 83,28 @@ namespace app
        createData();
        list->rebuildList();
    }
} // namespace app

    auto CategoriesModel::getCategoryList() -> std::optional<std::vector<CategoryRecord>>
    {
        auto categoryList    = std::make_unique<CategoryList>();
        categoryList->limit  = 0;
        categoryList->offset = 0;

        auto request = std::make_shared<Messages::GetCategoryListRequest>(std::move(categoryList));
        auto result =
            application->bus.sendUnicast(std::move(request), service::name::db, DBServiceAPI::DefaultTimeoutInMs);
        if (result.first != sys::ReturnCodes::Success) {
            LOG_WARN("Getting category list failed! error code = %d", static_cast<int>(result.first));
            return std::nullopt;
        }

        auto response = std::dynamic_pointer_cast<Messages::GetCategoryListResponse>(result.second);
        if (!response) {
            LOG_WARN("Wrong response on category list request!");
            return std::nullopt;
        }

        LOG_DEBUG("Categories list count: %u", response->getCount());
        return response->getResults();
    }
} // namespace Quotes

M module-apps/application-settings-new/models/CategoriesModel.hpp => module-apps/application-settings-new/models/CategoriesModel.hpp +5 -5
@@ 13,16 13,17 @@ namespace gui
    class CategoryWidget;
}

namespace app
namespace Quotes
{

    class CategoriesModel : public app::InternalModel<gui::CategoryWidget *>, public gui::ListItemProvider
    {
      private:
        app::Application *application = nullptr;

        auto getCategoryList() -> std::optional<std::vector<CategoryRecord>>;

      public:
        CategoriesModel(app::Application *app);
        explicit CategoriesModel(app::Application *app);
        [[nodiscard]] auto requestRecordsCount() -> unsigned int final;
        [[nodiscard]] auto getMinimalItemHeight() const -> unsigned int final;
        auto getItem(gui::Order order) -> gui::ListItem * final;


@@ 30,5 31,4 @@ namespace app
        void createData();
        void rebuild();
    };

} // namespace app
} // namespace Quotes

M module-apps/application-settings-new/models/QuotesRepository.hpp => module-apps/application-settings-new/models/QuotesRepository.hpp +0 -6
@@ 27,12 27,6 @@ namespace app
        }
    };

    struct QuoteCategory
    {
        int id = 0;
        std::string categoryDescription;
    };

    class QuotesRepository
    {
      public:

M module-apps/application-settings-new/widgets/CategoryWidget.cpp => module-apps/application-settings-new/widgets/CategoryWidget.cpp +18 -14
@@ 33,13 33,14 @@ namespace style::quotes
namespace gui
{

    CategoryWidget::CategoryWidget(const app::QuoteCategory &category,
    CategoryWidget::CategoryWidget(const Quotes::CategoryRecord &categoryRecord,
                                   std::function<bool(bool)> enableCategoryCallback,
                                   std::function<void(const UTF8 &)> bottomBarTemporaryMode,
                                   std::function<void()> bottomBarRestoreFromTemporaryMode)
        : bottomBarTemporaryMode(std::move(bottomBarTemporaryMode)),
          bottomBarRestoreFromTemporaryMode(std::move(bottomBarRestoreFromTemporaryMode)), category(category)
        : category(categoryRecord), enableCategory(std::move(enableCategoryCallback)),
          bottomBarTemporaryMode(std::move(bottomBarTemporaryMode)),
          bottomBarRestoreFromTemporaryMode(std::move(bottomBarRestoreFromTemporaryMode))
    {

        setMinimumSize(style::quotes::widget::w, style::quotes::widget::h);

        setMargins(gui::Margins(0, style::margins::big, 0, 0));


@@ 64,7 65,7 @@ namespace gui
        tickImage->setMargins(gui::Margins(
            style::quotes::widget::tick_image_left_margin, 0, style::quotes::widget::tick_image_right_margin, 0));
        tickImage->set("small_tick_W_M");
        tickImage->setVisible(true);
        tickImage->setVisible(category.enabled);
        tickImage->activeItem = false;

        descriptionLabel = new gui::Label(hBox, 0, 0, 0, 0);


@@ 76,18 77,18 @@ namespace gui
        descriptionLabel->setFont(style::window::font::medium);
        descriptionLabel->activeItem = false;

        descriptionLabel->setText(category.categoryDescription);
        descriptionLabel->setText(category.category_name);

        focusChangedCallback = [&](gui::Item &item) {
            if (item.focus) {
                descriptionLabel->setFont(style::footer::font::bold);
                descriptionLabel->setFont(style::window::font::mediumbold);
                setFocusItem(inputBoxLabel);
                auto bottorBarText =
                    tickImage->visible ? utils::localize.get("common_uncheck") : utils::localize.get("common_check");
                    category.enabled ? utils::localize.get("common_uncheck") : utils::localize.get("common_check");
                this->bottomBarTemporaryMode(bottorBarText);
            }
            else {
                descriptionLabel->setFont(style::footer::font::medium);
                descriptionLabel->setFont(style::window::font::medium);
                setFocusItem(nullptr);
                this->bottomBarRestoreFromTemporaryMode();
            }


@@ 95,11 96,14 @@ namespace gui
        };

        activatedCallback = [&](gui::Item &item) {
            tickImage->setVisible(!tickImage->visible);
            auto bottorBarText =
                tickImage->visible ? utils::localize.get("common_uncheck") : utils::localize.get("common_check");
            this->bottomBarTemporaryMode(bottorBarText);
            hBox->resizeItems();
            if (enableCategory(!category.enabled)) {
                category.enabled = !category.enabled;
                tickImage->setVisible(category.enabled);
                auto bottorBarText =
                    category.enabled ? utils::localize.get("common_uncheck") : utils::localize.get("common_check");
                this->bottomBarTemporaryMode(bottorBarText);
                hBox->resizeItems();
            }
            return true;
        };


M module-apps/application-settings-new/widgets/CategoryWidget.hpp => module-apps/application-settings-new/widgets/CategoryWidget.hpp +5 -2
@@ 9,13 9,15 @@
#include <Image.hpp>
#include <Label.hpp>
#include <ListItem.hpp>
#include <service-db/QuotesMessages.hpp>

namespace gui
{
    class CategoryWidget : public ListItem
    {
      public:
        CategoryWidget(const app::QuoteCategory &category,
        CategoryWidget(const Quotes::CategoryRecord &categoryRecord,
                       std::function<bool(bool)> enableCategory,
                       std::function<void(const UTF8 &text)> bottomBarTemporaryMode = nullptr,
                       std::function<void()> bottomBarRestoreFromTemporaryMode      = nullptr);



@@ 25,10 27,11 @@ namespace gui
        gui::Label *descriptionLabel = nullptr;
        gui::Image *tickImage        = nullptr;

        Quotes::CategoryRecord category;
        std::function<bool(bool)> enableCategory                     = nullptr;
        std::function<void(const UTF8 &text)> bottomBarTemporaryMode = nullptr;
        std::function<void()> bottomBarRestoreFromTemporaryMode      = nullptr;

        app::QuoteCategory category;
    };

} /* namespace gui */

M module-apps/application-settings-new/windows/QuoteCategoriesWindow.cpp => module-apps/application-settings-new/windows/QuoteCategoriesWindow.cpp +1 -1
@@ 8,7 8,7 @@

namespace gui
{
    QuoteCategoriesWindow::QuoteCategoriesWindow(app::Application *app, std::shared_ptr<app::CategoriesModel> model)
    QuoteCategoriesWindow::QuoteCategoriesWindow(app::Application *app, std::shared_ptr<Quotes::CategoriesModel> model)
        : AppWindow(app, gui::window::name::quote_categories), categoriesModel(std::move(model))
    {
        buildInterface();

M module-apps/application-settings-new/windows/QuoteCategoriesWindow.hpp => module-apps/application-settings-new/windows/QuoteCategoriesWindow.hpp +3 -3
@@ 14,14 14,14 @@ namespace gui
    class QuoteCategoriesWindow : public AppWindow
    {
      public:
        QuoteCategoriesWindow(app::Application *app, std::shared_ptr<app::CategoriesModel> model);
        QuoteCategoriesWindow(app::Application *app, std::shared_ptr<Quotes::CategoriesModel> model);

      private:
        void buildInterface() override;
        void onBeforeShow(ShowMode mode, SwitchData *data) override;

        std::shared_ptr<app::CategoriesModel> categoriesModel = nullptr;
        gui::ListView *list                                   = nullptr;
        std::shared_ptr<Quotes::CategoriesModel> categoriesModel = nullptr;
        gui::ListView *list                                      = nullptr;
    };

} // namespace gui