Skip to content

Commit 582d502

Browse files
committed
fix(android): use getDefaultViewModelInfo for ViewModel name resolution
getDefaultViewModelInfo is available since rive-android 11.3.2. This removes the null ViewModel name workaround for defaultArtboardViewModel(), enabling createInstanceByName and other name-dependent operations.
1 parent 9d358c0 commit 582d502

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

android/src/new/java/com/margelo/nitro/rive/HybridRiveFile.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,8 @@ class HybridRiveFile(
9999
Artboard.fromFile(file)
100100
}
101101
val vmSource = ViewModelSource.DefaultForArtboard(artboard)
102-
// Name is null because the Rive Android SDK does not expose the ViewModel name
103-
// from a ViewModelInstance — name-dependent operations will throw UnsupportedOperationException.
104-
// Track upstream: https://github.com/rive-app/rive-android/issues/XXX
105-
return HybridViewModel(file, riveWorker, null, this, vmSource)
102+
val vmInfo = file.getDefaultViewModelInfo(artboard)
103+
return HybridViewModel(file, riveWorker, vmInfo.viewModelName, this, vmSource)
106104
}
107105

108106
// Deprecated: Use defaultArtboardViewModelAsync instead

android/src/new/java/com/margelo/nitro/rive/HybridViewModel.kt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,18 @@ import kotlinx.coroutines.runBlocking
1414
class HybridViewModel(
1515
private val riveFile: RiveFile,
1616
private val riveWorker: CommandQueue,
17-
// Null when constructed via DefaultForArtboard — the Rive Android SDK does not expose
18-
// the ViewModel name from a ViewModelInstance, so name-dependent operations are unavailable.
19-
// Track: https://github.com/rive-app/rive-android/issues/XXX
2017
private val viewModelName: String?,
2118
private val parentFile: HybridRiveFile,
2219
private val vmSource: ViewModelSource
2320
) : HybridViewModelSpec() {
2421
companion object {
2522
private const val TAG = "HybridViewModel"
26-
private const val NO_NAME_ERROR =
27-
"This operation requires the ViewModel name, which is unavailable for ViewModels " +
28-
"obtained via defaultArtboardViewModel(). The Rive Android SDK does not yet expose " +
29-
"the ViewModel name from a ViewModelInstance. Use a named ViewModel instead, or " +
30-
"track the upstream fix: https://github.com/rive-app/rive-android/issues/XXX"
3123
}
3224

3325
override val propertyCount: Double
3426
get() {
3527
DeprecationWarning.warn("propertyCount", "getPropertyCountAsync")
36-
val name = viewModelName ?: throw UnsupportedOperationException(NO_NAME_ERROR)
28+
val name = viewModelName ?: throw UnsupportedOperationException("ViewModel name is unavailable")
3729
return try {
3830
runBlocking { riveFile.getViewModelProperties(name) }.size.toDouble()
3931
} catch (e: Exception) {
@@ -45,7 +37,7 @@ class HybridViewModel(
4537
override val instanceCount: Double
4638
get() {
4739
DeprecationWarning.warn("instanceCount", "getInstanceCountAsync")
48-
val name = viewModelName ?: throw UnsupportedOperationException(NO_NAME_ERROR)
40+
val name = viewModelName ?: throw UnsupportedOperationException("ViewModel name is unavailable")
4941
return try {
5042
runBlocking { riveFile.getViewModelInstanceNames(name) }.size.toDouble()
5143
} catch (e: Exception) {
@@ -55,22 +47,22 @@ class HybridViewModel(
5547
}
5648

5749
override val modelName: String
58-
get() = viewModelName ?: throw UnsupportedOperationException(NO_NAME_ERROR)
50+
get() = viewModelName ?: throw UnsupportedOperationException("ViewModel name is unavailable")
5951

6052
override fun getPropertyCountAsync(): Promise<Double> {
61-
val name = viewModelName ?: return Promise.rejected(UnsupportedOperationException(NO_NAME_ERROR))
53+
val name = viewModelName ?: return Promise.rejected(UnsupportedOperationException("ViewModel name is unavailable"))
6254
return Promise.async { riveFile.getViewModelProperties(name).size.toDouble() }
6355
}
6456

6557
override fun getInstanceCountAsync(): Promise<Double> {
66-
val name = viewModelName ?: return Promise.rejected(UnsupportedOperationException(NO_NAME_ERROR))
58+
val name = viewModelName ?: return Promise.rejected(UnsupportedOperationException("ViewModel name is unavailable"))
6759
return Promise.async { riveFile.getViewModelInstanceNames(name).size.toDouble() }
6860
}
6961

7062
// Deprecated: Use createInstanceByNameAsync instead
7163
override fun createInstanceByIndex(index: Double): HybridViewModelInstanceSpec? {
7264
DeprecationWarning.warn("createInstanceByIndex", "createInstanceByNameAsync")
73-
val name = viewModelName ?: throw UnsupportedOperationException(NO_NAME_ERROR)
65+
val name = viewModelName ?: throw UnsupportedOperationException("ViewModel name is unavailable")
7466
return try {
7567
val idx = index.toInt()
7668
val instanceNames = runBlocking { riveFile.getViewModelInstanceNames(name) }
@@ -86,7 +78,7 @@ class HybridViewModel(
8678
}
8779

8880
private suspend fun createInstanceByNameImpl(instanceName: String): HybridViewModelInstanceSpec? {
89-
val name = viewModelName ?: throw UnsupportedOperationException(NO_NAME_ERROR)
81+
val name = viewModelName ?: throw UnsupportedOperationException("ViewModel name is unavailable")
9082
val instanceNames = riveFile.getViewModelInstanceNames(name)
9183
if (!instanceNames.contains(instanceName)) return null
9284
val source = vmSource.namedInstance(instanceName)
@@ -97,7 +89,7 @@ class HybridViewModel(
9789
// Deprecated: Use createInstanceByNameAsync instead
9890
override fun createInstanceByName(name: String): HybridViewModelInstanceSpec? {
9991
DeprecationWarning.warn("createInstanceByName", "createInstanceByNameAsync")
100-
if (viewModelName == null) throw UnsupportedOperationException(NO_NAME_ERROR)
92+
if (viewModelName == null) throw UnsupportedOperationException("ViewModel name is unavailable")
10193
return try {
10294
runBlocking { createInstanceByNameImpl(name) }
10395
} catch (e: UnsupportedOperationException) {
@@ -109,7 +101,7 @@ class HybridViewModel(
109101
}
110102

111103
override fun createInstanceByNameAsync(name: String): Promise<HybridViewModelInstanceSpec?> {
112-
if (viewModelName == null) return Promise.rejected(UnsupportedOperationException(NO_NAME_ERROR))
104+
if (viewModelName == null) return Promise.rejected(UnsupportedOperationException("ViewModel name is unavailable"))
113105
return Promise.async { createInstanceByNameImpl(name) }
114106
}
115107

0 commit comments

Comments
 (0)