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
33 changes: 33 additions & 0 deletions android/src/main/java/com/margelo/nitro/rive/HybridRiveFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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.core.Promise
import java.lang.ref.WeakReference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -85,6 +86,38 @@ class HybridRiveFile : HybridRiveFileSpec() {
}
}

override fun getViewModelNamesAsync(): Promise<Array<String>> {
return Promise.async {
val file = riveFile ?: return@async emptyArray()
val count = file.viewModelCount
val names = mutableListOf<String>()
for (i in 0 until count) {
try {
val vm = file.getViewModelByIndex(i)
names.add(vm.name)
} catch (_: Exception) {
}
}
names.toTypedArray()
}
}

override fun viewModelByNameAsync(name: String, validate: Boolean?): Promise<HybridViewModelSpec?> {
return Promise.async { viewModelByName(name) }
}

override fun defaultArtboardViewModelAsync(artboardBy: ArtboardBy?): Promise<HybridViewModelSpec?> {
return Promise.async { defaultArtboardViewModel(artboardBy) }
}

override fun getArtboardCountAsync(): Promise<Double> {
return Promise.async { artboardCount }
}

override fun getArtboardNamesAsync(): Promise<Array<String>> {
return Promise.async { artboardNames }
}

override fun updateReferencedAssets(referencedAssets: ReferencedAssetsType) {
val assetsData = referencedAssets.data ?: return
val cache = referencedAssetCache ?: return
Expand Down
13 changes: 13 additions & 0 deletions android/src/main/java/com/margelo/nitro/rive/HybridViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModel
import app.rive.runtime.kotlin.core.errors.ViewModelException
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise

@Keep
@DoNotStrip
Expand Down Expand Up @@ -51,4 +52,16 @@ class HybridViewModel(private val viewModel: ViewModel) : HybridViewModelSpec()
return null
}
}

override fun createInstanceByNameAsync(name: String): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { createInstanceByName(name) }
}

override fun createDefaultInstanceAsync(): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { createDefaultInstance() }
}

