~aleteoryx/muditaos

ref: 8f801262510042a71c5c2ef8b2ed1391995d8f7d muditaos/module-apps/application-notes/windows/SearchResultsWindow.cpp -rw-r--r-- 3.1 KiB
8f801262 — Przemyslaw Brudny [EGD-3434] ListView Scroll bar refactor 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
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SearchResultsWindow.hpp"

#include <module-apps/application-notes/ApplicationNotes.hpp>
#include <module-apps/application-notes/data/NotesFoundData.hpp>
#include <module-apps/application-notes/style/NotesListStyle.hpp>
#include <module-apps/windows/DialogMetadata.hpp>
#include <module-apps/messages/DialogMetadataMessage.hpp>

namespace app::notes
{
    SearchResultsWindow::SearchResultsWindow(Application *application)
        : AppWindow(application, gui::name::window::notes_search_result), listModel{
                                                                              std::make_shared<SearchResultsListModel>(
                                                                                  application)}
    {
        buildInterface();
    }

    SearchResultsWindow::~SearchResultsWindow() noexcept
    {
        destroyInterface();
    }

    void SearchResultsWindow::buildInterface()
    {
        AppWindow::buildInterface();
        setTitle(utils::localize.get("app_notes_title_main"));

        bottomBar->setActive(gui::BottomBar::Side::CENTER, true);
        bottomBar->setText(gui::BottomBar::Side::CENTER, utils::localize.get(::style::strings::common::open));
        bottomBar->setActive(gui::BottomBar::Side::RIGHT, true);
        bottomBar->setText(gui::BottomBar::Side::RIGHT, utils::localize.get(::style::strings::common::back));

        list = new gui::ListView(this,
                                 style::list::X,
                                 style::list::Y,
                                 style::list::Width,
                                 style::list::Height,
                                 listModel,
                                 ::style::listview::ScrollBarType::Fixed);
        list->setScrollTopMargin(::style::margins::small);
        setFocusItem(list);
    }

    void SearchResultsWindow::destroyInterface()
    {
        erase();
        list = nullptr;
    }

    void SearchResultsWindow::onBeforeShow(gui::ShowMode mode, gui::SwitchData *data)
    {
        auto foundNotesData = dynamic_cast<NotesFoundData *>(data);
        if (foundNotesData == nullptr) {
            onNothingFound();
            return;
        }

        if (const auto &records = foundNotesData->getFoundRecords(); !records.empty()) {
            listModel->setResults(records);
            list->rebuildList();
            setFocusItem(list);
        }
        else {
            onNothingFound(foundNotesData->getSearchText());
        }
    }

    void SearchResultsWindow::onNothingFound(const std::string &searchText)
    {
        gui::DialogMetadata meta{utils::localize.get("common_results_prefix") + searchText,
                                 "search_big",
                                 utils::localize.get("app_notes_search_no_results")};
        auto data                        = std::make_unique<gui::DialogMetadataMessage>(meta);
        data->ignoreCurrentWindowOnStack = true;
        application->switchWindow(gui::name::window::note_dialog, std::move(data));
    }
} // namespace app::notes