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