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/lazy_for_each_node.h"
17
18 #include "base/memory/referenced.h"
19 #include "base/utils/time_util.h"
20 #include "base/utils/utils.h"
21 #include "core/components_ng/property/property.h"
22 #include "core/components_ng/syntax/for_each_node.h"
23 #include "core/components_ng/syntax/lazy_layout_wrapper_builder.h"
24 #include "core/components_v2/inspector/inspector_constants.h"
25 #include "core/pipeline/base/element_register.h"
26 #include "core/pipeline_ng/pipeline_context.h"
27
28 namespace OHOS::Ace::NG {
29
GetOrCreateLazyForEachNode(int32_t nodeId,const RefPtr<LazyForEachBuilder> & forEachBuilder)30 RefPtr<LazyForEachNode> LazyForEachNode::GetOrCreateLazyForEachNode(
31 int32_t nodeId, const RefPtr<LazyForEachBuilder>& forEachBuilder)
32 {
33 auto node = ElementRegister::GetInstance()->GetSpecificItemById<LazyForEachNode>(nodeId);
34 if (node) {
35 if (node->builder_ != forEachBuilder) {
36 LOGW("replace old lazy for each builder");
37 node->builder_ = forEachBuilder;
38 }
39 return node;
40 }
41 node = MakeRefPtr<LazyForEachNode>(nodeId, forEachBuilder);
42 ElementRegister::GetInstance()->AddUINode(node);
43 return node;
44 }
45
AdjustLayoutWrapperTree(const RefPtr<LayoutWrapper> & parent,bool forceMeasure,bool forceLayout)46 void LazyForEachNode::AdjustLayoutWrapperTree(const RefPtr<LayoutWrapper>& parent, bool forceMeasure, bool forceLayout)
47 {
48 CHECK_NULL_VOID(builder_);
49 auto lazyLayoutWrapperBuilder = MakeRefPtr<LazyLayoutWrapperBuilder>(builder_, WeakClaim(this));
50 if (parent->GetHostTag() == V2::SWIPER_ETS_TAG) {
51 lazyLayoutWrapperBuilder->SetLazySwiper();
52 }
53 lazyLayoutWrapperBuilder->UpdateIndexRange(startIndex_, endIndex_, ids_);
54 lazyLayoutWrapperBuilder->UpdateForceFlag(forceMeasure, forceLayout);
55 parent->SetLayoutWrapperBuilder(lazyLayoutWrapperBuilder);
56 }
57
UpdateLazyForEachItems(int32_t newStartIndex,int32_t newEndIndex,std::list<std::optional<std::string>> && nodeIds,std::unordered_map<int32_t,std::optional<std::string>> && cachedItems)58 void LazyForEachNode::UpdateLazyForEachItems(int32_t newStartIndex, int32_t newEndIndex,
59 std::list<std::optional<std::string>>&& nodeIds,
60 std::unordered_map<int32_t, std::optional<std::string>>&& cachedItems)
61 {
62 ACE_SCOPED_TRACE("lazyforeach update cache [%d -%d]", newStartIndex, newEndIndex);
63 CHECK_NULL_VOID(builder_);
64 std::list<std::optional<std::string>> newIds(std::move(nodeIds));
65
66 // delete all.
67 if (newIds.empty()) {
68 // clean current children.
69 Clean();
70 builder_->Clean();
71 startIndex_ = -1;
72 endIndex_ = -1;
73 ids_.clear();
74 return;
75 }
76
77 auto newSize = static_cast<int32_t>(newIds.size());
78 if ((newEndIndex - newStartIndex + 1) != newSize) {
79 LOGE("the index is illegal, %{public}d, %{public}d, %{public}d", newStartIndex, newEndIndex, newSize);
80 return;
81 }
82
83 int32_t slot = 0;
84 // use new ids to update child tree.
85 for (const auto& id : newIds) {
86 CHECK_NULL_VOID(id);
87 auto uiNode = builder_->GetChildByKey(*id);
88 CHECK_NULL_VOID(uiNode);
89 int32_t childIndex = GetChildIndex(uiNode);
90 if (childIndex < 0) {
91 AddChild(uiNode, slot);
92 } else if (childIndex != slot) {
93 uiNode->MovePosition(slot);
94 }
95 slot++;
96 }
97 while (static_cast<size_t>(slot) < GetChildren().size()) {
98 RemoveChild(GetLastChild());
99 }
100
101 // delete useless items.
102 builder_->UpdateCachedItems(newIds, std::move(cachedItems));
103
104 startIndex_ = newStartIndex;
105 endIndex_ = newEndIndex;
106 std::swap(ids_, newIds);
107 LOGD("cachedItems size is %{public}d", static_cast<int32_t>(newIds.size()));
108 }
109
PostIdleTask(std::list<int32_t> && items)110 void LazyForEachNode::PostIdleTask(std::list<int32_t>&& items)
111 {
112 auto context = GetContext();
113 CHECK_NULL_VOID(context);
114 predictItems_ = std::move(items);
115 if (needPredict) {
116 return;
117 }
118 needPredict = true;
119 context->AddPredictTask([weak = AceType::WeakClaim(this)](int64_t deadline) {
120 auto node = weak.Upgrade();
121 CHECK_NULL_VOID(node);
122 node->needPredict = false;
123 ACE_SCOPED_TRACE("LazyForEach predict size[%zu]", node->predictItems_.size());
124 decltype(node->predictItems_) items(std::move(node->predictItems_));
125 auto item = items.begin();
126 while (item != items.end()) {
127 if (GetSysTimestamp() > deadline) {
128 std::list<int32_t> predictItems;
129 predictItems.insert(predictItems.begin(), item, items.end());
130 node->PostIdleTask(std::move(predictItems));
131 return;
132 }
133
134 auto itemInfo = node->builder_->CreateChildByIndex(*item);
135 node->builder_->SetCacheItemInfo(*item, itemInfo.first);
136 auto uiNode = itemInfo.second;
137 if (uiNode) {
138 uiNode->Build();
139 }
140 item++;
141 }
142 });
143 }
144
OnDataReloaded()145 void LazyForEachNode::OnDataReloaded()
146 {
147 startIndex_ = -1;
148 endIndex_ = -1;
149 ids_.clear();
150 NotifyDataCountChanged(0);
151 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
152 }
153
OnDataAdded(size_t index)154 void LazyForEachNode::OnDataAdded(size_t index)
155 {
156 auto insertIndex = static_cast<int32_t>(index);
157 NotifyDataCountChanged(insertIndex);
158 // check if insertIndex is in the range [startIndex_, endIndex_ + 1]
159 if ((insertIndex < startIndex_)) {
160 LOGI("insertIndex is out of begin range, ignored, %{public}d, %{public}d", insertIndex, startIndex_);
161 startIndex_++;
162 endIndex_++;
163 return;
164 }
165 if (insertIndex > (endIndex_ + 1)) {
166 LOGI("insertIndex is out of end range, ignored, %{public}d, %{public}d", insertIndex, endIndex_);
167 return;
168 }
169 if (insertIndex == startIndex_) {
170 // insert at begin.
171 ids_.emplace_front(std::nullopt);
172 } else if (insertIndex == (endIndex_ + 1)) {
173 // insert at end.
174 ids_.emplace_back(std::nullopt);
175 } else {
176 // insert at middle.
177 auto iter = ids_.begin();
178 std::advance(iter, index - startIndex_);
179 ids_.insert(iter, std::nullopt);
180 }
181 endIndex_++;
182
183 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
184 }
185
OnDataDeleted(size_t index)186 void LazyForEachNode::OnDataDeleted(size_t index)
187 {
188 auto deletedIndex = static_cast<int32_t>(index);
189 NotifyDataCountChanged(deletedIndex);
190 if (deletedIndex > endIndex_) {
191 LOGI("deletedIndex is out of end range, ignored, %{public}d, %{public}d", deletedIndex, endIndex_);
192 return;
193 }
194 if (deletedIndex < startIndex_) {
195 LOGI("deletedIndex is out of begin range, ignored, %{public}d, %{public}d", deletedIndex, startIndex_);
196 startIndex_--;
197 endIndex_--;
198 return;
199 }
200 if (deletedIndex == startIndex_) {
201 // delete at begin.
202 ids_.pop_front();
203 } else if (deletedIndex == endIndex_) {
204 // delete at end.
205 ids_.pop_back();
206 } else {
207 // delete at middle.
208 auto iter = ids_.begin();
209 std::advance(iter, index - startIndex_);
210 ids_.erase(iter);
211 }
212 endIndex_--;
213
214 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
215 }
216
OnDataChanged(size_t index)217 void LazyForEachNode::OnDataChanged(size_t index)
218 {
219 auto changeIndex = static_cast<int32_t>(index);
220 if ((changeIndex < startIndex_) || (changeIndex > endIndex_)) {
221 LOGI("changeIndex is out of range, ignored, %{public}d, %{public}d, %{public}d", changeIndex, startIndex_,
222 endIndex_);
223 return;
224 }
225 auto iter = ids_.begin();
226 std::advance(iter, index - startIndex_);
227 *iter = std::nullopt;
228 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
229 }
230
OnDataMoved(size_t from,size_t to)231 void LazyForEachNode::OnDataMoved(size_t from, size_t to)
232 {
233 auto fromIndex = static_cast<int32_t>(from);
234 auto toIndex = static_cast<int32_t>(to);
235 NotifyDataCountChanged(startIndex_ + std::min(fromIndex, toIndex));
236 auto fromOutOfRange = (fromIndex < startIndex_) || (fromIndex > endIndex_);
237 auto toOutOfRange = (toIndex < startIndex_) || (toIndex > endIndex_);
238 if (fromOutOfRange && toOutOfRange) {
239 LOGI("both out of range, ignored");
240 return;
241 }
242
243 if (fromOutOfRange && !toOutOfRange) {
244 auto iter = ids_.begin();
245 std::advance(iter, toIndex - startIndex_);
246 *iter = std::nullopt;
247 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
248 return;
249 }
250 if (!fromOutOfRange && toOutOfRange) {
251 auto iter = ids_.begin();
252 std::advance(iter, fromIndex - startIndex_);
253 *iter = std::nullopt;
254 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
255 return;
256 }
257 auto formIter = ids_.begin();
258 std::advance(formIter, fromIndex - startIndex_);
259 auto toIter = ids_.begin();
260 std::advance(toIter, toIndex - startIndex_);
261 auto temp = *formIter;
262 *formIter = *toIter;
263 *toIter = temp;
264 MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
265 }
266
NotifyDataCountChanged(int32_t index)267 void LazyForEachNode::NotifyDataCountChanged(int32_t index)
268 {
269 auto parent = GetParent();
270 if (parent) {
271 parent->ChildrenUpdatedFrom(index);
272 }
273 }
274 } // namespace OHOS::Ace::NG
275