• 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/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<LayoutWrapper>& 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);
70 
71 private:
OnAttachToMainTree()72     void OnAttachToMainTree() override
73     {
74         CHECK_NULL_VOID(builder_);
75         builder_->RegisterDataChangeListener(Claim(this));
76     }
77 
OnDetachFromMainTree()78     void OnDetachFromMainTree() override
79     {
80         CHECK_NULL_VOID(builder_);
81         builder_->UnregisterDataChangeListener(Claim(this));
82     }
83 
84     void NotifyDataCountChanged(size_t index);
85 
86     // The index values of the start and end of the current children nodes and the corresponding keys.
87     int32_t startIndex_ = -1;
88     int32_t endIndex_ = -1;
89     std::list<std::optional<std::string>> ids_;
90     std::list<int32_t> predictItems_;
91     bool needPredict = false;
92 
93     RefPtr<LazyForEachBuilder> builder_;
94 
95     ACE_DISALLOW_COPY_AND_MOVE(LazyForEachNode);
96 };
97 
98 } // namespace OHOS::Ace::NG
99 
100 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_FOR_EACH_NODE_H
101