-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHybridViewModelInstance.kt
More file actions
160 lines (142 loc) · 5.31 KB
/
HybridViewModelInstance.kt
File metadata and controls
160 lines (142 loc) · 5.31 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
package com.margelo.nitro.rive
import android.util.Log
import androidx.annotation.Keep
import app.rive.ViewModelInstance
import app.rive.ViewModelInstanceSource
import app.rive.core.CommandQueue
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
@Keep
@DoNotStrip
class HybridViewModelInstance(
internal val viewModelInstance: ViewModelInstance,
private val riveWorker: CommandQueue,
private val parentFile: HybridRiveFile,
private val viewModelName: String? = null,
private val _instanceName: String? = null
) : HybridViewModelInstanceSpec() {
companion object {
private const val TAG = "HybridViewModelInstance"
}
private val propertyNames: Set<String> by lazy {
val name = viewModelName ?: return@lazy emptySet()
val file = parentFile.riveFile ?: return@lazy emptySet()
try {
runBlocking { file.getViewModelProperties(name) }.map { it.name }.toSet()
} catch (e: Exception) {
Log.e(TAG, "Failed to fetch property names for viewModel '$name'", e)
emptySet()
}
}
private fun hasProperty(path: String): Boolean {
if (propertyNames.isEmpty()) return true
return propertyNames.contains(path)
}
// TODO: Workaround — rive-android experimental SDK doesn't expose ViewModelInstance.name.
// Only works when caller knows the name (createInstanceByName). Falls back to "" otherwise.
override val instanceName: String
get() = _instanceName ?: ""
override fun numberProperty(path: String): HybridViewModelNumberPropertySpec? {
return try {
runBlocking { viewModelInstance.getNumberFlow(path).first() }
HybridViewModelNumberProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "numberProperty failed for path '$path'", e)
null
}
}
override fun stringProperty(path: String): HybridViewModelStringPropertySpec? {
return try {
runBlocking { viewModelInstance.getStringFlow(path).first() }
HybridViewModelStringProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "stringProperty failed for path '$path'", e)
null
}
}
override fun booleanProperty(path: String): HybridViewModelBooleanPropertySpec? {
return try {
runBlocking { viewModelInstance.getBooleanFlow(path).first() }
HybridViewModelBooleanProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "booleanProperty failed for path '$path'", e)
null
}
}
override fun colorProperty(path: String): HybridViewModelColorPropertySpec? {
return try {
runBlocking { viewModelInstance.getColorFlow(path).first() }
HybridViewModelColorProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "colorProperty failed for path '$path'", e)
null
}
}
override fun enumProperty(path: String): HybridViewModelEnumPropertySpec? {
return try {
runBlocking { viewModelInstance.getEnumFlow(path).first() }
HybridViewModelEnumProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "enumProperty failed for path '$path'", e)
null
}
}
override fun triggerProperty(path: String): HybridViewModelTriggerPropertySpec? {
if (!hasProperty(path)) return null
return try {
HybridViewModelTriggerProperty(viewModelInstance, path)
} catch (e: Exception) {
Log.e(TAG, "triggerProperty failed for path '$path'", e)
null
}
}
override fun imageProperty(path: String): HybridViewModelImagePropertySpec? {
return try {
HybridViewModelImageProperty(viewModelInstance, path, riveWorker)
} catch (e: Exception) {
Log.e(TAG, "imageProperty failed for path '$path'", e)
null
}
}
override fun listProperty(path: String): HybridViewModelListPropertySpec? {
return try {
HybridViewModelListProperty(viewModelInstance, path, riveWorker, parentFile)
} catch (e: Exception) {
Log.e(TAG, "listProperty failed for path '$path'", e)
null
}
}
override fun artboardProperty(path: String): HybridViewModelArtboardPropertySpec? {
return try {
HybridViewModelArtboardProperty(viewModelInstance, path, parentFile)
} catch (e: Exception) {
Log.e(TAG, "artboardProperty failed for path '$path'", e)
null
}
}
private fun viewModelImpl(path: String): HybridViewModelInstanceSpec? {
if (!hasProperty(path)) return null
val file = parentFile.riveFile ?: return null
val source = ViewModelInstanceSource.Reference(viewModelInstance, path)
val childVmi = ViewModelInstance.fromFile(file, source)
return HybridViewModelInstance(childVmi, riveWorker, parentFile)
}
// Deprecated: Use viewModelAsync instead
override fun viewModel(path: String): HybridViewModelInstanceSpec? {
DeprecationWarning.warn("viewModel", "viewModelAsync")
return try {
viewModelImpl(path)
} catch (e: Exception) {
RiveLog.e(TAG, "viewModel failed for path '$path': ${e.message}")
null
}
}
override fun viewModelAsync(path: String): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { viewModelImpl(path) }
}
override fun replaceViewModel(path: String, instance: HybridViewModelInstanceSpec) {
Log.w(TAG, "replaceViewModel not yet supported in experimental API")
}
}