• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_trace.h"
22 #include "core/components_ng/base/ui_node.h"
23 #include "core/components_ng/property/property.h"
24 #include "core/pipeline/base/element_register.h"
25 
26 namespace OHOS::Ace::NG {
27 namespace {
MakeNodeMapById(const std::list<RefPtr<UINode>> & nodes,const std::list<std::string> & ids,std::map<std::string,RefPtr<UINode>> & result)28 void MakeNodeMapById(const std::list<RefPtr<UINode>>& nodes, const std::list<std::string>& ids,
29     std::map<std::string, RefPtr<UINode>>& result)
30 {
31     ACE_DCHECK(ids.size() == nodes.size());
32     auto idsIter = ids.begin();
33     auto nodeIter = nodes.begin();
34     while (idsIter != ids.end() && nodeIter != nodes.end()) {
35         result.emplace(*idsIter, *nodeIter);
36         ++idsIter;
37         ++nodeIter;
38     }
39 }
40 } // namespace
41 
GetOrCreateForEachNode(int32_t nodeId)42 RefPtr<ForEachNode> ForEachNode::GetOrCreateForEachNode(int32_t nodeId)
43 {
44     auto node = ElementRegister::GetInstance()->GetSpecificItemById<ForEachNode>(nodeId);
45     if (node) {
46         return node;
47     }
48     node = MakeRefPtr<ForEachNode>(nodeId);
49     ElementRegister::GetInstance()->AddUINode(node);
50     return node;
51 }
52 
CreateTempItems()53 void ForEachNode::CreateTempItems()
54 {
55     std::swap(ids_, tempIds_);
56     std::swap(ModifyChildren(), tempChildren_);
57 }
58 
59 // same as foundation/arkui/ace_engine/frameworks/core/components_part_upd/foreach/foreach_element.cpp.
CompareAndUpdateChildren()60 void ForEachNode::CompareAndUpdateChildren()
61 {
62     ACE_SCOPED_TRACE("ForEachNode::CompareAndUpdateChildren");
63 
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);
119         }
120     }
121 
122     if (IsOnMainTree()) {
123         for (const auto& newChild : additionalChildComps) {
124             newChild->AttachToMainTree();
125         }
126     }
127 
128     tempChildren_.clear();
129     tempIds_.clear();
130 
131     auto parent = GetParent();
132     if (parent) {
133         parent->ChildrenUpdatedFrom(0);
134     }
135 }
136 
FlushUpdateAndMarkDirty()137 void ForEachNode::FlushUpdateAndMarkDirty()
138 {
139     // mark parent dirty to flush measure.
140     MarkNeedSyncRenderTree();
141     MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT | PROPERTY_UPDATE_BY_CHILD_REQUEST);
142 }
143 
144 } // namespace OHOS::Ace::NG