• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_PATTERNS_SWIPER_SWIPER_EVENT_HUB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_EVENT_HUB_H
18 
19 #include <algorithm>
20 #include <memory>
21 
22 #include "base/memory/ace_type.h"
23 #include "core/components_ng/event/event_hub.h"
24 #include "core/components_ng/pattern/swiper/swiper_model.h"
25 
26 namespace OHOS::Ace::NG {
27 
28 enum class Direction {
29     PRE = 0,
30     NEXT,
31 };
32 using ChangeIndicatorEvent = std::function<void()>;
33 using ChangeEvent = std::function<void(int32_t index)>;
34 using ChangeEventPtr = std::shared_ptr<ChangeEvent>;
35 using ChangeDoneEvent = std::function<void()>;
36 
37 class SwiperEventHub : public EventHub {
38     DECLARE_ACE_TYPE(SwiperEventHub, EventHub)
39 
40 public:
41     SwiperEventHub() = default;
42     ~SwiperEventHub() override = default;
43 
44     /* Using shared_ptr to enable event modification without adding again */
AddOnChangeEvent(const ChangeEventPtr & changeEvent)45     void AddOnChangeEvent(const ChangeEventPtr& changeEvent)
46     {
47         changeEvents_.emplace_back(changeEvent);
48     }
49 
SetIndicatorOnChange(ChangeIndicatorEvent && changeEvent)50     void SetIndicatorOnChange(ChangeIndicatorEvent&& changeEvent)
51     {
52         changeIndicatorEvent_ = std::move(changeEvent);
53     }
54 
SetChangeDoneEvent(ChangeDoneEvent && changeDoneEvent)55     void SetChangeDoneEvent(ChangeDoneEvent&& changeDoneEvent)
56     {
57         changeDoneEvent_ = std::move(changeDoneEvent);
58     }
59 
SetAnimationStartEvent(AnimationStartEvent && animationStartEvent)60     void SetAnimationStartEvent(AnimationStartEvent&& animationStartEvent)
61     {
62         animationStartEvent_ = std::move(animationStartEvent);
63     }
64 
SetAnimationEndEvent(AnimationEndEvent && animationEndEvent)65     void SetAnimationEndEvent(AnimationEndEvent&& animationEndEvent)
66     {
67         animationEndEvent_ = std::move(animationEndEvent);
68     }
69 
SetGestureSwipeEvent(GestureSwipeEvent && gestureSwipeEvent)70     void SetGestureSwipeEvent(GestureSwipeEvent&& gestureSwipeEvent)
71     {
72         gestureSwipeEvent_ = std::move(gestureSwipeEvent);
73     }
74 
FireChangeDoneEvent(bool direction)75     void FireChangeDoneEvent(bool direction)
76     {
77         if (changeDoneEvent_) {
78             if (direction) {
79                 direction_ = Direction::NEXT;
80             } else {
81                 direction_ = Direction::PRE;
82             }
83             changeDoneEvent_();
84         }
85     }
86 
FireChangeEvent(int32_t index)87     void FireChangeEvent(int32_t index) const
88     {
89         if (!changeEvents_.empty()) {
90             std::for_each(
91                 changeEvents_.begin(), changeEvents_.end(), [index](const ChangeEventPtr& event) { (*event)(index); });
92         }
93     }
94 
FireIndicatorChangeEvent(int32_t index)95     void FireIndicatorChangeEvent(int32_t index) const
96     {
97         if (changeIndicatorEvent_) {
98             changeIndicatorEvent_();
99         }
100     }
101 
GetDirection()102     Direction GetDirection()
103     {
104         return direction_;
105     }
106 
FireAnimationStartEvent(int32_t index,int32_t targetIndex,const AnimationCallbackInfo & info)107     void FireAnimationStartEvent(int32_t index, int32_t targetIndex, const AnimationCallbackInfo& info) const
108     {
109         if (animationStartEvent_) {
110             animationStartEvent_(index, targetIndex, info);
111         }
112     }
113 
FireAnimationEndEvent(int32_t index,const AnimationCallbackInfo & info)114     void FireAnimationEndEvent(int32_t index, const AnimationCallbackInfo& info) const
115     {
116         if (animationEndEvent_) {
117             animationEndEvent_(index, info);
118         }
119     }
120 
FireGestureSwipeEvent(int32_t index,const AnimationCallbackInfo & info)121     void FireGestureSwipeEvent(int32_t index, const AnimationCallbackInfo& info) const
122     {
123         if (gestureSwipeEvent_) {
124             gestureSwipeEvent_(index, info);
125         }
126     }
127 
128 private:
129     Direction direction_;
130     std::list<ChangeEventPtr> changeEvents_;
131     ChangeDoneEvent changeDoneEvent_;
132     ChangeIndicatorEvent changeIndicatorEvent_;
133     AnimationStartEvent animationStartEvent_;
134     AnimationEndEvent animationEndEvent_;
135     GestureSwipeEvent gestureSwipeEvent_;
136 };
137 
138 } // namespace OHOS::Ace::NG
139 
140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_EVENT_HUB_H