• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_FOR_EACH_NODE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_FOR_EACH_NODE_H
18 
19 #include <list>
20 #include <optional>
21 #include <stdint.h>
22 #include <string>
23 #include <unordered_set>
24 #include <utility>
25 
26 #include "base/utils/utils.h"
27 #include "core/common/resource/resource_configuration.h"
28 #include "core/components_ng/base/frame_node.h"
29 #include "core/components_ng/base/ui_node.h"
30 #include "core/components_ng/syntax/lazy_for_each_builder.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 
33 namespace OHOS::Ace::NG {
34 
35 class ACE_EXPORT LazyForEachNode : public UINode, public V2::DataChangeListener {
36     DECLARE_ACE_TYPE(LazyForEachNode, UINode);
37 
38 public:
39     static RefPtr<LazyForEachNode> GetOrCreateLazyForEachNode(
40         int32_t nodeId, const RefPtr<LazyForEachBuilder>& forEachBuilder);
41 
LazyForEachNode(int32_t nodeId,const RefPtr<LazyForEachBuilder> & forEachBuilder)42     LazyForEachNode(int32_t nodeId, const RefPtr<LazyForEachBuilder>& forEachBuilder)
43         : UINode(V2::JS_LAZY_FOR_EACH_ETS_TAG, nodeId, false), builder_(forEachBuilder)
44     {}
45 
~LazyForEachNode()46     ~LazyForEachNode() {
47         CHECK_NULL_VOID(builder_);
48         builder_->UnregisterDataChangeListener(this);
49         builder_->ClearAllOffscreenNode();
50         isRegisterListener_ = false;
51     }
52 
IsAtomicNode()53     bool IsAtomicNode() const override
54     {
55         return false;
56     }
57 
FrameCount()58     int32_t FrameCount() const override
59     {
60         return builder_ ? builder_->GetTotalCount() : 0;
61     }
62 
63     void AdjustLayoutWrapperTree(const RefPtr<LayoutWrapperNode>& parent, bool forceMeasure, bool forceLayout) override;
64 
65     void UpdateLazyForEachItems(int32_t newStartIndex, int32_t newEndIndex,
66         std::list<std::optional<std::string>>&& nodeIds,
67         std::unordered_map<int32_t, std::optional<std::string>>&& cachedItems);
68 
69     void OnDataReloaded() override;
70     void OnDataAdded(size_t index) override;
71     void OnDataDeleted(size_t index) override;
72     void OnDataChanged(size_t index) override;
73     void OnDataMoved(size_t from, size_t to) override;
74 
75     void PostIdleTask(std::list<int32_t>&& items, const std::optional<LayoutConstraintF>& itemConstraint = std::nullopt,
76         bool longPredictTask = false);
77 
SetRequestLongPredict(bool requestLongPredict)78     void SetRequestLongPredict(bool requestLongPredict)
79     {
80         requestLongPredict_ = requestLongPredict;
81     }
82 
SetFlagForGeneratedItem(PropertyChangeFlag propertyChangeFlag)83     void SetFlagForGeneratedItem(PropertyChangeFlag propertyChangeFlag)
84     {
85         builder_->SetFlagForGeneratedItem(propertyChangeFlag);
86     }
87 
SetIsLoop(bool isLoop)88     void SetIsLoop(bool isLoop)
89     {
90         isLoop_ = isLoop;
91         if (builder_) {
92             builder_->SetIsLoop(isLoop);
93         }
94     }
95 
GetIsLoop()96     bool GetIsLoop() const
97     {
98         return isLoop_;
99     }
100     void PostIdleTask();
101     void OnConfigurationUpdate(const ConfigurationChange& configurationChange) override;
102     void MarkNeedSyncRenderTree(bool needRebuild = false) override;
103 
104     void BuildAllChildren();
105     RefPtr<UINode> GetFrameChildByIndex(uint32_t index, bool needBuild) override;
106     void DoRemoveChildInRenderTree(uint32_t index, bool isAll) override;
107     void DoSetActiveChildRange(int32_t start, int32_t end) override;
108 
109     const std::list<RefPtr<UINode>>& GetChildren() const override;
OnSetCacheCount(int32_t cacheCount,const std::optional<LayoutConstraintF> & itemConstraint)110     void OnSetCacheCount(int32_t cacheCount, const std::optional<LayoutConstraintF>& itemConstraint) override
111     {
112         itemConstraint_ = itemConstraint;
113         if (builder_) {
114             builder_->SetCacheCount(cacheCount);
115         }
116     }
117     void SetJSViewActive(bool active = true) override
118     {
119         if (builder_) {
120             builder_->SetJSViewActive(active);
121             isActive_ = active;
122         }
123     }
124 
125     int32_t GetIndexByUINode(const RefPtr<UINode>& uiNode) const;
126 
127 private:
OnAttachToMainTree(bool recursive)128     void OnAttachToMainTree(bool recursive) override
129     {
130         UINode::OnAttachToMainTree(recursive);
131         CHECK_NULL_VOID(builder_);
132         if (!isRegisterListener_) {
133             builder_->RegisterDataChangeListener(Claim(this));
134             isRegisterListener_ = true;
135         }
136     }
137 
OnOffscreenProcess(bool recursive)138     void OnOffscreenProcess(bool recursive) override
139     {
140         UINode::OnOffscreenProcess(recursive);
141         CHECK_NULL_VOID(builder_);
142         if (!isRegisterListener_) {
143             builder_->RegisterDataChangeListener(Claim(this));
144             isRegisterListener_ = true;
145         }
146     }
147 
OnGenerateOneDepthVisibleFrameWithTransition(std::list<RefPtr<FrameNode>> & visibleList)148     void OnGenerateOneDepthVisibleFrameWithTransition(std::list<RefPtr<FrameNode>>& visibleList) override
149     {
150         // LazyForEachNode::GetChildren() may add some children to disappearingChildren_, execute earlier to ensure
151         // disappearingChildren_ is correct before calling GenerateOneDepthVisibleFrameWithTransition.
152         GetChildren();
153         UINode::GenerateOneDepthVisibleFrameWithTransition(visibleList);
154     }
155 
156     void NotifyDataCountChanged(int32_t index);
157 
158     // The index values of the start and end of the current children nodes and the corresponding keys.
159     std::list<std::optional<std::string>> ids_;
160     std::list<int32_t> predictItems_;
161     std::optional<LayoutConstraintF> itemConstraint_;
162     bool requestLongPredict_ = false;
163     bool isRegisterListener_ = false;
164     bool isLoop_ = false;
165 
166     mutable std::list<RefPtr<UINode>> children_;
167     mutable bool needPredict_ = false;
168     bool needMarkParent_ = true;
169     bool isActive_ = true;
170 
171     RefPtr<LazyForEachBuilder> builder_;
172 
173     ACE_DISALLOW_COPY_AND_MOVE(LazyForEachNode);
174 };
175 
176 } // namespace OHOS::Ace::NG
177 
178 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_FOR_EACH_NODE_H