override fun createBlankInstanceAsync(): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { createInstance() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelBooleanProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise

@Keep
@DoNotStrip
Expand All @@ -15,6 +16,14 @@ class HybridViewModelBooleanProperty(private val viewModelBoolean: ViewModelBool
viewModelBoolean.value = value
}

override fun getValueAsync(): Promise<Boolean> {
return Promise.async { value }
}

override fun set(value: Boolean) {
viewModelBoolean.value = value
}

override fun addListener(onChanged: (value: Boolean) -> Unit): () -> Unit {
val remover = addListenerInternal(onChanged)
ensureValueListenerJob(viewModelBoolean.valueFlow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelColorProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise

@Keep
@DoNotStrip
Expand All @@ -15,6 +16,14 @@ class HybridViewModelColorProperty(private val viewModelColor: ViewModelColorPro
viewModelColor.value = value.toLong().toInt()
}

override fun getValueAsync(): Promise<Double> {
return Promise.async { value }
}

override fun set(value: Double) {
viewModelColor.value = value.toLong().toInt()
}

override fun addListener(onChanged: (value: Double) -> Unit): () -> Unit {
val remover = addListenerInternal { intValue: Int -> onChanged(intValue.toDouble()) }
ensureValueListenerJob(viewModelColor.valueFlow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelEnumProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise

@Keep
@DoNotStrip
Expand All @@ -15,6 +16,14 @@ class HybridViewModelEnumProperty(private val viewModelEnum: ViewModelEnumProper
viewModelEnum.value = value
}

override fun getValueAsync(): Promise<String> {
return Promise.async { value }
}

override fun set(value: String) {
viewModelEnum.value = value
}

override fun addListener(onChanged: (value: String) -> Unit): () -> Unit {
val remover = addListenerInternal(onChanged)
ensureValueListenerJob(viewModelEnum.valueFlow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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
Expand Down Expand Up @@ -65,4 +66,8 @@ class HybridViewModelInstance(val viewModelInstance: ViewModelInstance) : Hybrid
val nativeInstance = (instance as HybridViewModelInstance).viewModelInstance
viewModelInstance.setInstanceProperty(path, nativeInstance)
}

override fun viewModelAsync(path: String): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { viewModel(path) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelListProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise
import kotlinx.coroutines.flow.map

@Keep
Expand Down Expand Up @@ -56,6 +57,14 @@ class HybridViewModelListProperty(private val listProperty: ViewModelListPropert
return true
}

override fun getLengthAsync(): Promise<Double> {
return Promise.async { length }
}

override fun getInstanceAtAsync(index: Double): Promise<HybridViewModelInstanceSpec?> {
return Promise.async { getInstanceAt(index) }
}

override fun addListener(onChanged: () -> Unit): () -> Unit {
val remover = addListenerInternal { _ -> onChanged() }
ensureValueListenerJob(listProperty.valueFlow.map { })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelNumberProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise
import kotlinx.coroutines.flow.map

@Keep
Expand All @@ -16,6 +17,14 @@ class HybridViewModelNumberProperty(private val viewModelNumber: ViewModelNumber
viewModelNumber.value = value.toFloat()
}

override fun getValueAsync(): Promise<Double> {
return Promise.async { value }
}

override fun set(value: Double) {
viewModelNumber.value = value.toFloat()
}

override fun addListener(onChanged: (value: Double) -> Unit): () -> Unit {
val remover = addListenerInternal(onChanged)
ensureValueListenerJob(viewModelNumber.valueFlow.map { it.toDouble() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.margelo.nitro.rive
import androidx.annotation.Keep
import app.rive.runtime.kotlin.core.ViewModelStringProperty
import com.facebook.proguard.annotations.DoNotStrip
import com.margelo.nitro.core.Promise

@Keep
@DoNotStrip
Expand All @@ -15,6 +16,14 @@ class HybridViewModelStringProperty(private val viewModelString: ViewModelString
viewModelString.value = value
}

override fun getValueAsync(): Promise<String> {
return Promise.async { value }
}

override fun set(value: String) {
viewModelString.value = value
}

override fun addListener(onChanged: (value: String) -> Unit): () -> Unit {
val remover = addListenerInternal(onChanged)
ensureValueListenerJob(viewModelString.valueFlow)
Expand Down
31 changes: 31 additions & 0 deletions ios/HybridRiveFile.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NitroModules
import RiveRuntime

typealias ReferencedAssetCache = [String: RiveFileAsset]
Expand Down Expand Up @@ -88,6 +89,36 @@ class HybridRiveFile: HybridRiveFileSpec, RiveViewSource {
return HybridBindableArtboard(bindableArtboard: bindable)
}

func getViewModelNamesAsync() throws -> Promise<[String]> {
return Promise.async {
guard let file = self.riveFile else { return [] }
let count = file.viewModelCount
var names: [String] = []
for i in 0..<count {
if let vm = file.viewModel(at: UInt(i)) {
names.append(vm.name)
}
}
return names
}
}

func viewModelByNameAsync(name: String, validate: Bool?) throws -> Promise<(any HybridViewModelSpec)?> {
return Promise.async { try self.viewModelByName(name: name) }
}

func defaultArtboardViewModelAsync(artboardBy: ArtboardBy?) throws -> Promise<(any HybridViewModelSpec)?> {
return Promise.async { try self.defaultArtboardViewModel(artboardBy: artboardBy) }
}

func getArtboardCountAsync() throws -> Promise<Double> {
return Promise.async { self.artboardCount }
}

func getArtboardNamesAsync() throws -> Promise<[String]> {
return Promise.async { self.artboardNames }
}

func updateReferencedAssets(referencedAssets: ReferencedAssetsType) {
guard let assetsData = referencedAssets.data,
let cache = referencedAssetCache,
Expand Down
13 changes: 13 additions & 0 deletions ios/HybridViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NitroModules
import RiveRuntime

class HybridViewModel: HybridViewModelSpec {
Expand Down Expand Up @@ -39,4 +40,16 @@ class HybridViewModel: HybridViewModelSpec {
let vmi = viewModel.createInstance() else { return nil }
return HybridViewModelInstance(viewModelInstance: vmi)
}

func createInstanceByNameAsync(name: String) throws -> Promise<(any HybridViewModelInstanceSpec)?> {
return Promise.async { try self.createInstanceByName(name: name) }
}

func createDefaultInstanceAsync() throws -> Promise<(any HybridViewModelInstanceSpec)?> {
return Promise.async { try self.createDefaultInstance() }
}

func createBlankInstanceAsync() throws -> Promise<(any HybridViewModelInstanceSpec)?> {
return Promise.async { try self.createInstance() }
}
}
8 changes: 8 additions & 0 deletions ios/HybridViewModelBooleanProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ class HybridViewModelBooleanProperty: HybridViewModelBooleanPropertySpec, Valued
property.value = newValue
}
}

func getValueAsync() throws -> Promise<Bool> {
return Promise.async { self.property.value }
}

func set(value: Bool) throws {
property.value = value
}
}
8 changes: 8 additions & 0 deletions ios/HybridViewModelColorProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ class HybridViewModelColorProperty: HybridViewModelColorPropertySpec, ValuedProp
}
}

func getValueAsync() throws -> Promise<Double> {
return Promise.async { self.property.value.toHexDouble() }
}

func set(value: Double) throws {
property.value = UIColor(argb: Int(value))
}

func addListener(onChanged: @escaping (Double) -> Void) throws -> () -> Void {
return helper.addListener { (color: UIColor) in
onChanged(color.toHexDouble())
Expand Down
8 changes: 8 additions & 0 deletions ios/HybridViewModelEnumProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ class HybridViewModelEnumProperty: HybridViewModelEnumPropertySpec, ValuedProper
property.value = newValue
}
}

func getValueAsync() throws -> Promise<String> {
return Promise.async { self.property.value }
}

func set(value: String) throws {
property.value = value
}
}
4 changes: 4 additions & 0 deletions ios/HybridViewModelInstance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ class HybridViewModelInstance: HybridViewModelInstanceSpec {
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) }
}
}
9 changes: 9 additions & 0 deletions ios/HybridViewModelListProperty.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NitroModules
import RiveRuntime

class HybridViewModelListProperty: HybridViewModelListPropertySpec, ValuedPropertyProtocol {
Expand Down Expand Up @@ -56,6 +57,14 @@ class HybridViewModelListProperty: HybridViewModelListPropertySpec, ValuedProper
return true
}

func getLengthAsync() throws -> Promise<Double> {
return Promise.async { self.length }
}

func getInstanceAtAsync(index: Double) throws -> Promise<(any HybridViewModelInstanceSpec)?> {
return Promise.async { try self.getInstanceAt(index: index) }
}

func addListener(onChanged: @escaping () -> Void) throws -> () -> Void {
helper.addListener({ _ in onChanged() })
}
Expand Down
9 changes: 9 additions & 0 deletions ios/HybridViewModelNumberProperty.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import NitroModules
import RiveRuntime

class HybridViewModelNumberProperty: HybridViewModelNumberPropertySpec, ValuedPropertyProtocol {
Expand All @@ -18,6 +19,14 @@ class HybridViewModelNumberProperty: HybridViewModelNumberPropertySpec, ValuedPr
}
}

func getValueAsync() throws -> Promise<Double> {
return Promise.async { Double(self.property.value) }
}

func set(value: Double) throws {
property.value = Float(value)
}

func addListener(onChanged: @escaping (Double) -> Void) throws -> () -> Void {
return helper.addListener({ floatValue in onChanged(Double(floatValue)) })
}
Expand Down
8 changes: 8 additions & 0 deletions ios/HybridViewModelStringProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ class HybridViewModelStringProperty: HybridViewModelStringPropertySpec, ValuedPr
property.value = newValue
}
}

func getValueAsync() throws -> Promise<String> {
return Promise.async { self.property.value }
}

func set(value: String) throws {
property.value = value
}
}
Loading
Loading