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_COMPONENTS_NG_PATTERN_RECYCLE_VIEW_RECYCLE_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RECYCLE_VIEW_RECYCLE_MANAGER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <optional>
22 #include <unordered_map>
23 
24 #include "base/memory/referenced.h"
25 #include "core/common/resource/resource_configuration.h"
26 
27 namespace OHOS::Ace::NG {
28 class CustomNodeBase;
29 // record config changes for recyled node.
30 struct RecycleNodeState {
31     ConfigurationChange config;
32     WeakPtr<CustomNodeBase> node;
MergeConfigRecycleNodeState33     void MergeConfig(const ConfigurationChange& cfg)
34     {
35         config.MergeConfig(cfg);
36     }
RecycleNodeStateRecycleNodeState37     RecycleNodeState(WeakPtr<CustomNodeBase>&& n) : node(std::move(n)) {}
38 };
39 // record recycled node id and mark recycle state
40 struct RecycleNodeInfo {
41     int32_t elemtId = -1;
42     bool hasBeenRecyled = false;
RecycleRecycleNodeInfo43     void Recycle(int32_t id)
44     {
45         elemtId = id;
46         hasBeenRecyled = true;
47     }
ReuseRecycleNodeInfo48     void Reuse()
49     {
50         elemtId = -1;
51         hasBeenRecyled = false;
52     }
53 };
54 class RecycleManager {
55 public:
56     static void Push(int32_t elmtId, WeakPtr<CustomNodeBase>&& node);
57     static void Pop(int32_t elmtId);
58     static void Erase(int32_t elmtId);
59     static void Notify(const ConfigurationChange& config);
60 
61 private:
62     void PushNode(int32_t elmtId, WeakPtr<CustomNodeBase>&& node);
63     void PopNode(int32_t elmtId);
64     void EraseNode(int32_t elmtId);
65     void NotifyConfigurationChange(const ConfigurationChange& config);
66     std::unordered_map<int32_t, std::unique_ptr<RecycleNodeState>> recyclePool_;
67 };
68 } // namespace OHOS::Ace::NG
69 
70 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RECYCLE_VIEW_RECYCLE_MANAGER_H
71