1 /*
2  * Copyright (c) 2022 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_SYNTAX_FOR_EACH_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_NODE_H
18 
19 #include <cstdint>
20 #include <list>
21 #include <string>
22 #include <type_traits>
23 
24 #include "base/utils/macros.h"
25 #include "core/components_ng/base/ui_node.h"
26 #include "core/components_ng/syntax/for_each_base_node.h"
27 #include "core/components_v2/inspector/inspector_constants.h"
28 
29 namespace OHOS::Ace::NG {
30 
31 class ACE_EXPORT ForEachNode : public ForEachBaseNode {
32     DECLARE_ACE_TYPE(ForEachNode, ForEachBaseNode);
33 
34 public:
35     static RefPtr<ForEachNode> GetOrCreateForEachNode(int32_t nodeId);
36     static RefPtr<ForEachNode> GetOrCreateRepeatNode(int32_t nodeId);
37 
ForEachNode(int32_t nodeId)38     explicit ForEachNode(int32_t nodeId) : ForEachBaseNode(V2::JS_FOR_EACH_ETS_TAG, nodeId) {}
39 
40     ~ForEachNode() override = default;
41 
IsAtomicNode()42     bool IsAtomicNode() const override
43     {
44         return false;
45     }
46 
47     void CreateTempItems();
48 
49     void CollectRemovingIds(std::list<int32_t>& removedElmtId);
50 
51     void CompareAndUpdateChildren();
52 
53     void FlushUpdateAndMarkDirty() override;
54 
55     // RepeatNode only
56     void FinishRepeatRender(std::list<int32_t>& removedElmtId);
57 
58     // RepeatNode only
59     void MoveChild(uint32_t fromIndex);
60 
GetTempIds()61     const std::list<std::string>& GetTempIds() const
62     {
63         return tempIds_;
64     }
65 
SetIds(std::list<std::string> && ids)66     void SetIds(std::list<std::string>&& ids)
67     {
68         ids_ = std::move(ids);
69     }
70 
71     void SetOnMove(std::function<void(int32_t, int32_t)>&& onMove);
72     void MoveData(int32_t from, int32_t to) override;
73     RefPtr<FrameNode> GetFrameNode(int32_t index) override;
74     void InitDragManager(const RefPtr<UINode>& childNode);
75     void InitAllChildrenDragManager(bool init);
76     void MappingChildWithId(std::unordered_set<std::string>& oldIdsSet, std::list<RefPtr<UINode>>& additionalChildComps,
77         std::map<std::string, RefPtr<UINode>>& oldNodeByIdMap);
78 private:
79     std::list<std::string> ids_;
80 
81     // temp items use to compare each update.
82     std::list<std::string> tempIds_;
83     std::list<RefPtr<UINode>> tempChildren_;
84     std::unordered_set<std::string> tempOldIdsSet_;
85 
86     // create map id -> Node
87     std::map<std::string, RefPtr<UINode>> oldNodeByIdMap_;
88 
89     // RepeatNode only
90     std::vector<RefPtr<UINode>> tempChildrenOfRepeat_;
91 
92     // true when this is actually RepeatNode (not "ForEach")
93     bool isThisRepeatNode_ = false;
94 
95     ACE_DISALLOW_COPY_AND_MOVE(ForEachNode);
96 };
97 
98 } // namespace OHOS::Ace::NG
99 
100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_FOR_EACH_NODE_H
101