-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHybridViewModelInstance.swift
More file actions
77 lines (62 loc) · 3.49 KB
/
HybridViewModelInstance.swift
File metadata and controls
77 lines (62 loc) · 3.49 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
import NitroModules
import RiveRuntime
class HybridViewModelInstance: HybridViewModelInstanceSpec {
let viewModelInstance: RiveDataBindingViewModel.Instance?
init(viewModelInstance: RiveDataBindingViewModel.Instance) {
self.viewModelInstance = viewModelInstance
}
var instanceName: String { viewModelInstance?.name ?? "" }
func numberProperty(path: String) throws -> (any HybridViewModelNumberPropertySpec)? {
guard let property = viewModelInstance?.numberProperty(fromPath: path) else { return nil }
return HybridViewModelNumberProperty(property: property)
}
func stringProperty(path: String) throws -> (any HybridViewModelStringPropertySpec)? {
guard let property = viewModelInstance?.stringProperty(fromPath: path) else { return nil }
return HybridViewModelStringProperty(property: property)
}
func booleanProperty(path: String) throws -> (any HybridViewModelBooleanPropertySpec)? {
guard let property = viewModelInstance?.booleanProperty(fromPath: path) else { return nil }
return HybridViewModelBooleanProperty(property: property)
}
func colorProperty(path: String) throws -> (any HybridViewModelColorPropertySpec)? {
guard let property = viewModelInstance?.colorProperty(fromPath: path) else { return nil }
return HybridViewModelColorProperty(property: property)
}
func enumProperty(path: String) throws -> (any HybridViewModelEnumPropertySpec)? {
guard let property = viewModelInstance?.enumProperty(fromPath: path) else { return nil }
return HybridViewModelEnumProperty(property: property)
}
func triggerProperty(path: String) throws -> (any HybridViewModelTriggerPropertySpec)? {
guard let property = viewModelInstance?.triggerProperty(fromPath: path) else { return nil }
return HybridViewModelTriggerProperty(property: property)
}
func imageProperty(path: String) throws -> (any HybridViewModelImagePropertySpec)? {
guard let property = viewModelInstance?.imageProperty(fromPath: path) else { return nil }
return HybridViewModelImageProperty(property: property)
}
func listProperty(path: String) throws -> (any HybridViewModelListPropertySpec)? {
guard let property = viewModelInstance?.listProperty(fromPath: path) else { return nil }
return HybridViewModelListProperty(property: property)
}
func artboardProperty(path: String) throws -> (any HybridViewModelArtboardPropertySpec)? {
guard let property = viewModelInstance?.artboardProperty(fromPath: path) else { return nil }
return HybridViewModelArtboardProperty(property: property)
}
func viewModel(path: String) throws -> (any HybridViewModelInstanceSpec)? {
guard let instance = viewModelInstance?.viewModelInstanceProperty(fromPath: path) else { return nil }
return HybridViewModelInstance(viewModelInstance: instance)
}
func replaceViewModel(path: String, instance: any HybridViewModelInstanceSpec) throws {
guard let hybridInstance = instance as? HybridViewModelInstance,
let nativeInstance = hybridInstance.viewModelInstance else {
throw RuntimeError.error(withMessage: "Invalid ViewModelInstance provided to replaceViewModel")
}
let success = viewModelInstance?.setViewModelInstanceProperty(fromPath: path, to: nativeInstance) ?? false
if !success {
throw RuntimeError.error(withMessage: "Failed to replace ViewModel at path: \(path)")
}
}
func viewModelAsync(path: String) throws -> Promise<(any HybridViewModelInstanceSpec)?> {
return Promise.async { try self.viewModel(path: path) }
}
}