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