1 /*
2  * Copyright (c) 2023 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 #include "core/components_ng/pattern/recycle_view/recycle_dummy_node.h"
17 
18 #include "base/memory/ace_type.h"
19 #include "base/utils/utils.h"
20 #include "core/components_ng/pattern/custom/custom_node_base.h"
21 #include "core/components_v2/inspector/inspector_constants.h"
22 #include "core/pipeline/base/element_register.h"
23 
24 namespace OHOS::Ace::NG {
CreateRecycleDummyNode(int32_t nodeId)25 RefPtr<RecycleDummyNode> RecycleDummyNode::CreateRecycleDummyNode(int32_t nodeId)
26 {
27     auto node = MakeRefPtr<RecycleDummyNode>(nodeId);
28     return node;
29 }
30 
WrapRecycleDummyNode(const RefPtr<AceType> & customNode,int32_t nodeId)31 RefPtr<AceType> RecycleDummyNode::WrapRecycleDummyNode(const RefPtr<AceType>& customNode, int32_t nodeId)
32 {
33     auto node = CreateRecycleDummyNode(nodeId);
34     auto uiNode = AceType::DynamicCast<UINode>(customNode);
35     node->AddChild(uiNode);
36     return node;
37 }
38 
RecycleDummyNode(int32_t nodeId)39 RecycleDummyNode::RecycleDummyNode(int32_t nodeId) : UINode(V2::RECYCLE_VIEW_ETS_TAG, nodeId) {}
40 
~RecycleDummyNode()41 RecycleDummyNode::~RecycleDummyNode()
42 {
43     auto childCount = TotalChildCount();
44     if (childCount != 1) {
45         return;
46     }
47     auto child = GetFirstChild();
48     CHECK_NULL_VOID(child);
49     if (child->TotalChildCount() == 0) {
50         return;
51     }
52     child->DetachFromMainTree();
53     auto customNode = AceType::DynamicCast<CustomNodeBase>(child);
54     CHECK_NULL_VOID(customNode);
55     customNode->FireRecycleSelf();
56 }
57 } // namespace OHOS::Ace::NG
58