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 #include "core/components_ng/syntax/for_each_node.h"
17
18 #include <list>
19 #include <type_traits>
20
21 #include "base/log/ace_performance_check.h"
22 #include "base/log/ace_trace.h"
23 #include "core/components_ng/base/ui_node.h"
24 #include "core/components_ng/property/property.h"
25 #include "core/pipeline/base/element_register.h"
26
27 namespace OHOS::Ace::NG {
28 namespace {
MakeNodeMapById(const std::list<RefPtr<UINode>> & nodes,const std::list<std::string> & ids,std::map<std::string,RefPtr<UINode>> & result)29 void MakeNodeMapById(const std::list<RefPtr<UINode>>& nodes, const std::list<std::string>& ids,
30 std::map<std::string, RefPtr<UINode>>& result)
31 {
32 ACE_DCHECK(ids.size() == nodes.size());
33 auto idsIter = ids.begin();
34 auto nodeIter = nodes.begin();
35 while (idsIter != ids.end() && nodeIter != nodes.end()) {
36 result.emplace(*idsIter, *nodeIter);
37 ++idsIter;
38 ++nodeIter;
39 }
40 }
41 } // namespace
42
GetOrCreateForEachNode(int32_t nodeId)43 RefPtr<ForEachNode> ForEachNode::GetOrCreateForEachNode(int32_t nodeId)
44 {
45 auto node = ElementRegister::GetInstance()->GetSpecificItemById<ForEachNode>(nodeId);
46 if (node) {
47 return node;
48 }
49 node = MakeRefPtr<ForEachNode>(nodeId);
50 ElementRegister::GetInstance()->AddUINode(node);
51 return node;
52 }
53
CreateTempItems()54 void ForEachNode::CreateTempItems()
55 {
56 std::swap(ids_, tempIds_);
57 std::swap(ModifyChildren(), tempChildren_);
58 }
59
60 // same as foundation/arkui/ace_engine/frameworks/core/components_part_upd/foreach/foreach_element.cpp.
CompareAndUpdateChildren()61 void ForEachNode::CompareAndUpdateChildren()
62 {
63 ACE_SCOPED_TRACE("ForEachNode::CompareAndUpdateChildren");
64 LOGD("Local update for ForEachNode nodeId: %{public}d ....", GetId());
65
66 // result of id gen function of most re-recent render
67 // create a map for quicker find/search
68 std::unordered_set<std::string> newIdsSet(ids_.begin(), ids_.end());
69
70 // result of id gen function of previous render/re-render
71 // create a map for quicker find/search
72 std::unordered_set<std::string> oldIdsSet(tempIds_.begin(), tempIds_.end());
73
74 // ForEachNode only includes children for newly created_ array items
75 // it does not include children of array items that were rendered on a previous
76 // render
77 std::list<RefPtr<UINode>> additionalChildComps;
78 auto& children = ModifyChildren();
79 std::swap(additionalChildComps, children);
80
81 // create map id -> Node
82 // old children
83 std::map<std::string, RefPtr<UINode>> oldNodeByIdMap;
84 MakeNodeMapById(tempChildren_, tempIds_, oldNodeByIdMap);
85
86 int32_t additionalChildIndex = 0;
87 for (const auto& newId : ids_) {
88 auto oldIdIt = oldIdsSet.find(newId);
89 if (oldIdIt == oldIdsSet.end()) {
90 // found a newly added ID
91 // insert new child item.
92 auto newCompsIter = additionalChildComps.begin();
93 std::advance(newCompsIter, additionalChildIndex++);
94 if (newCompsIter != additionalChildComps.end()) {
95 // Call AddChild to execute AttachToMainTree of new child.
96 AddChild(*newCompsIter);
97 }
98 } else {
99 auto iter = oldNodeByIdMap.find(newId);
100 // the ID was used before, only need to update the child position.
101 if (iter != oldNodeByIdMap.end() && iter->second) {
102 AddChild(iter->second, DEFAULT_NODE_SLOT, true);
103 }
104 oldIdsSet.erase(oldIdIt);
105 }
106 }
107
108 for (const auto& oldId : oldIdsSet) {
109 auto iter = oldNodeByIdMap.find(oldId);
110 if (iter != oldNodeByIdMap.end()) {
111 // Adding silently, so that upon removal
112 // node is a part the tree.
113 // OnDetachFromMainTree to be called while node
114 // still part of the tree, we need to find
115 // position in the tab tab for the tab.
116 AddChild(iter->second, DEFAULT_NODE_SLOT, true);
117 // Remove and trigger all Detach callback.
118 RemoveChild(iter->second, true);
119 }
120 }
121
122 if (IsOnMainTree()) {
123 for (const auto& newChild : additionalChildComps) {
124 newChild->AttachToMainTree();
125 }
126 }
127
128 tempChildren_.clear();
129
130 auto parent = GetParent();
131 if (parent) {
132 parent->ChildrenUpdatedFrom(0);
133 }
134 }
135
FlushUpdateAndMarkDirty()136 void ForEachNode::FlushUpdateAndMarkDirty()
137 {
138 if (ids_ == tempIds_) {
139 tempIds_.clear();
140 return;
141 }
142 tempIds_.clear();
143 // mark parent dirty to flush measure.
144 MarkNeedSyncRenderTree(true);
145 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT | PROPERTY_UPDATE_BY_CHILD_REQUEST);
146 }
147
148 } // namespace OHOS::Ace::NG
149