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_ANIMATION_SPRING_ADAPTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_ADAPTER_H
18 
19 #include "base/memory/ace_type.h"
20 
21 namespace OHOS::Ace {
22 
23 class SpringNode;
24 
25 class SpringAdapter : public AceType {
26     DECLARE_ACE_TYPE(SpringAdapter, AceType);
27 
28 public:
29     class AdapterObserve : public AceType {
30         DECLARE_ACE_TYPE(AdapterObserve, AceType);
31 
32     public:
33         virtual void OnControlNodeChange() = 0;
34         virtual void OnNodeAdd(RefPtr<SpringNode>& node) = 0;
35         virtual void OnNodeDelete(RefPtr<SpringNode>& node) = 0;
36     };
37 
SetObserve(WeakPtr<AdapterObserve> observe)38     void SetObserve(WeakPtr<AdapterObserve> observe)
39     {
40         observe_ = observe;
41     }
42 
43     virtual RefPtr<SpringNode> GetNext(const RefPtr<SpringNode>& node) = 0;
44 
45     virtual int32_t GetSize() const = 0;
46 
47     virtual RefPtr<SpringNode> GetControlNode() const = 0;
48 
49     virtual int32_t GetControlIndex() const = 0;
50 
51     virtual RefPtr<SpringNode> GetNode(int32_t index) const = 0;
52 
NotifyControlIndexChange()53     void NotifyControlIndexChange()
54     {
55         if (observe_.Upgrade()) {
56             observe_.Upgrade()->OnControlNodeChange();
57         }
58     }
59 
NotifyNodeAdd(RefPtr<SpringNode> & node)60     void NotifyNodeAdd(RefPtr<SpringNode>& node)
61     {
62         if (observe_.Upgrade()) {
63             observe_.Upgrade()->OnNodeAdd(node);
64         }
65     }
66 
NotifyNodeDelete(RefPtr<SpringNode> & node)67     void NotifyNodeDelete(RefPtr<SpringNode>& node)
68     {
69         if (observe_.Upgrade()) {
70             observe_.Upgrade()->OnNodeDelete(node);
71         }
72     }
73 
74     virtual void FlushAnimation() = 0;
75 
TickAnimation(uint64_t timestamp)76     void TickAnimation(uint64_t timestamp)
77     {
78         if (timestamp_ == timestamp) {
79             return;
80         }
81         FlushAnimation();
82         timestamp_ = timestamp;
83     }
84 
85 private:
86     WeakPtr<AdapterObserve> observe_;
87     uint64_t timestamp_ = 0;
88 };
89 
90 } // namespace OHOS::Ace
91 
92 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_ADAPTER_H
93