Skip to content

Commit

Permalink
fix: fix some race conditions, allow string values calls
Browse files Browse the repository at this point in the history
  • Loading branch information
hansemannn committed Sep 18, 2022
1 parent a8f4467 commit f1424f0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
15 changes: 13 additions & 2 deletions ios/Classes/TiAnimationAnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import Lottie
@objc(TiAnimationAnimationView)
public class TiAnimationAnimationView : TiUIView {

private var _animationView: AnimationView!
var _animationView: AnimationView!

private func animationView() -> AnimationView {
if _animationView == nil {
let file = TiUtils.stringValue(proxy.value(forKey: "file"))
let jsonString = TiUtils.stringValue(proxy.value(forKey: "jsonString"))
let autoStart = (proxy as! TiAnimationAnimationViewProxy).autoStart
let speed = TiUtils.floatValueSwift((proxy as! TiAnimationAnimationViewProxy).speed)
let loop = (proxy as! TiAnimationAnimationViewProxy).loop
let contentMode: Int? = Int(TiUtils.intValue(proxy.value(forKey: "contentMode")))

Expand Down Expand Up @@ -51,6 +52,10 @@ public class TiAnimationAnimationView : TiUIView {
_animationView.contentMode = .scaleAspectFit
}

if let speed = speed {
setSpeed(speed: speed)
}

_animationView.frame = bounds
addSubview(_animationView)

Expand Down Expand Up @@ -131,10 +136,16 @@ public class TiAnimationAnimationView : TiUIView {

return try? JSONSerialization.jsonObject(with: data!) as? [String: Any]
}

func initializeView() {
let _ = animationView()
}

public override func frameSizeChanged(_ frame: CGRect, bounds: CGRect) {
super.frameSizeChanged(frame, bounds: bounds)
animationView().frame = bounds
if let animationView = _animationView {
animationView.frame = bounds
}
}
}

Expand Down
75 changes: 47 additions & 28 deletions ios/Classes/TiAnimationAnimationViewProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,25 @@ import TitaniumKit
class TiAnimationAnimationViewProxy: TiViewProxy {

var autoStart = false


var loop = false

var speed: Float? = 1.0

var progress: Float? = 0.0

var cache = false

override func _init(withProperties properties: [AnyHashable : Any]!) {
super._init(withProperties: properties)

autoStart = TiUtils.boolValue("autoStart", properties: properties)
loop = TiUtils.boolValue("loop", properties: properties)
cache = TiUtils.boolValue("cache", properties: properties)
progress = TiUtils.floatValueSwift(properties["progress"])
speed = TiUtils.floatValueSwift(properties["speed"])

animationView().initializeView()
}

private func animationView() -> TiAnimationAnimationView {
Expand Down Expand Up @@ -66,43 +79,34 @@ class TiAnimationAnimationViewProxy: TiViewProxy {

// MARK: - Properties

@objc var progress: Any? {
set {
let progress = newValue as? Float ?? 0.0
@objc(setProgress:)
func setProgress(value: Any) {
let progress = TiUtils.floatValueSwift(value) ?? 0.0
if animationView()._animationView != nil {
animationView().setProgress(progress: progress)

replaceValue(progress, forKey: "progress", notification: false)
}
get {
return animationView().progress()
}

replaceValue(progress, forKey: "progress", notification: false)
}

@objc var speed: Any? {
set {
let speed = newValue as? Float ?? 0.0
@objc(setSpeed:)
func setSpeed(value: Any) {
let speed = TiUtils.floatValueSwift(value) ?? 1.0
if animationView()._animationView != nil {
animationView().setSpeed(speed: speed)

replaceValue(speed, forKey: "speed", notification: false)

}
get {
return animationView().speed()
}

replaceValue(speed, forKey: "speed", notification: false)
}

@objc lazy var loop: Bool = {
return animationView().loop()
}()

@objc var cache: Any {
set {
let cache = newValue as? Bool ?? false
@objc(setCache:)
func setCache(value: Any) {
let cache = value as? Bool ?? false
if animationView()._animationView != nil {
animationView().setCacheEnabled(cacheEnabled: cache)
}
get {
return animationView().cacheEnabled()
}

replaceValue(cache, forKey: "cache", notification: false)
}

@objc var isPlaying: Bool {
Expand Down Expand Up @@ -135,3 +139,18 @@ class TiAnimationAnimationViewProxy: TiViewProxy {
animationView().add(view: viewProxy.view, toLayer: keypathLayer)
}
}

// MARK: Utils

extension TiUtils {

class func floatValueSwift(_ value: Any?) -> Float? {
if let floatValue = value as? Float {
return floatValue
} else if let stringValue = value as? String {
return Float(stringValue)
}

return nil
}
}

0 comments on commit f1424f0

Please sign in to comment.