-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ContainerView.swift
286 lines (247 loc) · 9.6 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import Lottie
import Foundation
@objc protocol LottieContainerViewDelegate {
func onAnimationFinish(isCancelled: Bool);
}
@objc(LottieContainerView)
class ContainerView: RCTView {
private var speed: CGFloat = 0.0
private var progress: CGFloat = 0.0
private var autoPlay: Bool = false
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] = []
private var renderMode: RenderingEngineOption = .automatic
@objc weak var delegate: LottieContainerViewDelegate? = nil
@objc var onAnimationFinish: RCTBubblingEventBlock?
var animationView: LottieAnimationView?
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if #available(iOS 13.0, tvOS 13.0, *) {
if (self.traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection)) {
applyColorProperties()
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 setAutoPlay(_ autoPlay: Bool) {
self.autoPlay = autoPlay
if(autoPlay && animationView?.isAnimationPlaying == false) {
self.play()
}
}
@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 nextAnimationView = LottieAnimationView()
nextAnimationView.textProvider = DictionaryTextProvider(filters)
nextAnimationView.animation = animationView?.animation
replaceAnimationView(next: nextAnimationView)
}
}
func getLottieConfiguration() -> LottieConfiguration {
return LottieConfiguration(
renderingEngine: renderMode
)
}
@objc func setRenderMode(_ newRenderMode: String) {
switch newRenderMode {
case "SOFTWARE":
if (renderMode == .mainThread) {
return
}
renderMode = .mainThread
case "HARDWARE":
if (renderMode == .coreAnimation) {
return
}
renderMode = .coreAnimation
case "AUTOMATIC":
fallthrough
default:
if (renderMode == .automatic) {
return
}
renderMode = .automatic
}
if (animationView != nil) {
let nextAnimationView = LottieAnimationView(
animation: animationView?.animation,
configuration: getLottieConfiguration()
)
replaceAnimationView(next: nextAnimationView)
}
}
@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(LottieAnimation.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 nextAnimationView = LottieAnimationView(
animation: animation,
configuration: self.getLottieConfiguration()
)
self.replaceAnimationView(next: nextAnimationView)
}
} 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(LottieAnimation.self, from: data) else {
if (RCT_DEBUG == 1) {
print("Unable to create the lottie animation object from the JSON source")
}
return
}
let nextAnimationView = LottieAnimationView(
animation: animation,
configuration: getLottieConfiguration()
)
replaceAnimationView(next: nextAnimationView)
}
@objc func setSourceName(_ newSourceName: String) {
if (newSourceName == sourceName) {
return
}
sourceName = newSourceName
let nextAnimationView = LottieAnimationView(
name: sourceName,
configuration: getLottieConfiguration()
)
replaceAnimationView(next: nextAnimationView)
}
@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
applyColorProperties()
}
// There is no Nullable CGFloat in Objective-C, so this function uses a Nullable NSNumber and converts it later
@objc(playFromFrame:toFrame:)
func objcCompatiblePlay(fromFrame: NSNumber? = nil, toFrame: AnimationFrameTime) {
let convertedFromFrame = fromFrame != nil ? CGFloat(truncating: fromFrame!) : nil;
play(fromFrame: convertedFromFrame, toFrame: toFrame);
}
func getCompletionCallback() -> LottieCompletionBlock {
return { animationFinished in
if let onFinish = self.onAnimationFinish {
onFinish(["isCancelled": !animationFinished])
}
self.delegate?.onAnimationFinish(isCancelled: !animationFinished);
if (animationFinished) {
// Force the animation to stay on the last frame when the animation ends
self.animationView?.currentProgress = 1;
}
};
}
func play(fromFrame: AnimationFrameTime? = nil, toFrame: AnimationFrameTime) {
animationView?.play(fromFrame: fromFrame, toFrame: toFrame, loopMode: self.loop, completion: getCompletionCallback());
}
@objc func play() {
animationView?.play(completion: getCompletionCallback())
}
@objc func reset() {
animationView?.currentProgress = 0;
animationView?.pause()
}
@objc func pause() {
animationView?.pause()
}
@objc func resume() {
play()
}
// reset the animationView frame whenever the view's frame changes
override var frame: CGRect {
didSet {
animationView?.reactSetFrame(frame)
}
}
// MARK: Private
func replaceAnimationView(next: LottieAnimationView) {
animationView?.removeFromSuperview()
let contentMode = animationView?.contentMode ?? .scaleAspectFit
animationView = next
addSubview(next)
animationView?.contentMode = contentMode
animationView?.reactSetFrame(frame)
animationView?.backgroundBehavior = .pauseAndRestore
animationView?.animationSpeed = speed
animationView?.loopMode = loop
applyColorProperties()
if (self.autoPlay) {
self.play()
}
}
func applyColorProperties() {
guard let animationView = animationView else { return }
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)
}
}
}
}