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/common/recorder/event_recorder.h" 24 #include "core/common/recorder/node_data_cache.h" 25 #include "core/components_ng/base/frame_node.h" 26 #include "core/components_ng/event/event_hub.h" 27 #include "core/components_ng/pattern/swiper/swiper_model.h" 28 29 namespace OHOS::Ace::NG { 30 31 enum class Direction { 32 PRE = 0, 33 NEXT, 34 }; 35 using ChangeIndicatorEvent = std::function<void()>; 36 using ChangeEvent = std::function<void(int32_t index)>; 37 using ChangeEventPtr = std::shared_ptr<ChangeEvent>; 38 using ChangeDoneEvent = std::function<void()>; 39 40 class SwiperEventHub : public EventHub { 41 DECLARE_ACE_TYPE(SwiperEventHub, EventHub) 42 43 public: 44 SwiperEventHub() = default; 45 ~SwiperEventHub() override = default; 46 47 /* Using shared_ptr to enable event modification without adding again */ AddOnChangeEvent(const ChangeEventPtr & changeEvent)48 void AddOnChangeEvent(const ChangeEventPtr& changeEvent) 49 { 50 changeEvents_.emplace_back(changeEvent); 51 } 52 SetIndicatorOnChange(ChangeIndicatorEvent && changeEvent)53 void SetIndicatorOnChange(ChangeIndicatorEvent&& changeEvent) 54 { 55 changeIndicatorEvent_ = std::move(changeEvent); 56 } 57 SetChangeDoneEvent(ChangeDoneEvent && changeDoneEvent)58 void SetChangeDoneEvent(ChangeDoneEvent&& changeDoneEvent) 59 { 60 changeDoneEvent_ = std::move(changeDoneEvent); 61 } 62 AddAnimationStartEvent(const AnimationStartEventPtr & animationStartEvent)63 void AddAnimationStartEvent(const AnimationStartEventPtr& animationStartEvent) 64 { 65 animationStartEvents_.emplace_back(animationStartEvent); 66 } 67 AddAnimationEndEvent(const AnimationEndEventPtr & animationEndEvent)68 void AddAnimationEndEvent(const AnimationEndEventPtr& animationEndEvent) 69 { 70 animationEndEvents_.emplace_back(animationEndEvent); 71 } 72 SetGestureSwipeEvent(GestureSwipeEvent && gestureSwipeEvent)73 void SetGestureSwipeEvent(GestureSwipeEvent&& gestureSwipeEvent) 74 { 75 gestureSwipeEvent_ = std::move(gestureSwipeEvent); 76 } 77 FireChangeDoneEvent(bool direction)78 void FireChangeDoneEvent(bool direction) 79 { 80 if (changeDoneEvent_) { 81 if (direction) { 82 direction_ = Direction::NEXT; 83 } else { 84 direction_ = Direction::PRE; 85 } 86 changeDoneEvent_(); 87 } 88 } 89 FireChangeEvent(int32_t index)90 void FireChangeEvent(int32_t index) const 91 { 92 if (!changeEvents_.empty()) { 93 std::for_each( 94 changeEvents_.begin(), changeEvents_.end(), [index](const ChangeEventPtr& changeEvent) { 95 auto event = *changeEvent; 96 event(index); 97 }); 98 } 99 100 if (Recorder::EventRecorder::Get().IsComponentRecordEnable()) { 101 Recorder::EventParamsBuilder builder; 102 auto host = GetFrameNode(); 103 if (host) { 104 auto id = host->GetInspectorIdValue(""); 105 builder.SetId(id).SetType(host->GetHostTag()).SetDescription(host->GetAutoEventParamValue("")); 106 if (!id.empty()) { 107 Recorder::NodeDataCache::Get().PutInt(id, index); 108 } 109 } 110 builder.SetIndex(index); 111 Recorder::EventRecorder::Get().OnChange(std::move(builder)); 112 } 113 } 114 FireIndicatorChangeEvent(int32_t index)115 void FireIndicatorChangeEvent(int32_t index) const 116 { 117 if (changeIndicatorEvent_) { 118 changeIndicatorEvent_(); 119 } 120 } 121 GetDirection()122 Direction GetDirection() 123 { 124 return direction_; 125 } 126 FireAnimationStartEvent(int32_t index,int32_t targetIndex,const AnimationCallbackInfo & info)127 void FireAnimationStartEvent(int32_t index, int32_t targetIndex, const AnimationCallbackInfo& info) const 128 { 129 if (!animationStartEvents_.empty()) { 130 std::for_each(animationStartEvents_.begin(), animationStartEvents_.end(), 131 [index, targetIndex, info](const AnimationStartEventPtr& animationStartEvent) { 132 auto event = *animationStartEvent; 133 event(index, targetIndex, info); 134 }); 135 } 136 } 137 FireAnimationEndEvent(int32_t index,const AnimationCallbackInfo & info)138 void FireAnimationEndEvent(int32_t index, const AnimationCallbackInfo& info) const 139 { 140 if (!animationEndEvents_.empty()) { 141 std::for_each(animationEndEvents_.begin(), animationEndEvents_.end(), 142 [index, info](const AnimationEndEventPtr& animationEndEvent) { 143 auto event = *animationEndEvent; 144 event(index, info); 145 }); 146 } 147 } 148 FireGestureSwipeEvent(int32_t index,const AnimationCallbackInfo & info)149 void FireGestureSwipeEvent(int32_t index, const AnimationCallbackInfo& info) const 150 { 151 if (gestureSwipeEvent_) { 152 // gestureSwipeEvent_ may be overwrite in its invoke, so copy it first 153 auto event = gestureSwipeEvent_; 154 event(index, info); 155 } 156 } 157 158 private: 159 Direction direction_; 160 std::list<ChangeEventPtr> changeEvents_; 161 ChangeDoneEvent changeDoneEvent_; 162 ChangeIndicatorEvent changeIndicatorEvent_; 163 std::list<AnimationStartEventPtr> animationStartEvents_; 164 std::list<AnimationEndEventPtr> animationEndEvents_; 165 GestureSwipeEvent gestureSwipeEvent_; 166 }; 167 168 } // namespace OHOS::Ace::NG 169 170 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SWIPER_SWIPER_EVENT_HUB_H