1 /*
2  * Copyright (c) 2024 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_INTERFACES_NATIVE_NODE_NODE_ADAPTER_IMPL_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_INTERFACES_NATIVE_NODE_NODE_ADAPTER_IMPL_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <string>
22 #include <vector>
23 
24 #include "base/error/error_code.h"
25 #include "base/memory/ace_type.h"
26 #include "base/memory/referenced.h"
27 #include "core/components_ng/base/ui_node.h"
28 #include "core/components_ng/syntax/lazy_for_each_builder.h"
29 #include "core/interfaces/arkoala/arkoala_api.h"
30 #include "core/interfaces/cjui/cjui_api.h"
31 
32 namespace OHOS::Ace::NG {
33 class NativeLazyForEachBuilder : public LazyForEachBuilder {
DECLARE_ACE_TYPE(NativeLazyForEachBuilder,LazyForEachBuilder)34     DECLARE_ACE_TYPE(NativeLazyForEachBuilder, LazyForEachBuilder)
35 public:
36     // used in ArkTS side.
37     void ReleaseChildGroupById(const std::string& id) override {}
38 
39     void RegisterDataChangeListener(const RefPtr<V2::DataChangeListener>& listener) override;
40 
41     void UnregisterDataChangeListener(V2::DataChangeListener* listener) override;
42 
SetNodeTotalCount(ArkUI_Uint32 nodeCount)43     void SetNodeTotalCount(ArkUI_Uint32 nodeCount)
44     {
45         totalCount_ = nodeCount;
46     }
47 
GetNodeTotalCount()48     ArkUI_Uint32 GetNodeTotalCount() const
49     {
50         return totalCount_;
51     }
52 
SetUserData(void * userData)53     void SetUserData(void* userData)
54     {
55         userData_ = userData;
56     }
57 
SetReceiver(void (* receiver)(ArkUINodeAdapterEvent * event))58     void SetReceiver(void (*receiver)(ArkUINodeAdapterEvent* event))
59     {
60         receiver_ = receiver;
61     }
62 
NotifyItemReloaded()63     ArkUI_Int32 NotifyItemReloaded()
64     {
65         if (listener_) {
66             listener_->OnDataReloaded();
67             return ERROR_CODE_NO_ERROR;
68         }
69         return ERROR_CODE_NATIVE_IMPL_NODE_ADAPTER_NO_LISTENER_ERROR;
70     }
71 
NotifyItemChanged(ArkUI_Uint32 startPosition,ArkUI_Uint32 itemCount)72     ArkUI_Int32 NotifyItemChanged(ArkUI_Uint32 startPosition, ArkUI_Uint32 itemCount)
73     {
74         if (listener_) {
75             listener_->OnDataBulkChanged(startPosition, itemCount);
76             return ERROR_CODE_NO_ERROR;
77         }
78         return ERROR_CODE_NATIVE_IMPL_NODE_ADAPTER_NO_LISTENER_ERROR;
79     }
80 
NotifyItemRemoved(ArkUI_Uint32 startPosition,ArkUI_Uint32 itemCount)81     ArkUI_Int32 NotifyItemRemoved(ArkUI_Uint32 startPosition, ArkUI_Uint32 itemCount)
82     {
83         if (listener_) {
84             listener_->OnDataBulkDeleted(startPosition, itemCount);
85             return ERROR_CODE_NO_ERROR;
86         }
87         return ERROR_CODE_NATIVE_IMPL_NODE_ADAPTER_NO_LISTENER_ERROR;
88     }
NotifyItemInserted(ArkUI_Uint32 startPosition,ArkUI_Uint32 itemCount)89     ArkUI_Int32 NotifyItemInserted(ArkUI_Uint32 startPosition, ArkUI_Uint32 itemCount)
90     {
91         if (listener_) {
92             listener_->OnDataBulkAdded(startPosition, itemCount);
93             return ERROR_CODE_NO_ERROR;
94         }
95         return ERROR_CODE_NATIVE_IMPL_NODE_ADAPTER_NO_LISTENER_ERROR;
96     }
NotifyItemMoved(ArkUI_Uint32 from,ArkUI_Uint32 to)97     ArkUI_Int32 NotifyItemMoved(ArkUI_Uint32 from, ArkUI_Uint32 to)
98     {
99         if (listener_) {
100             listener_->OnDataMoveToNewPlace(from, to);
101             return ERROR_CODE_NO_ERROR;
102         }
103         return ERROR_CODE_NATIVE_IMPL_NODE_ADAPTER_NO_LISTENER_ERROR;
104     }
105 
106     // should manager the array memory.
107     ArkUI_Int32 GetAllItem(ArkUINodeHandle** items, ArkUI_Uint32* size);
108 
SetHostHandle(ArkUINodeAdapterHandle handle)109     void SetHostHandle(ArkUINodeAdapterHandle handle)
110     {
111         handle_ = handle;
112     }
113 
GetHostHandle()114     ArkUINodeAdapterHandle GetHostHandle() const
115     {
116         return handle_;
117     }
118 
SetNeedUpdateEvent(bool needUpdateEvent)119     void SetNeedUpdateEvent(bool needUpdateEvent)
120     {
121         needUpdateEvent_ = needUpdateEvent;
122     }
123 
124 protected:
125     int32_t OnGetTotalCount() override;
126 
127     LazyForEachChild OnGetChildByIndex(
128         int32_t index, std::unordered_map<std::string, LazyForEachCacheChild>& cachedItems) override;
129 
130     void OnItemDeleted(UINode* node, const std::string& key) override;
131 
DeleteExpiringItemImmediately()132     bool DeleteExpiringItemImmediately() override
133     {
134         return true;
135     }
136 
137     // used in ArkTS side for tabs.
OnGetChildByIndexNew(int32_t index,std::map<int32_t,LazyForEachChild> & cachedItems,std::unordered_map<std::string,LazyForEachCacheChild> & expiringItems)138     LazyForEachChild OnGetChildByIndexNew(int32_t index, std::map<int32_t, LazyForEachChild>& cachedItems,
139         std::unordered_map<std::string, LazyForEachCacheChild>& expiringItems) override
140     {
141         return {};
142     }
143 
144     // used in ArkTS side for tabs.
OnExpandChildrenOnInitialInNG()145     void OnExpandChildrenOnInitialInNG() override {}
146 
147     // used in ArkTS side.
NotifyDataChanged(size_t index,const RefPtr<UINode> & lazyForEachNode,bool isRebuild)148     void NotifyDataChanged(size_t index, const RefPtr<UINode>& lazyForEachNode, bool isRebuild) override {}
149 
150     // used in ArkTS side.
NotifyDataDeleted(const RefPtr<UINode> & lazyForEachNode,size_t index,bool removeIds)151     void NotifyDataDeleted(const RefPtr<UINode>& lazyForEachNode, size_t index, bool removeIds) override {}
152 
153     // used in ArkTS side.
NotifyDataAdded(size_t index)154     void NotifyDataAdded(size_t index) override {}
155 
156     // used in ArkTS side.
KeepRemovedItemInCache(NG::LazyForEachChild node,std::unordered_map<std::string,NG::LazyForEachCacheChild> & cachedItems)157     void KeepRemovedItemInCache(
158         NG::LazyForEachChild node, std::unordered_map<std::string, NG::LazyForEachCacheChild>& cachedItems) override
159     {}
160 
161     void FlushDirtyPropertyNodes(const RefPtr<UINode>& node);
162 
163 private:
164     V2::DataChangeListener* listener_;
165     uint32_t totalCount_ = 0;
166     void* userData_ = nullptr;
167     void (*receiver_)(ArkUINodeAdapterEvent* event) = nullptr;
168     bool needUpdateEvent_ = false;
169     ArkUINodeAdapterHandle handle_ = nullptr;
170 };
171 
172 class UINodeAdapter : public AceType {
173 public:
174     explicit UINodeAdapter(ArkUINodeAdapterHandle handle);
175     ~UINodeAdapter() override;
176 
177     void OnEventReceived(ArkUINodeAdapterEvent* event);
178 
SetOnAttachToNodeFunc(std::function<void (ArkUINodeHandle)> && func)179     void SetOnAttachToNodeFunc(std::function<void(ArkUINodeHandle)>&& func)
180     {
181         attachToNodeFunc_ = func;
182     }
183 
SetOnDetachFromNodeFunc(std::function<void ()> && func)184     void SetOnDetachFromNodeFunc(std::function<void()>&& func)
185     {
186         detachFromNodeFunc_ = func;
187     }
188 
SetOnGetChildIdFunc(std::function<int32_t (uint32_t)> && func)189     void SetOnGetChildIdFunc(std::function<int32_t(uint32_t)>&& func)
190     {
191         getChildIdFunc_ = func;
192     }
193 
SetOnCreateNewChild(std::function<ArkUINodeHandle (uint32_t)> && func)194     void SetOnCreateNewChild(std::function<ArkUINodeHandle(uint32_t)>&& func)
195     {
196         createNewChildFunc_ = func;
197     }
198 
SetOnDisposeChild(std::function<void (ArkUINodeHandle,int32_t)> && func)199     void SetOnDisposeChild(std::function<void(ArkUINodeHandle, int32_t)>&& func)
200     {
201         disposeChildFunc_ = func;
202     }
203 
SetOnUpdateChind(std::function<void (ArkUINodeHandle,int32_t)> && func)204     void SetOnUpdateChind(std::function<void(ArkUINodeHandle, int32_t)>&& func)
205     {
206         updateChildFunc_ = func;
207     }
208 
GetHandle()209     ArkUINodeAdapterHandle GetHandle() const
210     {
211         return handle_;
212     }
213 
214     void SetTotalNodeCount(uint32_t count);
215     uint32_t GetTotalNodeCount() const;
216     void NotifyItemReloaded();
217     void NotifyItemChanged(uint32_t start, uint32_t count);
218     void NotifyItemRemoved(uint32_t start, uint32_t count);
219     void NotifyItemInserted(uint32_t start, uint32_t count);
220     void NotifyItemMoved(uint32_t from, uint32_t to);
221     std::vector<ArkUINodeHandle> GetAllItems();
222 
223 private:
224     ArkUINodeAdapterHandle handle_;
225     std::function<void(ArkUINodeHandle)> attachToNodeFunc_;
226     std::function<void()> detachFromNodeFunc_;
227     std::function<int32_t(uint32_t)> getChildIdFunc_;
228     std::function<ArkUINodeHandle(uint32_t)> createNewChildFunc_;
229     std::function<void(ArkUINodeHandle, int32_t)> disposeChildFunc_;
230     std::function<void(ArkUINodeHandle, int32_t)> updateChildFunc_;
231 };
232 
233 } // namespace OHOS::Ace::NG
234 
235 namespace OHOS::Ace::NodeAdapter {
236 const ArkUINodeAdapterAPI* GetNodeAdapterAPI();
237 const CJUINodeAdapterAPI* GetCJUINodeAdapterAPI();
238 } // namespace OHOS::Ace::NodeAdapter
239 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_INTERFACES_NATIVE_NODE_NODE_ADAPTER_IMPL_H
240