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_COMPONENTS_SWIPER_SWIPER_CONTROLLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SWIPER_SWIPER_CONTROLLER_H 18 19 #include <functional> 20 #include <set> 21 22 #include "base/memory/ace_type.h" 23 #include "core/components/common/layout/constants.h" 24 25 namespace OHOS::Ace { 26 27 using CommonFunc = std::function<void()>; 28 using SwipeToImpl = std::function<void(const int32_t, bool)>; 29 using SwipeToWithoutAnimationImpl = std::function<void(const int32_t)>; 30 using TurnPageRateFunc = std::function<void(const int32_t, float)>; 31 using ChangeIndexImpl = std::function<void(const int32_t, bool)>; 32 using ChangeIndexWithModeImpl = std::function<void(const int32_t, SwiperAnimationMode)>; 33 using PreloadItemsFunc = std::function<void(const std::set<int32_t>)>; 34 using PreloadItemsFinishFunc = std::function<void(const int32_t, const std::string)>; 35 using OnChangeFunc = std::function<void(int32_t index)>; 36 37 class SwiperController : public virtual AceType { 38 DECLARE_ACE_TYPE(SwiperController, AceType); 39 40 public: 41 void SwipeTo(int32_t index, bool reverse = false) 42 { 43 if (swipeToImpl_) { 44 swipeToImpl_(index, reverse); 45 } 46 } 47 SetSwipeToImpl(const SwipeToImpl & swipeToImpl)48 void SetSwipeToImpl(const SwipeToImpl& swipeToImpl) 49 { 50 swipeToImpl_ = swipeToImpl; 51 } 52 SwipeToWithoutAnimation(int32_t index)53 void SwipeToWithoutAnimation(int32_t index) 54 { 55 if (swipeToWithoutAnimationImpl_) { 56 swipeToWithoutAnimationImpl_(index); 57 } 58 } 59 SetSwipeToWithoutAnimationImpl(const SwipeToWithoutAnimationImpl & swipeToWithoutAnimationImpl)60 void SetSwipeToWithoutAnimationImpl(const SwipeToWithoutAnimationImpl& swipeToWithoutAnimationImpl) 61 { 62 swipeToWithoutAnimationImpl_ = swipeToWithoutAnimationImpl; 63 } 64 ShowPrevious()65 void ShowPrevious() 66 { 67 if (showPrevImpl_) { 68 showPrevImpl_(); 69 } 70 } 71 SetShowPrevImpl(const CommonFunc & showPrevImpl)72 void SetShowPrevImpl(const CommonFunc& showPrevImpl) 73 { 74 showPrevImpl_ = showPrevImpl; 75 } 76 ShowNext()77 void ShowNext() 78 { 79 if (showNextImpl_) { 80 showNextImpl_(); 81 } 82 } 83 SetShowNextImpl(const CommonFunc & showNextImpl)84 void SetShowNextImpl(const CommonFunc& showNextImpl) 85 { 86 showNextImpl_ = showNextImpl; 87 } 88 ChangeIndex(int32_t index,bool useAnimation)89 void ChangeIndex(int32_t index, bool useAnimation) 90 { 91 if (changeIndexImpl_) { 92 changeIndexImpl_(index, useAnimation); 93 } 94 } 95 ChangeIndex(int32_t index,SwiperAnimationMode animationMode)96 void ChangeIndex(int32_t index, SwiperAnimationMode animationMode) 97 { 98 if (changeIndexWithModeImpl_) { 99 changeIndexWithModeImpl_(index, animationMode); 100 } 101 } 102 SetChangeIndexImpl(const ChangeIndexImpl & changeIndexImpl)103 void SetChangeIndexImpl(const ChangeIndexImpl& changeIndexImpl) 104 { 105 changeIndexImpl_ = changeIndexImpl; 106 } 107 SetChangeIndexWithModeImpl(const ChangeIndexWithModeImpl & changeIndexWithModeImpl)108 void SetChangeIndexWithModeImpl(const ChangeIndexWithModeImpl& changeIndexWithModeImpl) 109 { 110 changeIndexWithModeImpl_ = changeIndexWithModeImpl; 111 } 112 FinishAnimation()113 void FinishAnimation() const 114 { 115 if (finishImpl_) { 116 finishImpl_(); 117 } 118 } 119 SetFinishImpl(const CommonFunc & finishImpl)120 void SetFinishImpl(const CommonFunc& finishImpl) 121 { 122 finishImpl_ = finishImpl; 123 } 124 SetFinishCallback(const CommonFunc & onFinish)125 void SetFinishCallback(const CommonFunc& onFinish) 126 { 127 finishCallback_ = onFinish; 128 } 129 GetFinishCallback()130 const CommonFunc& GetFinishCallback() const 131 { 132 return finishCallback_; 133 } 134 HasInitialized()135 bool HasInitialized() const 136 { 137 return showPrevImpl_ && showNextImpl_ && finishImpl_; 138 } 139 SetTabBarFinishCallback(const CommonFunc & onTabBarFinish)140 void SetTabBarFinishCallback(const CommonFunc& onTabBarFinish) 141 { 142 tabBarFinishCallback_ = onTabBarFinish; 143 } 144 GetTabBarFinishCallback()145 const CommonFunc& GetTabBarFinishCallback() const 146 { 147 return tabBarFinishCallback_; 148 } 149 SetRemoveTabBarEventCallback(const CommonFunc & removeTabBarEventCallback)150 void SetRemoveTabBarEventCallback(const CommonFunc& removeTabBarEventCallback) 151 { 152 removeTabBarEventCallback_ = removeTabBarEventCallback; 153 } 154 GetRemoveTabBarEventCallback()155 const CommonFunc& GetRemoveTabBarEventCallback() const 156 { 157 return removeTabBarEventCallback_; 158 } 159 SetAddTabBarEventCallback(const CommonFunc & addTabBarEventCallback)160 void SetAddTabBarEventCallback(const CommonFunc& addTabBarEventCallback) 161 { 162 addTabBarEventCallback_ = addTabBarEventCallback; 163 } 164 GetAddTabBarEventCallback()165 const CommonFunc& GetAddTabBarEventCallback() const 166 { 167 return addTabBarEventCallback_; 168 } 169 SetRemoveSwiperEventCallback(const CommonFunc & removeSwiperEventCallback)170 void SetRemoveSwiperEventCallback(const CommonFunc& removeSwiperEventCallback) 171 { 172 removeSwiperEventCallback_ = removeSwiperEventCallback; 173 } 174 GetRemoveSwiperEventCallback()175 const CommonFunc& GetRemoveSwiperEventCallback() const 176 { 177 return removeSwiperEventCallback_; 178 } 179 SetAddSwiperEventCallback(const CommonFunc & addSwiperEventCallback)180 void SetAddSwiperEventCallback(const CommonFunc& addSwiperEventCallback) 181 { 182 addSwiperEventCallback_ = addSwiperEventCallback; 183 } 184 GetAddSwiperEventCallback()185 const CommonFunc& GetAddSwiperEventCallback() const 186 { 187 return addSwiperEventCallback_; 188 } 189 SetTurnPageRateCallback(const TurnPageRateFunc & turnPageRateCallback)190 void SetTurnPageRateCallback(const TurnPageRateFunc& turnPageRateCallback) 191 { 192 turnPageRateCallback_ = turnPageRateCallback; 193 } 194 GetTurnPageRateCallback()195 const TurnPageRateFunc& GetTurnPageRateCallback() const 196 { 197 return turnPageRateCallback_; 198 } 199 SetUpdateCubicCurveCallback(const CommonFunc & updateCubicCurveCallback)200 void SetUpdateCubicCurveCallback(const CommonFunc& updateCubicCurveCallback) 201 { 202 updateCubicCurveCallback_ = updateCubicCurveCallback; 203 } 204 GetUpdateCubicCurveCallback()205 const CommonFunc& GetUpdateCubicCurveCallback() const 206 { 207 return updateCubicCurveCallback_; 208 } 209 SetSurfaceChangeCallback(const CommonFunc & surfaceChangeCallback)210 void SetSurfaceChangeCallback(const CommonFunc& surfaceChangeCallback) 211 { 212 surfaceChangeCallback_ = surfaceChangeCallback; 213 } 214 GetSurfaceChangeCallback()215 const CommonFunc& GetSurfaceChangeCallback() const 216 { 217 return surfaceChangeCallback_; 218 } 219 SetPreloadFinishCallback(const PreloadItemsFinishFunc & preloadFinishCallback)220 void SetPreloadFinishCallback(const PreloadItemsFinishFunc& preloadFinishCallback) 221 { 222 preloadFinishCallback_ = preloadFinishCallback; 223 } 224 GetPreloadFinishCallback()225 const PreloadItemsFinishFunc& GetPreloadFinishCallback() const 226 { 227 return preloadFinishCallback_; 228 } 229 SetPreloadItemsImpl(const PreloadItemsFunc & preloadItemsImpl)230 void SetPreloadItemsImpl(const PreloadItemsFunc& preloadItemsImpl) 231 { 232 preloadItemsImpl_ = preloadItemsImpl; 233 } 234 PreloadItems(const std::set<int32_t> & indexSet)235 void PreloadItems(const std::set<int32_t>& indexSet) const 236 { 237 if (preloadItemsImpl_) { 238 preloadItemsImpl_(indexSet); 239 } 240 } 241 SetOnChangeImpl(const OnChangeFunc & onChangeImpl)242 void SetOnChangeImpl(const OnChangeFunc& onChangeImpl) 243 { 244 onChangeImpl_ = onChangeImpl; 245 } 246 FireOnChangeEvent(int32_t index)247 void FireOnChangeEvent(int32_t index) 248 { 249 if (onChangeImpl_) { 250 onChangeImpl_(index); 251 } 252 } 253 254 private: 255 SwipeToImpl swipeToImpl_; 256 SwipeToWithoutAnimationImpl swipeToWithoutAnimationImpl_; 257 CommonFunc showPrevImpl_; 258 CommonFunc showNextImpl_; 259 ChangeIndexImpl changeIndexImpl_; 260 ChangeIndexWithModeImpl changeIndexWithModeImpl_; 261 CommonFunc finishImpl_; 262 CommonFunc finishCallback_; 263 CommonFunc tabBarFinishCallback_; 264 CommonFunc removeTabBarEventCallback_; 265 CommonFunc addTabBarEventCallback_; 266 CommonFunc removeSwiperEventCallback_; 267 CommonFunc addSwiperEventCallback_; 268 TurnPageRateFunc turnPageRateCallback_; 269 CommonFunc updateCubicCurveCallback_; 270 CommonFunc surfaceChangeCallback_; 271 PreloadItemsFinishFunc preloadFinishCallback_; 272 PreloadItemsFunc preloadItemsImpl_; 273 OnChangeFunc onChangeImpl_; 274 }; 275 276 } // namespace OHOS::Ace 277 278 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SWIPER_SWIPER_CONTROLLER_H 279