-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHybridViewModelInstance.kt
More file actions
73 lines (58 loc) · 2.54 KB
/
HybridViewModelInstance.kt
File metadata and controls
73 lines (58 loc) · 2.54 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
package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelInstance
import app.rive.runtime.kotlin.core.errors.ViewModelException
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise
@Keep
@DoNotStrip
class HybridViewModelInstance(val viewModelInstance: ViewModelInstance) : HybridViewModelInstanceSpec() {
override val instanceName: String
get() = viewModelInstance.name
// Returns null if ViewModelException is thrown for iOS parity
// (iOS SDK returns nil when property not found, Android SDK throws)
private inline fun <T> getPropertyOrNull(block: () -> T): T? {
return try {
block()
} catch (e: ViewModelException) {
null
}
}
override fun numberProperty(path: String) = getPropertyOrNull {
HybridViewModelNumberProperty(viewModelInstance.getNumberProperty(path))
}
override fun stringProperty(path: String) = getPropertyOrNull {
HybridViewModelStringProperty(viewModelInstance.getStringProperty(path))
}
override fun booleanProperty(path: String) = getPropertyOrNull {
HybridViewModelBooleanProperty(viewModelInstance.getBooleanProperty(path))
}
override fun colorProperty(path: String) = getPropertyOrNull {
HybridViewModelColorProperty(viewModelInstance.getColorProperty(path))
}
override fun enumProperty(path: String) = getPropertyOrNull {
HybridViewModelEnumProperty(viewModelInstance.getEnumProperty(path))
}
override fun triggerProperty(path: String) = getPropertyOrNull {
HybridViewModelTriggerProperty(viewModelInstance.getTriggerProperty(path))
}
override fun imageProperty(path: String) = getPropertyOrNull {
HybridViewModelImageProperty(viewModelInstance.getImageProperty(path))
}
override fun listProperty(path: String) = getPropertyOrNull {
HybridViewModelListProperty(viewModelInstance.getListProperty(path))
}
override fun artboardProperty(path: String) = getPropertyOrNull {
HybridViewModelArtboardProperty(viewModelInstance.getArtboardProperty(path))
}
override fun viewModel(path: String) = getPropertyOrNull {
HybridViewModelInstance(viewModelInstance.getInstanceProperty(path))
}
override fun replaceViewModel(path: String, instance: HybridViewModelInstanceSpec) {
val nativeInstance = (instance as HybridViewModelInstance).viewModelInstance
viewModelInstance.setInstanceProperty(path, nativeInstance)
}
override fun viewModelAsync(path: String): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { viewModel(path) }
}
}