Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.File
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.NitroModules
import java.lang.ref.WeakReference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,45 @@ class HybridViewModelInstance(val viewModelInstance: ViewModelInstance) : Hybrid
override val instanceName: String
get() = viewModelInstance.name

override fun numberProperty(path: String): HybridViewModelNumberPropertySpec? {
try {
val numberProper = viewModelInstance.getNumberProperty(path)
return HybridViewModelNumberProperty(numberProper)
// Returns null if ViewModelException is thrown for iOS parity
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! This is what we did from the start. Nice abstration then!

// (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) {
return null
null
}
}

override fun stringProperty(path: String): HybridViewModelStringPropertySpec? {
try {
val stringProperty = viewModelInstance.getStringProperty(path)
return HybridViewModelStringProperty(stringProperty)
} catch (e: ViewModelException) {
return null
}
override fun numberProperty(path: String) = getPropertyOrNull {
HybridViewModelNumberProperty(viewModelInstance.getNumberProperty(path))
}

override fun booleanProperty(path: String): HybridViewModelBooleanPropertySpec? {
try {
val booleanProperty = viewModelInstance.getBooleanProperty(path)
return HybridViewModelBooleanProperty(booleanProperty)
} catch (e: ViewModelException) {
return null
}
override fun stringProperty(path: String) = getPropertyOrNull {
HybridViewModelStringProperty(viewModelInstance.getStringProperty(path))
}

override fun colorProperty(path: String): HybridViewModelColorPropertySpec? {
try {
val colorProperty = viewModelInstance.getColorProperty(path)
return HybridViewModelColorProperty(colorProperty)
} catch (e: ViewModelException) {
return null
}
override fun booleanProperty(path: String) = getPropertyOrNull {
HybridViewModelBooleanProperty(viewModelInstance.getBooleanProperty(path))
}

override fun enumProperty(path: String): HybridViewModelEnumPropertySpec? {
try {
val enumProperty = viewModelInstance.getEnumProperty(path)
return HybridViewModelEnumProperty(enumProperty)
} catch (e: ViewModelException) {
return null
}
override fun colorProperty(path: String) = getPropertyOrNull {
HybridViewModelColorProperty(viewModelInstance.getColorProperty(path))
}

override fun triggerProperty(path: String): HybridViewModelTriggerPropertySpec? {
try {
val triggerProperty = viewModelInstance.getTriggerProperty(path)
return HybridViewModelTriggerProperty(triggerProperty)
} catch (e: ViewModelException) {
return null
}
override fun enumProperty(path: String) = getPropertyOrNull {
HybridViewModelEnumProperty(viewModelInstance.getEnumProperty(path))
}

override fun imageProperty(path: String): HybridViewModelImagePropertySpec? {
try {
val imageProperty = viewModelInstance.getImageProperty(path)
return HybridViewModelImageProperty(imageProperty)
} catch (e: ViewModelException) {
return null
}
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))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.margelo.nitro.rive

import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelListProperty
import com.facebook.proguard.annotations.DoNotStrip
import kotlinx.coroutines.flow.map

@Keep
@DoNotStrip
class HybridViewModelListProperty(private val listProperty: ViewModelListProperty) :
HybridViewModelListPropertySpec(),
BaseHybridViewModelProperty<Unit> by BaseHybridViewModelPropertyImpl() {
override val length: Double
get() = listProperty.size.toDouble()

private fun requireHybridInstance(instance: HybridViewModelInstanceSpec): HybridViewModelInstance {
return instance as? HybridViewModelInstance
?: throw IllegalArgumentException("Expected HybridViewModelInstance but got ${instance::class.simpleName}")
}

override fun getInstanceAt(index: Double): HybridViewModelInstanceSpec? {
val idx = index.toInt()
if (idx < 0 || idx >= listProperty.size) return null
return HybridViewModelInstance(listProperty.elementAt(idx))
}

override fun addInstance(instance: HybridViewModelInstanceSpec) {
val hybridInstance = requireHybridInstance(instance)
listProperty.add(hybridInstance.viewModelInstance)
}

override fun addInstanceAt(instance: HybridViewModelInstanceSpec, index: Double): Boolean {
val hybridInstance = requireHybridInstance(instance)
val idx = index.toInt()
if (idx < 0 || idx > listProperty.size) return false
listProperty.add(idx, hybridInstance.viewModelInstance)
return true
}

override fun removeInstance(instance: HybridViewModelInstanceSpec) {
val hybridInstance = requireHybridInstance(instance)
listProperty.remove(hybridInstance.viewModelInstance)
}

override fun removeInstanceAt(index: Double) {
listProperty.removeAt(index.toInt())
}

override fun swap(index1: Double, index2: Double): Boolean {
val idx1 = index1.toInt()
val idx2 = index2.toInt()
if (idx1 < 0 || idx1 >= listProperty.size || idx2 < 0 || idx2 >= listProperty.size) {
return false
}
listProperty.swap(idx1, idx2)
return true
}

override fun addListener(onChanged: () -> Unit): () -> Unit {
val remover = addListenerInternal { _ -> onChanged() }
ensureValueListenerJob(listProperty.valueFlow.map { })
return remover
}
}
Binary file added example/assets/list.riv
Binary file not shown.
Loading
Loading