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 #include "core/components_ng/pattern/recycle_view/recycle_manager.h"
17 
18 #include <cstdint>
19 #include <memory>
20 #include <optional>
21 #include <unistd.h>
22 #include <utility>
23 
24 #include "base/memory/ace_type.h"
25 #include "base/utils/utils.h"
26 #include "core/components_ng/base/ui_node.h"
27 #include "core/components_ng/pattern/custom/custom_node_base.h"
28 #include "core/pipeline_ng/pipeline_context.h"
29 
30 namespace OHOS::Ace::NG {
31 
Push(int32_t elmtId,WeakPtr<CustomNodeBase> && node)32 void RecycleManager::Push(int32_t elmtId, WeakPtr<CustomNodeBase> &&node)
33 {
34     auto context = PipelineContext::GetCurrentContext();
35     CHECK_NULL_VOID(context);
36     context->GetRecycleManager()->PushNode(elmtId, std::move(node));
37 }
38 
Pop(int32_t elmtId)39 void RecycleManager::Pop(int32_t elmtId)
40 {
41     auto context = PipelineContext::GetCurrentContext();
42     CHECK_NULL_VOID(context);
43     context->GetRecycleManager()->PopNode(elmtId);
44 }
45 
Erase(int32_t elmtId)46 void RecycleManager::Erase(int32_t elmtId)
47 {
48     auto context = PipelineContext::GetCurrentContext();
49     CHECK_NULL_VOID(context);
50     context->GetRecycleManager()->EraseNode(elmtId);
51 }
52 
Notify(const ConfigurationChange & config)53 void RecycleManager::Notify(const ConfigurationChange &config)
54 {
55     auto context = PipelineContext::GetCurrentContext();
56     CHECK_NULL_VOID(context);
57     context->GetRecycleManager()->NotifyConfigurationChange(config);
58 }
59 
PushNode(int32_t elmtId,WeakPtr<CustomNodeBase> && node)60 void RecycleManager::PushNode(int32_t elmtId, WeakPtr<CustomNodeBase>&& node)
61 {
62     recyclePool_.try_emplace(elmtId, std::make_unique<RecycleNodeState>(std::move(node)));
63 }
64 
PopNode(int32_t elmtId)65 void RecycleManager::PopNode(int32_t elmtId)
66 {
67     auto it = recyclePool_.find(elmtId);
68     CHECK_EQUAL_VOID(it, recyclePool_.end());
69     auto node = it->second->node.Upgrade();
70     if (!node) {
71         recyclePool_.erase(it);
72         return;
73     }
74     if (it->second->config.IsNeedUpdate()) {
75         AceType::DynamicCast<UINode>(node)->UpdateConfigurationUpdate(it->second->config);
76     }
77     recyclePool_.erase(it);
78 }
79 
EraseNode(int32_t elmtId)80 void RecycleManager::EraseNode(int32_t elmtId)
81 {
82     recyclePool_.erase(elmtId);
83 }
84 
NotifyConfigurationChange(const ConfigurationChange & config)85 void RecycleManager::NotifyConfigurationChange(const ConfigurationChange& config)
86 {
87     for (auto& state : recyclePool_) {
88         state.second->MergeConfig(config);
89     }
90 }
91 } // namespace OHOS::Ace::NG
92