-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHybridRiveFile.swift
More file actions
164 lines (137 loc) · 4.67 KB
/
HybridRiveFile.swift
File metadata and controls
164 lines (137 loc) · 4.67 KB
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
import NitroModules
import RiveRuntime
typealias ReferencedAssetCache = [String: RiveFileAsset]
class HybridRiveFile: HybridRiveFileSpec, RiveViewSource {
var riveFile: RiveFile?
var referencedAssetCache: ReferencedAssetCache?
var assetLoader: ReferencedAssetLoader?
var cachedFactory: RiveFactory?
private var weakViews: [Weak<RiveReactNativeView>] = []
public func setRiveFile(_ riveFile: RiveFile) {
self.riveFile = riveFile
}
func registerView(_ view: RiveReactNativeView) {
weakViews.append(Weak(view))
}
func unregisterView(_ view: RiveReactNativeView) {
weakViews.removeAll { $0.value === view }
}
private func refreshAfterAssetChange() {
weakViews = weakViews.filter { $0.value != nil }
for weakView in weakViews {
guard let view = weakView.value else { continue }
view.refreshAfterAssetChange()
}
}
var viewModelCount: Double? {
guard let count = riveFile?.viewModelCount else { return nil }
return Double(count)
}
func viewModelByIndex(index: Double) throws -> (any HybridViewModelSpec)? {
guard index >= 0 else { return nil }
guard let vm = riveFile?.viewModel(at: UInt(index)) else { return nil }
return HybridViewModel(viewModel: vm)
}
func viewModelByName(name: String) throws -> (any HybridViewModelSpec)? {
guard let vm = riveFile?.viewModelNamed(name) else { return nil }
return HybridViewModel(viewModel: vm)
}
func defaultArtboardViewModel(artboardBy: ArtboardBy?) throws -> (any HybridViewModelSpec)? {
let artboard: RiveArtboard?
if let artboardBy = artboardBy {
switch artboardBy.type {
case .index:
guard let index = artboardBy.index else { return nil }
artboard = try? riveFile?.artboard(from: Int(index))
case .name:
guard let name = artboardBy.name else { return nil }
artboard = try? riveFile?.artboard(fromName: name)
default:
artboard = nil
}
} else {
artboard = try? riveFile?.artboard()
}
guard let artboard = artboard,
let vm = riveFile?.defaultViewModel(for: artboard) else { return nil }
return HybridViewModel(viewModel: vm)
}
var artboardCount: Double {
Double(riveFile?.artboardNames().count ?? 0)
}
var artboardNames: [String] {
riveFile?.artboardNames() ?? []
}
func getBindableArtboard(name: String) throws -> any HybridBindableArtboardSpec {
guard let bindable = try riveFile?.bindableArtboard(withName: name) else {
throw NSError(
domain: "RiveError",
code: 0,
userInfo: [NSLocalizedDescriptionKey: "Artboard '\(name)' not found"]
)
}
return HybridBindableArtboard(bindableArtboard: bindable)
}
func getViewModelNamesAsync() throws -> Promise<[String]> {
return Promise.async {
guard let file = self.riveFile else { return [] }
let count = file.viewModelCount
var names: [String] = []
for i in 0..<count {
if let vm = file.viewModel(at: UInt(i)) {
names.append(vm.name)
}
}
return names
}
}
func viewModelByNameAsync(name: String, validate: Bool?) throws -> Promise<(any HybridViewModelSpec)?> {
return Promise.async { try self.viewModelByName(name: name) }
}
func defaultArtboardViewModelAsync(artboardBy: ArtboardBy?) throws -> Promise<(any HybridViewModelSpec)?> {
return Promise.async { try self.defaultArtboardViewModel(artboardBy: artboardBy) }
}
func getArtboardCountAsync() throws -> Promise<Double> {
return Promise.async { self.artboardCount }
}
func getArtboardNamesAsync() throws -> Promise<[String]> {
return Promise.async { self.artboardNames }
}
func updateReferencedAssets(referencedAssets: ReferencedAssetsType) {
guard let assetsData = referencedAssets.data,
let cache = referencedAssetCache,
let loader = assetLoader,
let _ = riveFile else {
return
}
let dispatchGroup = DispatchGroup()
var hasChanged = false
for (key, assetData) in assetsData {
guard let asset = cache[key] else { continue }
if let riveFactory = cachedFactory {
dispatchGroup.enter()
loader.loadAsset(source: assetData, asset: asset, factory: riveFactory) {
dispatchGroup.leave()
}
} else {
RCTLogError("[RiveFile] no factory available for update")
}
hasChanged = true
}
if hasChanged {
dispatchGroup.notify(queue: .main) { [weak self] in
self?.refreshAfterAssetChange()
}
}
}
func dispose() {
weakViews.removeAll()
referencedAssetCache = nil
assetLoader = nil
cachedFactory = nil
riveFile = nil
}
deinit {
dispose()
}
}