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 #include "core/animation/animator_group.h"
17 
18 namespace OHOS::Ace {
19 
~AnimatorGroup()20 AnimatorGroup::~AnimatorGroup()
21 {
22     for (auto& info : animators_) {
23         if (info.first) {
24             info.first->RemoveStopListener(info.second);
25         }
26     }
27     animators_.clear();
28 }
29 
AddAnimator(const RefPtr<Animator> & animator)30 void AnimatorGroup::AddAnimator(const RefPtr<Animator>& animator)
31 {
32     if (animator) {
33         if (animators_.find(animator) == animators_.end()) {
34             BaseId::IdType id = animator->AddStopListener([weak = WeakClaim(this), animator] {
35                                     auto animatorGroup = weak.Upgrade();
36                                     if (animatorGroup) {
37                                         animatorGroup->OnAnimatorStop(animator);
38                                     }
39                                 });
40             animators_.emplace(animator, id);
41         }
42     }
43 }
44 
RemoveAnimator(const RefPtr<Animator> & animator)45 void AnimatorGroup::RemoveAnimator(const RefPtr<Animator>& animator)
46 {
47     if (animator) {
48         auto iter = animators_.find(animator);
49         if (iter != animators_.end()) {
50             animator->RemoveStopListener(iter->second);
51             animators_.erase(iter);
52         }
53     }
54 }
55 
Play()56 void AnimatorGroup::Play()
57 {
58     if (status_ == AnimatorGroupStatus::RUNNING) {
59         return;
60     }
61     status_ = AnimatorGroupStatus::RUNNING;
62     for (auto& info : animators_) {
63         if (info.first) {
64             runningAnimators_.emplace(info.first);
65             info.first->Play();
66         }
67     }
68 }
69 
Stop()70 void AnimatorGroup::Stop()
71 {
72     if (status_ == AnimatorGroupStatus::STOPPED) {
73         return;
74     }
75     status_ = AnimatorGroupStatus::STOPPED;
76     for (auto& info : animators_) {
77         if (info.first) {
78             info.first->Stop();
79         }
80     }
81 }
82 
OnAnimatorStop(const RefPtr<Animator> & animator)83 void AnimatorGroup::OnAnimatorStop(const RefPtr<Animator>& animator)
84 {
85     runningAnimators_.erase(animator);
86     if (runningAnimators_.empty()) {
87         if (status_ == AnimatorGroupStatus::RUNNING) {
88             status_ = AnimatorGroupStatus::STOPPED;
89             StatusListenable::NotifyStopListener();
90         }
91     }
92 }
93 
94 } // namespace OHOS::Ace