1 /*
2  * Copyright (c) 2021 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_LIST_LIST_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_ELEMENT_H
18 
19 #include <map>
20 
21 #include "core/components/list/render_list.h"
22 #include "core/pipeline/base/component_group_element.h"
23 
24 namespace OHOS::Ace {
25 
26 class ListElement : public ComponentGroupElement, public FocusGroup {
27     DECLARE_ACE_TYPE(ListElement, ComponentGroupElement, FocusGroup);
28 
29 public:
30     void PerformBuild() override;
31     void Update() override;
32 
33     bool RequestNextFocus(bool vertical, bool reverse, const Rect& rect) override;
34 
35     void MoveItemToViewPort(double position);
36 
37     void MoveItemGroupToViewPort(double position, double size);
38 
39     void SetGroupState(int32_t expandIndex, bool expand);
40 
41     void AddItemGroupFocusIndex(int32_t groupIndex, int32_t groupFocusIndex);
42 
43     int32_t GetItemGroupFocusIndex(int32_t groupIndex);
44 
NeedMoveFocusItem(bool need)45     void NeedMoveFocusItem(bool need)
46     {
47         needMoveFocusItem_ = need;
48     }
49 
50 private:
51     RefPtr<RenderNode> CreateRenderNode() override;
52     void ApplyRenderChild(const RefPtr<RenderElement>& renderChild) override;
53 
54     bool RecycleItem(int32_t index);
55     void RecycleByRange(int32_t& from, int32_t& to);
56     void RecycleByItems(const std::vector<int32_t>& items);
57     void ReleaseRecycledListItem(int32_t from, int32_t to);
58 
59     bool BuildListDataFromChild(int32_t index);
60     bool BuildDetachedFocusComponent();
61     void PreBuildListItems(int32_t index, const std::list<RefPtr<Component>>& newComponent, int32_t from);
62     void RetrieveListData(int32_t beginIndex, int32_t endIndex);
63     bool BuildListData(int32_t index);
64     bool BuildListComponent(const RefPtr<Component>& component);
65     void UpdateListItemElement(const RefPtr<Component>& component);
66     RefPtr<RenderNode> BuildStickyItem(int32_t index, bool next);
67     int32_t SearchStickyItem(int32_t index);
68     bool SupportStickyItem() const;
69     void ResetStickyItem();
70     void InitStickyFunc();
71 
72     void UpdateListElement();
73     void GetRefreshItems(bool& rebuild, int32_t& index);
74     void RebuildElements(int32_t tailIndex);
75     void PatchElements(bool rebuild);
76     void OnRefreshed();
77 
78     void UpdateCachedComponent();
79     int32_t AddToCache(const RefPtr<Component>& item, int32_t index, bool isDynamic = false);
80     void RemoveComposedChildFromMap(RefPtr<Element> element);
81 
82     RefPtr<RenderList> renderList_;
83 
84     using RequestItem = std::function<void(const std::string&, std::string&)>;
85     RequestItem requestItem_;
86     using RequestItemAsync = std::function<void(const std::string&)>;
87     RequestItemAsync requestItemAsync_;
88 
89     int32_t maxCount_ = 0;
90     int32_t cachedCount_ = 0;
91     int32_t beginIndex_ = LIST_PARAM_INVAID;
92     int32_t endIndex_ = LIST_PARAM_INVAID;
93     int32_t repeatedLength_ = 0;
94     // For the case of dynamically loading list-items, length_ = indexOffset_ + repeatedLength_ + tailLength_;
95     int32_t length_ = 0;
96     int32_t indexOffset_ = 0;
97     int32_t tailLength_ = 0;
98     bool accessibilityDisabled_ = false;
99 
100     int32_t preBuildCount_ = 0;
101     std::list<RefPtr<Component>> newListItems_;
102     std::map<int32_t, RefPtr<Component>> newListItemsMap_;
103     std::vector<RefPtr<Component>> needRefreshItems_;
104     std::vector<RefPtr<Component>> itemComponents_;
105     std::pair<int32_t, int32_t> itemVectorHit_ {-1, -1};
106     RefPtr<Component> listComponent_;
107     bool needRefresh_ = false;
108     bool isJsCard_ = false;
109 
110     bool building_ = false;
111     std::map<int32_t, RefPtr<Element>> itemElements_;
112 
113     bool needMoveFocusItem_ = false;
114     RefPtr<Element> stickyElement_;
115     RefPtr<Element> stickyNextElement_;
116 };
117 
118 } // namespace OHOS::Ace
119 
120 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_LIST_LIST_ELEMENT_H
121