Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 5.98 KB | None | 0 0
  1. //
  2. //  AnimationHandler.swift
  3. //  NovaVPN
  4. //
  5. //  Created by vladislav klimenko on 29/06/2017.
  6. //  Copyright © 2017 SMedia Link. All rights reserved.
  7. //
  8.  
  9. import Lottie
  10.  
  11. class AnimationHandler {
  12.    
  13.     var animationView: LOTAnimationView?
  14.     var view: View!
  15.     private var animationsQueue: [AnimationNode] = []
  16.     private var errorInProgress = false
  17.     private var queueInProgress = false
  18.     private var currentAnimation: AnimationNode = AnimationNode(animationName: "", looped: false)
  19.     private var destinationAnimation: AnimationNode = AnimationNode(animationName: "", looped: false)
  20.     var stopQueue = false
  21.  
  22.     private func processAnimationsQueue() {
  23.         guard let nextAnimation = animationsQueue.first, !stopQueue else {
  24.             queueInProgress = false
  25.             errorInProgress = false
  26.             return
  27.         }
  28.         self.currentAnimation = nextAnimation
  29.         animationsQueue.remove(at: 0)
  30.         guard currentAnimation.animationName != destinationAnimation.animationName else {
  31.             if nextAnimation.looped {
  32.                 animationsQueue = []
  33.                 let newAnimation = presentAnimation(with: nextAnimation.animationName, speed: 1)
  34.                 newAnimation?.loopAnimation = true
  35.                 queueInProgress = false
  36.                 errorInProgress = false
  37.                 newAnimation?.play()
  38.             }
  39.             return
  40.         }
  41.         let speed: CGFloat = (currentAnimation.animationName == "cosmonautIntro" ? 2.5 : 1.0)
  42.         presentAnimation(with: currentAnimation.animationName, speed: speed)?.play { _ in
  43.             self.processAnimationsQueue()
  44.         }
  45.     }
  46.  
  47.     func runAnimationQueue() {
  48.         if !queueInProgress {
  49.             queueInProgress = true
  50.             processAnimationsQueue()
  51.         }
  52.     }
  53.    
  54.     func enqueueAnimation(for status: VpnStatus) {
  55.         let transferName: String
  56.         let destinationName: String
  57.         errorInProgress = false
  58.         switch status {
  59.         case .connected:
  60.             transferName = "cosmonautFlightStart"
  61.             destinationName = "cosmonautFlight"
  62.             let animationStart = AnimationNode(animationName: transferName, looped: false)
  63.             let animationEnd = AnimationNode(animationName: destinationName, looped: true)
  64.             animationsQueue.append(animationStart)
  65.             animationsQueue.append(animationEnd)
  66.             destinationAnimation = animationEnd
  67.         case .disconnected:
  68.             guard destinationAnimation.animationName != "floating" else {
  69.                 break
  70.             }
  71.             transferName = "cosmonautFlightEnd"
  72.             destinationName = "floating"
  73.             let animationStart = AnimationNode(animationName: transferName, looped: false)
  74.             let animationEnd = AnimationNode(animationName: destinationName, looped: true)
  75.             animationsQueue.append(animationStart)
  76.             animationsQueue.append(animationEnd)
  77.             destinationAnimation = animationEnd
  78.         default:
  79.             break
  80.         }
  81.  
  82.     }
  83.    
  84.     func presentAnimation(with name: String, speed: CGFloat) -> LOTAnimationView? {
  85.         animationView?.removeFromSuperview()
  86.         if let animation = LOTAnimationView(name: name) {
  87.             animation.animationSpeed = speed
  88.             animationView = animation
  89.             view.addSubview(animation)
  90.             setConstraints(for: animation)
  91.             return animation
  92.         }
  93.         return nil
  94.     }
  95.    
  96.     func setConstraints(for animationView: LOTAnimationView) {
  97.         animationView.snp.makeConstraints({ (make) in
  98.             make.height.equalTo(view).multipliedBy(0.9)
  99.             make.width.equalTo(animationView.snp.height).multipliedBy(2.8)
  100.             make.centerX.equalTo(view)
  101.             make.centerY.equalTo(view).offset(-20)
  102.         })
  103.     }
  104.    
  105.     func runInitialAnimation() {
  106.         let transferName = "cosmonautIntro"
  107.         let destinationName = "floating"
  108.         let animationStart = AnimationNode(animationName: transferName, looped: false)
  109.         let animationEnd = AnimationNode(animationName: destinationName, looped: true)
  110.         animationsQueue.append(animationStart)
  111.         animationsQueue.append(animationEnd)
  112.         destinationAnimation = animationEnd
  113.         runAnimationQueue()
  114.     }
  115.    
  116.     func stopAnimations() {
  117.         animationsQueue = []
  118.         stopQueue = true
  119.         queueInProgress = false
  120.         errorInProgress = false
  121.         animationView?.removeFromSuperview()
  122.        
  123.     }
  124.    
  125.     func showErrorAnimation() {
  126.         guard !errorInProgress else {
  127.             return
  128.         }
  129.         errorInProgress = true
  130.         if destinationAnimation.animationName == "floating" {
  131.             let transferName = "newError"
  132.             let destinationName = "floating"
  133.             let animationStart = AnimationNode(animationName: transferName, looped: false)
  134.             let animationEnd = AnimationNode(animationName: destinationName, looped: true)
  135.             animationsQueue.append(animationStart)
  136.             animationsQueue.append(animationEnd)
  137.             destinationAnimation = animationEnd
  138.         } else if destinationAnimation.animationName == "cosmonautFlight" {
  139.             let start = "newError"
  140.             let transfer = "floating"
  141.             let animationStart = AnimationNode(animationName: start, looped: false)
  142.             let transferAnimation = AnimationNode(animationName: transfer, looped: false)
  143.             animationsQueue.append(animationStart)
  144.             animationsQueue.append(transferAnimation)
  145.             enqueueAnimation(for: .connected)
  146.         }
  147.        
  148.  
  149.         runAnimationQueue()
  150.     }
  151.    
  152.     func continueAnimations() {
  153.         if stopQueue {
  154.             stopQueue = false
  155.             let newAnimation = presentAnimation(with: destinationAnimation.animationName, speed: 1)
  156.             newAnimation?.loopAnimation = destinationAnimation.looped
  157.             newAnimation?.play()
  158.         }
  159.  
  160.     }
  161.    
  162.    
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement