-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
ContainerView.swift
216 lines (184 loc) · 7.31 KB
/
ContainerView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import Lottie
import Foundation
class ContainerView: RCTView {
private var speed: CGFloat = 0.0
private var progress: CGFloat = 0.0
private var loop: LottieLoopMode = .playOnce
private var sourceJson: String = ""
private var resizeMode: String = ""
private var sourceName: String = ""
private var colorFilters: [NSDictionary] = []
private var textFilters: [NSDictionary] = []
@objc var onAnimationFinish: RCTBubblingEventBlock?
var animationView: AnimationView?
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, tvOS 13.0, *) {
if (self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
applyProperties()
print("dark mode changed")
}
}
}
@objc func setSpeed(_ newSpeed: CGFloat) {
speed = newSpeed
if (newSpeed != 0.0) {
animationView?.animationSpeed = newSpeed
if (!(animationView?.isAnimationPlaying ?? true)) {
animationView?.play()
}
} else if (animationView?.isAnimationPlaying ?? false) {
animationView?.pause()
}
}
@objc func setProgress(_ newProgress: CGFloat) {
progress = newProgress
animationView?.currentProgress = progress
}
override func reactSetFrame(_ frame: CGRect) {
super.reactSetFrame(frame)
animationView?.reactSetFrame(frame)
}
@objc func setLoop(_ isLooping: Bool) {
loop = isLooping ? .loop : .playOnce
animationView?.loopMode = loop
}
@objc func setTextFiltersIOS(_ newTextFilters: [NSDictionary]) {
textFilters = newTextFilters
if (textFilters.count > 0) {
var filters = [String:String]()
for filter in textFilters {
let key = filter.value(forKey: "keypath") as! String
let value = filter.value(forKey: "text") as! String
filters[key] = value;
}
let starAnimationView = AnimationView()
starAnimationView.textProvider = DictionaryTextProvider(filters)
starAnimationView.animation = animationView?.animation
replaceAnimationView(next: starAnimationView)
}
}
@objc func setSourceURL(_ newSourceURLString: String) {
var url = URL(string: newSourceURLString)
if(url?.scheme == nil) {
// interpret raw URL paths as relative to the resource bundle
url = URL(fileURLWithPath: newSourceURLString, relativeTo: Bundle.main.resourceURL)
}
if(url != nil) {
DispatchQueue.global(qos: .default).async {
do {
let sourceJson = try String(contentsOf: url!)
guard let data = sourceJson.data(using: String.Encoding.utf8),
let animation = try? JSONDecoder().decode(Animation.self, from: data) else {
if (RCT_DEBUG == 1) {
print("Unable to decode the lottie animation object from the fetched URL source")
}
return
}
DispatchQueue.main.async {
let starAnimationView = AnimationView()
starAnimationView.animation = animation
self.replaceAnimationView(next: starAnimationView)
self.animationView?.play()
}
} catch {
if (RCT_DEBUG == 1) {
print("Unable to load the lottie animation URL")
}
}
}
}
}
@objc func setSourceJson(_ newSourceJson: String) {
sourceJson = newSourceJson
guard let data = sourceJson.data(using: String.Encoding.utf8),
let animation = try? JSONDecoder().decode(Animation.self, from: data) else {
if (RCT_DEBUG == 1) {
print("Unable to create the lottie animation object from the JSON source")
}
return
}
let starAnimationView = AnimationView()
starAnimationView.animation = animation
replaceAnimationView(next: starAnimationView)
}
@objc func setSourceName(_ newSourceName: String) {
if (newSourceName == sourceName) {
return
}
sourceName = newSourceName
let starAnimationView = AnimationView(name: sourceName)
replaceAnimationView(next: starAnimationView)
}
@objc func setResizeMode(_ resizeMode: String) {
switch (resizeMode) {
case "cover":
animationView?.contentMode = .scaleAspectFill
case "contain":
animationView?.contentMode = .scaleAspectFit
case "center":
animationView?.contentMode = .center
default: break
}
}
@objc func setColorFilters(_ newColorFilters: [NSDictionary]) {
colorFilters = newColorFilters
applyProperties()
}
func play(fromFrame: AnimationFrameTime? = nil, toFrame: AnimationFrameTime) {
let callback: LottieCompletionBlock = { animationFinished in
if let onFinish = self.onAnimationFinish {
onFinish(["isCancelled": !animationFinished])
}
}
animationView?.backgroundBehavior = .pauseAndRestore
animationView?.play(fromFrame: fromFrame, toFrame: toFrame, loopMode: self.loop, completion: callback);
}
func play() {
let callback: LottieCompletionBlock = { animationFinished in
if let onFinish = self.onAnimationFinish {
onFinish(["isCancelled": !animationFinished])
}
}
animationView?.backgroundBehavior = .pauseAndRestore
animationView?.play(completion: callback)
}
func reset() {
animationView?.currentProgress = 0;
animationView?.pause()
}
func pause() {
animationView?.pause()
}
func resume() {
play()
}
// MARK: Private
func replaceAnimationView(next: AnimationView) {
animationView?.removeFromSuperview()
let contentMode = animationView?.contentMode ?? .scaleAspectFit
animationView = next
addSubview(next)
animationView?.contentMode = contentMode
animationView?.reactSetFrame(frame)
applyProperties()
}
func applyProperties() {
guard let animationView = animationView else { return }
let isPlaying = animationView.isAnimationPlaying
animationView.currentProgress = progress
animationView.animationSpeed = speed
animationView.loopMode = loop
if (colorFilters.count > 0) {
for filter in colorFilters {
let keypath: String = "\(filter.value(forKey: "keypath") as! String).**.Color"
let fillKeypath = AnimationKeypath(keypath: keypath)
let colorFilterValueProvider = ColorValueProvider((filter.value(forKey: "color") as! PlatformColor).lottieColorValue)
animationView.setValueProvider(colorFilterValueProvider, keypath: fillKeypath)
}
}
if isPlaying {
resume()
}
}
}