1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WATERFLOW_WATER_FLOW_SECTIONS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WATERFLOW_WATER_FLOW_SECTIONS_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <optional>
22 
23 #include "base/geometry/dimension.h"
24 #include "core/components_ng/property/measure_property.h"
25 
26 namespace OHOS::Ace::NG {
27 using GetItemMainSizeByIndex = std::function<float(int32_t)>;
28 
29 class WaterFlowSections : public virtual AceType {
30     DECLARE_ACE_TYPE(WaterFlowSections, AceType)
31 public:
32     struct Section {
33         bool operator==(const Section& other) const
34         {
35             return itemsCount == other.itemsCount && crossCount == other.crossCount && columnsGap == other.columnsGap &&
36                    rowsGap == other.rowsGap && margin == other.margin;
37         }
38         bool operator!=(const Section& other) const
39         {
40             return !(*this == other);
41         }
42 
OnlyCountDiffSection43         bool OnlyCountDiff(const Section& other) const
44         {
45             return crossCount == other.crossCount && columnsGap == other.columnsGap && rowsGap == other.rowsGap &&
46                    margin == other.margin;
47         }
48 
49         int32_t itemsCount = 0;
50         std::optional<int32_t> crossCount;
51         GetItemMainSizeByIndex onGetItemMainSizeByIndex;
52         std::optional<Dimension> columnsGap;
53         std::optional<Dimension> rowsGap;
54         std::optional<MarginProperty> margin;
55     };
56 
57     WaterFlowSections() = default;
58     ~WaterFlowSections() override = default;
SetOnDataChange(std::function<void (int32_t start)> && func)59     void SetOnDataChange(std::function<void(int32_t start)>&& func)
60     {
61         onSectionDataChange_ = func;
62     }
63 
64     void NotifySectionChange(
65         int32_t start, int32_t deleteCount, const std::vector<WaterFlowSections::Section>& newSections);
66 
SetNotifyDataChange(std::function<void (int32_t start,int32_t count)> && func)67     void SetNotifyDataChange(std::function<void(int32_t start, int32_t count)>&& func)
68     {
69         notifyDataChange_ = func;
70     }
71 
72     /**
73      * @brief Change section data.
74      *
75      * @param start index of the first modified section.
76      * @param deleteCount number of sections to delete at index [start].
77      * @param newSections to insert at index [start].
78      */
79     void ChangeData(size_t start, size_t deleteCount, const std::vector<Section>& newSections);
80 
81     // replace all sections from start
82     void ReplaceFrom(size_t start, const std::vector<Section>& newSections);
83 
GetSectionInfo()84     const std::vector<Section>& GetSectionInfo() const
85     {
86         return sections_;
87     }
88 
GetPrevSectionInfo()89     const std::vector<Section>& GetPrevSectionInfo() const
90     {
91         return prevSections_;
92     }
93 
94 private:
95     std::vector<Section> sections_;
96     // for comparing and handling special case
97     std::vector<Section> prevSections_;
98     std::function<void(int32_t start)> onSectionDataChange_;
99     std::function<void(int32_t start, int32_t count)> notifyDataChange_;
100 };
101 
102 } // namespace OHOS::Ace::NG
103 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WATERFLOW_WATER_FLOW_SECTIONS_H
104