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