~aleteoryx/muditaos

muditaos/module-apps/apps-common/InternalModel.hpp -rw-r--r-- 2.5 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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#pragma once

#include <module-gui/gui/widgets/ListItemProvider.hpp>
#include <vector>

namespace app
{

    template <class T>
    class InternalModel
    {

      protected:
        int modelIndex              = 0;
        unsigned int internalOffset = 0;
        unsigned int internalLimit  = 0;
        std::vector<T> internalData;

        virtual ~InternalModel()
        {
            eraseInternalData();
        }

        void eraseInternalData()
        {
            for (auto item : internalData) {
                delete item;
            }
            internalData.clear();
        }

        void setupModel(const uint32_t offset, const uint32_t limit)
        {
            modelIndex     = 0;
            internalOffset = offset;
            internalLimit  = limit;
        }

        T getRecord(gui::Order order)
        {
            auto index = 0;
            if (order == gui::Order::Previous) {
                auto calculatedIndex = internalOffset + internalLimit >= internalData.size()
                                           ? internalData.size()
                                           : internalOffset + internalLimit;

                index = calculatedIndex - 1 + modelIndex;
                modelIndex--;
            }
            if (order == gui::Order::Next) {
                index = internalOffset + modelIndex;

                modelIndex++;
            }

            return getInternalDataElement(index, order);
        }

        [[nodiscard]] bool isIndexValid(unsigned int index, gui::Order order) const noexcept
        {
            return (order == gui::Order::Next && index < internalData.size()) ||
                   (order == gui::Order::Previous && static_cast<int>(index) >= 0 && index < internalData.size() &&
                    index >= internalOffset);
        }

        void clearItemProperties(T Item)
        {
            Item->setFocus(false);
            Item->setVisible(true);
            Item->clearNavigationItem(gui::NavigationDirection::UP);
            Item->clearNavigationItem(gui::NavigationDirection::DOWN);
        }

        [[nodiscard]] T getInternalDataElement(unsigned int index, gui::Order order)
        {
            if (isIndexValid(index, order)) {
                clearItemProperties(internalData[index]);
                return internalData[index];
            }
            else {
                return nullptr;
            }
        }
    };

} /* namespace app */