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_LAYOUT_WRAPPER_BUILDER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_LAYOUT_WRAPPER_BUILDER_H
18 
19 #include <optional>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/noncopyable.h"
23 #include "core/components_ng/layout/layout_wrapper_builder.h"
24 #include "core/components_ng/syntax/lazy_for_each_builder.h"
25 
26 namespace OHOS::Ace::NG {
27 class LazyForEachNode;
28 
29 class LazyLayoutWrapperBuilder : public LayoutWrapperBuilder {
30     DECLARE_ACE_TYPE(LazyLayoutWrapperBuilder, LayoutWrapperBuilder)
31 public:
32     LazyLayoutWrapperBuilder(const RefPtr<LazyForEachBuilder>& builder, const WeakPtr<LazyForEachNode>& host);
33     ~LazyLayoutWrapperBuilder() override = default;
34 
35     void SwapDirtyAndUpdateBuildCache() override;
36 
37     void AdjustGridOffset() override;
38 
UpdateIndexRange(int32_t startIndex,int32_t endIndex,const std::list<std::optional<std::string>> & ids)39     void UpdateIndexRange(int32_t startIndex, int32_t endIndex, const std::list<std::optional<std::string>>& ids)
40     {
41         auto size = static_cast<int32_t>(ids.size());
42         if ((size != 0) && (size != (endIndex - startIndex + 1))) {
43             LOGE("fail to update index range due to ides not match!, %{public}d, %{public}d, %{public}d", startIndex,
44                 endIndex, size);
45             return;
46         }
47 
48         preStartIndex_ = startIndex;
49         preEndIndex_ = endIndex;
50         preNodeIds_ = ids;
51     }
52 
UpdateForceFlag(bool forceMeasure,bool forceLayout)53     void UpdateForceFlag(bool forceMeasure, bool forceLayout)
54     {
55         forceMeasure_ = forceMeasure;
56         forceLayout_ = forceLayout;
57     }
58 
59     const std::list<RefPtr<LayoutWrapper>>& GetCachedChildLayoutWrapper() override;
60 
61     void SetLazySwiper(bool flag = true)
62     {
63         lazySwiper_ = flag;
64     }
65 
66 protected:
67     int32_t OnGetTotalCount() override;
68     RefPtr<LayoutWrapper> OnGetOrCreateWrapperByIndex(int32_t index) override;
69     const std::list<RefPtr<LayoutWrapper>>& OnExpandChildLayoutWrapper() override;
70 
71 private:
72     RefPtr<LazyForEachBuilder> builder_;
73     WeakPtr<LazyForEachNode> host_;
74 
75     int32_t preStartIndex_ = -1;
76     int32_t preEndIndex_ = -1;
77     std::list<std::optional<std::string>> preNodeIds_;
78 
79     std::optional<int32_t> startIndex_;
80     std::optional<int32_t> endIndex_;
81     std::list<std::optional<std::string>> nodeIds_;
82 
83     std::optional<std::string> GetKeyByIndexFromPreNodes(int32_t index);
84     RefPtr<LayoutWrapper> OnGetOrCreateWrapperByIndexLegacy(int32_t index);
85 
86     std::list<RefPtr<LayoutWrapper>> childWrappers_;
87 
88     bool forceMeasure_ = false;
89     bool forceLayout_ = false;
90     bool lazySwiper_ = false;
91 
92     ACE_DISALLOW_COPY_AND_MOVE(LazyLayoutWrapperBuilder);
93 };
94 
95 } // namespace OHOS::Ace::NG
96 
97 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_SYNTAX_LAZY_LAYOUT_WRAPPER_BUILDER_H
98