Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AnimationHandler.swift
- // NovaVPN
- //
- // Created by vladislav klimenko on 29/06/2017.
- // Copyright © 2017 SMedia Link. All rights reserved.
- //
- import Lottie
- class AnimationHandler {
- var animationView: LOTAnimationView?
- var view: View!
- private var animationsQueue: [AnimationNode] = []
- private var errorInProgress = false
- private var queueInProgress = false
- private var currentAnimation: AnimationNode = AnimationNode(animationName: "", looped: false)
- private var destinationAnimation: AnimationNode = AnimationNode(animationName: "", looped: false)
- var stopQueue = false
- private func processAnimationsQueue() {
- guard let nextAnimation = animationsQueue.first, !stopQueue else {
- queueInProgress = false
- errorInProgress = false
- return
- }
- self.currentAnimation = nextAnimation
- animationsQueue.remove(at: 0)
- guard currentAnimation.animationName != destinationAnimation.animationName else {
- if nextAnimation.looped {
- animationsQueue = []
- let newAnimation = presentAnimation(with: nextAnimation.animationName, speed: 1)
- newAnimation?.loopAnimation = true
- queueInProgress = false
- errorInProgress = false
- newAnimation?.play()
- }
- return
- }
- let speed: CGFloat = (currentAnimation.animationName == "cosmonautIntro" ? 2.5 : 1.0)
- presentAnimation(with: currentAnimation.animationName, speed: speed)?.play { _ in
- self.processAnimationsQueue()
- }
- }
- func runAnimationQueue() {
- if !queueInProgress {
- queueInProgress = true
- processAnimationsQueue()
- }
- }
- func enqueueAnimation(for status: VpnStatus) {
- let transferName: String
- let destinationName: String
- errorInProgress = false
- switch status {
- case .connected:
- transferName = "cosmonautFlightStart"
- destinationName = "cosmonautFlight"
- let animationStart = AnimationNode(animationName: transferName, looped: false)
- let animationEnd = AnimationNode(animationName: destinationName, looped: true)
- animationsQueue.append(animationStart)
- animationsQueue.append(animationEnd)
- destinationAnimation = animationEnd
- case .disconnected:
- guard destinationAnimation.animationName != "floating" else {
- break
- }
- transferName = "cosmonautFlightEnd"
- destinationName = "floating"
- let animationStart = AnimationNode(animationName: transferName, looped: false)
- let animationEnd = AnimationNode(animationName: destinationName, looped: true)
- animationsQueue.append(animationStart)
- animationsQueue.append(animationEnd)
- destinationAnimation = animationEnd
- default:
- break
- }
- }
- func presentAnimation(with name: String, speed: CGFloat) -> LOTAnimationView? {
- animationView?.removeFromSuperview()
- if let animation = LOTAnimationView(name: name) {
- animation.animationSpeed = speed
- animationView = animation
- view.addSubview(animation)
- setConstraints(for: animation)
- return animation
- }
- return nil
- }
- func setConstraints(for animationView: LOTAnimationView) {
- animationView.snp.makeConstraints({ (make) in
- make.height.equalTo(view).multipliedBy(0.9)
- make.width.equalTo(animationView.snp.height).multipliedBy(2.8)
- make.centerX.equalTo(view)
- make.centerY.equalTo(view).offset(-20)
- })
- }
- func runInitialAnimation() {
- let transferName = "cosmonautIntro"
- let destinationName = "floating"
- let animationStart = AnimationNode(animationName: transferName, looped: false)
- let animationEnd = AnimationNode(animationName: destinationName, looped: true)
- animationsQueue.append(animationStart)
- animationsQueue.append(animationEnd)
- destinationAnimation = animationEnd
- runAnimationQueue()
- }
- func stopAnimations() {
- animationsQueue = []
- stopQueue = true
- queueInProgress = false
- errorInProgress = false
- animationView?.removeFromSuperview()
- }
- func showErrorAnimation() {
- guard !errorInProgress else {
- return
- }
- errorInProgress = true
- if destinationAnimation.animationName == "floating" {
- let transferName = "newError"
- let destinationName = "floating"
- let animationStart = AnimationNode(animationName: transferName, looped: false)
- let animationEnd = AnimationNode(animationName: destinationName, looped: true)
- animationsQueue.append(animationStart)
- animationsQueue.append(animationEnd)
- destinationAnimation = animationEnd
- } else if destinationAnimation.animationName == "cosmonautFlight" {
- let start = "newError"
- let transfer = "floating"
- let animationStart = AnimationNode(animationName: start, looped: false)
- let transferAnimation = AnimationNode(animationName: transfer, looped: false)
- animationsQueue.append(animationStart)
- animationsQueue.append(transferAnimation)
- enqueueAnimation(for: .connected)
- }
- runAnimationQueue()
- }
- func continueAnimations() {
- if stopQueue {
- stopQueue = false
- let newAnimation = presentAnimation(with: destinationAnimation.animationName, speed: 1)
- newAnimation?.loopAnimation = destinationAnimation.looped
- newAnimation?.play()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement