• 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/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     // check if insertIndex is in the range [startIndex_, endIndex_ + 1]
158     if ((insertIndex < startIndex_)) {
159         LOGI("insertIndex is out of begin range, ignored, %{public}d, %{public}d", insertIndex, startIndex_);
160         startIndex_++;
161         endIndex_++;
162         return;
163     }
164     if (insertIndex > (endIndex_ + 1)) {
165         LOGI("insertIndex is out of end range, ignored, %{public}d, %{public}d", insertIndex, endIndex_);
166         return;
167     }
168     if (insertIndex == startIndex_) {
169         // insert at begin.
170         ids_.emplace_front(std::nullopt);
171     } else if (insertIndex == (endIndex_ + 1)) {
172         // insert at end.
173         ids_.emplace_back(std::nullopt);
174     } else {
175         // insert at middle.
176         auto iter = ids_.begin();
177         std::advance(iter, index - startIndex_);
178         ids_.insert(iter, std::nullopt);
179     }
180     endIndex_++;
181     NotifyDataCountChanged(insertIndex);
182     MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
183 }
184 
OnDataDeleted(size_t index)185 void LazyForEachNode::OnDataDeleted(size_t index)
186 {
187     auto deletedIndex = static_cast<int32_t>(index);
188     if (deletedIndex > endIndex_) {
189         LOGI("deletedIndex is out of end range, ignored, %{public}d, %{public}d", deletedIndex, endIndex_);
190         return;
191     }
192     if (deletedIndex < startIndex_) {
193         LOGI("deletedIndex is out of begin range, ignored, %{public}d, %{public}d", deletedIndex, startIndex_);
194         startIndex_--;
195         endIndex_--;
196         return;
197     }
198     if (deletedIndex == startIndex_) {
199         // delete at begin.
200         ids_.pop_front();
201     } else if (deletedIndex == endIndex_) {
202         // delete at end.
203         ids_.pop_back();
204     } else {
205         // delete at middle.
206         auto iter = ids_.begin();
207         std::advance(iter, index - startIndex_);
208         ids_.erase(iter);
209     }
210     endIndex_--;
211     NotifyDataCountChanged(deletedIndex);
212     MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
213 }
214 
OnDataChanged(size_t index)215 void LazyForEachNode::OnDataChanged(size_t index)
216 {
217     auto changeIndex = static_cast<int32_t>(index);
218     if ((changeIndex < startIndex_) || (changeIndex > endIndex_)) {
219         LOGI("changeIndex is out of range, ignored, %{public}d, %{public}d, %{public}d", changeIndex, startIndex_,
220             endIndex_);
221         return;
222     }
223     auto iter = ids_.begin();
224     std::advance(iter, index - startIndex_);
225     *iter = std::nullopt;
226     MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
227 }
228 
OnDataMoved(size_t from,size_t to)229 void LazyForEachNode::OnDataMoved(size_t from, size_t to)
230 {
231     auto fromIndex = static_cast<int32_t>(from);
232     auto toIndex = static_cast<int32_t>(to);
233     auto fromOutOfRange = (fromIndex < startIndex_) || (fromIndex > endIndex_);
234     auto toOutOfRange = (toIndex < startIndex_) || (toIndex > endIndex_);
235     if (fromOutOfRange && toOutOfRange) {
236         LOGI("both out of range, ignored");
237         return;
238     }
239     NotifyDataCountChanged(startIndex_ + std::min(from, to));
240     if (fromOutOfRange && !toOutOfRange) {
241         auto iter = ids_.begin();
242         std::advance(iter, toIndex - startIndex_);
243         *iter = std::nullopt;
244         MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
245         return;
246     }
247     if (!fromOutOfRange && toOutOfRange) {
248         auto iter = ids_.begin();
249         std::advance(iter, fromIndex - startIndex_);
250         *iter = std::nullopt;
251         MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
252         return;
253     }
254     auto formIter = ids_.begin();
255     std::advance(formIter, fromIndex - startIndex_);
256     auto toIter = ids_.begin();
257     std::advance(toIter, toIndex - startIndex_);
258     auto temp = *formIter;
259     *formIter = *toIter;
260     *toIter = temp;
261     MarkNeedFrameFlushDirty(PROPERTY_UPDATE_MEASURE_SELF_AND_PARENT);
262 }
263 
NotifyDataCountChanged(size_t index)264 void LazyForEachNode::NotifyDataCountChanged(size_t index)
265 {
266     auto parent = GetParent();
267     if (parent) {
268         parent->ChildrenUpdatedFrom(static_cast<int32_t>(index));
269     }
270 }
271 } // namespace OHOS::Ace::NG
272