1 /* 2 * Copyright (c) 2024 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_LINEAR_INDICATOR_LINEAR_INDICATOR_CONTROLLER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LINEAR_INDICATOR_LINEAR_INDICATOR_CONTROLLER_H 18 19 #include "base/memory/referenced.h" 20 #include "base/thread/cancelable_callback.h" 21 #include "core/components_ng/render/animation_utils.h" 22 23 namespace OHOS::Ace::NG { 24 class LinearIndicatorPattern; 25 class FrameNode; 26 27 enum class LinearIndicatorControllerDataState { STOP, ANIMATION, INTERVAL, ANIMATION_PAUSE, INTERVAL_PAUSE }; 28 29 class LinearIndicatorControllerData { 30 public: 31 LinearIndicatorControllerData(); 32 33 void InitData(); 34 IsAnimationTimeEqually(int32_t totalAnimationTime)35 bool IsAnimationTimeEqually(int32_t totalAnimationTime) const 36 { 37 return totalAnimationTime == totalAnimationTime_; 38 } 39 IsIntervalTimeEqually(int32_t totalIntervalTime)40 bool IsIntervalTimeEqually(int32_t totalIntervalTime) const 41 { 42 return totalIntervalTime == totalIntervalTime_; 43 } 44 IsTimeEqually(int32_t totalAnimationTime,int32_t totalIntervalTime)45 bool IsTimeEqually(int32_t totalAnimationTime, int32_t totalIntervalTime) const 46 { 47 return IsAnimationTimeEqually(totalAnimationTime) && IsIntervalTimeEqually(totalIntervalTime); 48 } 49 SetTotalAnimationTime(int32_t totalAnimationTime)50 void SetTotalAnimationTime(int32_t totalAnimationTime) 51 { 52 totalAnimationTime_ = totalAnimationTime; 53 } 54 SetTotalIntervalTime(int32_t totalIntervalTime)55 void SetTotalIntervalTime(int32_t totalIntervalTime) 56 { 57 totalIntervalTime_ = totalIntervalTime; 58 } 59 TotalAnimationTime()60 int32_t TotalAnimationTime() const 61 { 62 return totalAnimationTime_; 63 } 64 TotalIntervalTime()65 int32_t TotalIntervalTime() const 66 { 67 return totalIntervalTime_; 68 } 69 SetTime(int32_t totalAnimationTime,int32_t totalIntervalTime)70 void SetTime(int32_t totalAnimationTime, int32_t totalIntervalTime) 71 { 72 SetTotalAnimationTime(totalAnimationTime); 73 SetTotalIntervalTime(totalIntervalTime); 74 } 75 IsStop()76 bool IsStop() const 77 { 78 return state_ == LinearIndicatorControllerDataState::STOP; 79 } 80 IsRuning()81 bool IsRuning() const 82 { 83 return state_ == LinearIndicatorControllerDataState::ANIMATION || 84 state_ == LinearIndicatorControllerDataState::INTERVAL; 85 } 86 IsPause()87 bool IsPause() const 88 { 89 return state_ == LinearIndicatorControllerDataState::ANIMATION_PAUSE || 90 state_ == LinearIndicatorControllerDataState::INTERVAL_PAUSE; 91 } 92 State()93 LinearIndicatorControllerDataState State() const 94 { 95 return state_; 96 } 97 SetState(LinearIndicatorControllerDataState state)98 void SetState(LinearIndicatorControllerDataState state) 99 { 100 state_ = state; 101 if (LinearIndicatorControllerDataState::INTERVAL_PAUSE == state) { 102 UpdateIntervalConsumeTime(); 103 } 104 } 105 UpdateIntervalStart(int32_t consumeTime)106 void UpdateIntervalStart(int32_t consumeTime) 107 { 108 intervalStart_ = std::chrono::system_clock::now() - std::chrono::milliseconds(consumeTime); 109 } 110 IntervalCurrentConsumeTime()111 int32_t IntervalCurrentConsumeTime() const 112 { 113 auto end = std::chrono::system_clock::now(); 114 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - intervalStart_); 115 return int(duration.count()); 116 } 117 IntervalConsumeTime()118 int32_t IntervalConsumeTime() const 119 { 120 return intervalConsumeTime_; 121 } 122 UpdateIntervalConsumeTime()123 void UpdateIntervalConsumeTime() 124 { 125 intervalConsumeTime_ = IntervalCurrentConsumeTime(); 126 } 127 Value()128 float Value() const 129 { 130 return value_; 131 } 132 SetValue(float value)133 void SetValue(float value) 134 { 135 value_ = value; 136 } 137 Index()138 int32_t Index() const 139 { 140 return index_; 141 } 142 UpdateIndex()143 int32_t UpdateIndex() 144 { 145 SetValue(.0f); 146 return ++index_; 147 } 148 SetIndex(int32_t index)149 void SetIndex(int32_t index) 150 { 151 index_ = index; 152 } 153 SetIndexAndValue(int32_t index,float value)154 void SetIndexAndValue(int32_t index, float value) 155 { 156 SetIndex(index); 157 SetValue(value); 158 } 159 IsLoop()160 bool IsLoop() const 161 { 162 return isLoop_; 163 } 164 Loop(bool value)165 void Loop(bool value) 166 { 167 isLoop_ = value; 168 } 169 IsProgressAnimation()170 bool IsProgressAnimation() const 171 { 172 return progressAnimation_.operator bool(); 173 } 174 ProgressAnimationAndClear()175 std::shared_ptr<AnimationUtils::Animation> ProgressAnimationAndClear() 176 { 177 std::shared_ptr<AnimationUtils::Animation> animation = progressAnimation_; 178 progressAnimation_.reset(); 179 return animation; 180 } 181 SetProgressAnimation(const std::shared_ptr<AnimationUtils::Animation> & animation)182 void SetProgressAnimation(const std::shared_ptr<AnimationUtils::Animation>& animation) 183 { 184 progressAnimation_ = animation; 185 } 186 ProgressInterval()187 const CancelableCallback<void()>& ProgressInterval() const 188 { 189 return progressInterval_; 190 } 191 ProgressIntervalCancel()192 void ProgressIntervalCancel() 193 { 194 progressInterval_.Cancel(); 195 } 196 ProgressIntervalReset(std::function<void ()> && callback)197 void ProgressIntervalReset(std::function<void()>&& callback) 198 { 199 progressInterval_.Reset(callback); 200 } 201 UpdateAnimationTag()202 int32_t UpdateAnimationTag() 203 { 204 return ++animationTag_; 205 } 206 AnimationTag()207 int32_t AnimationTag() const 208 { 209 return animationTag_; 210 } 211 212 private: 213 std::shared_ptr<AnimationUtils::Animation> progressAnimation_; 214 int32_t animationTag_ = 0; 215 CancelableCallback<void()> progressInterval_; 216 int32_t totalAnimationTime_ = 0; 217 int32_t totalIntervalTime_ = 0; 218 219 LinearIndicatorControllerDataState state_; 220 221 int32_t index_ = 0; 222 float value_ = .0f; 223 int32_t intervalConsumeTime_ = 0; 224 std::chrono::system_clock::time_point intervalStart_; 225 bool isLoop_ = true; 226 }; 227 228 class LinearIndicatorController : public Referenced { 229 public: LinearIndicatorController(const WeakPtr<LinearIndicatorPattern> & pattern)230 explicit LinearIndicatorController(const WeakPtr<LinearIndicatorPattern>& pattern) 231 : pattern_(pattern), animationData_(), changeCallback_(nullptr) 232 {} 233 ~LinearIndicatorController() override = default; 234 235 void SetProgress(int32_t index, float value); 236 void Start(int32_t animationTime, int32_t intervalTime); 237 void Pause(); 238 void Stop(); 239 OnChange(std::function<void (int32_t index,float progress)> && callback)240 void OnChange(std::function<void(int32_t index, float progress)>&& callback) 241 { 242 changeCallback_ = std::move(callback); 243 } 244 Loop(bool value)245 void Loop(bool value) 246 { 247 animationData_.Loop(value); 248 } 249 TotalAnimationTime()250 int32_t TotalAnimationTime() const 251 { 252 return animationData_.TotalAnimationTime(); 253 } 254 TotalIntervalTime()255 int32_t TotalIntervalTime() const 256 { 257 return animationData_.TotalIntervalTime(); 258 } 259 IsRuning()260 bool IsRuning() const 261 { 262 return animationData_.IsRuning(); 263 } 264 265 void UpdateProgressSize(int32_t size); 266 Value()267 float Value() const 268 { 269 return animationData_.Value(); 270 } 271 Index()272 int32_t Index() const 273 { 274 return animationData_.Index(); 275 } 276 277 private: 278 void PlayingUpdateTime(int32_t animationTime, int32_t intervalTime); 279 void StartProgressAnimation(); 280 void SetProgressValue(float value); 281 void ProgreAnimationStart(); 282 void ProgreAnimationEnd(); 283 void StartProgressInterval(int32_t intervalTime); 284 285 int32_t GetProgressSize() const; 286 RefPtr<FrameNode> GetProgressNode(int32_t index) const; 287 bool SetProgressComponentValue(int32_t index, float value); 288 void InitProgressValue(); 289 290 RefPtr<FrameNode> GetHost() const; 291 292 void StopAnimation(LinearIndicatorControllerDataState state); 293 294 float RecalcProgressValue(int32_t newAnimationTime); 295 296 void SetValueAndCallback(float value, bool isDraw); 297 298 public: 299 static constexpr float END_VALUE = 100.0f; 300 static constexpr char LINEAR_INDICATOR_ANIMATION_NAME[] = "linear_indicator_animation"; 301 static constexpr char LINEAR_INDICATOR_INTERVAL_NAME[] = "linear_indicator_interval"; 302 static constexpr int32_t ANIMATION_TIME_MIN = 1; 303 304 private: 305 WeakPtr<LinearIndicatorPattern> pattern_; 306 LinearIndicatorControllerData animationData_; 307 std::function<void(int32_t index, float progress)> changeCallback_; 308 }; 309 } // namespace OHOS::Ace::NG 310 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_LINEAR_INDICATOR_LINEAR_INDICATOR_CONTROLLER_H 311