Skip to content

Commit d888d84

Browse files
committed
feat: add viewModelInstance to ViewProps, so we don't need to wait for ref
1 parent a4ed4ec commit d888d84

21 files changed

+149
-51
lines changed

android/src/main/java/com/margelo/nitro/rive/HybridRiveView.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
5656
override var alignment: Alignment? = null
5757
override var fit: Fit? = null
5858
override var layoutScaleFactor: Double? = null
59+
override var bind: HybridViewModelInstanceSpec? = null
5960
//endregion
6061

6162
//region View Methods
@@ -112,6 +113,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
112113
val hybridFile = file as? HybridRiveFile
113114
val riveFile = hybridFile?.riveFile ?: return
114115

116+
val viewModelInstance = (bind as? HybridViewModelInstance)?.viewModelInstance
115117
val config = ViewConfiguration(
116118
artboardName = artboardName,
117119
stateMachineName = stateMachineName,
@@ -121,6 +123,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
121123
alignment = convertAlignment(alignment) ?: DefaultConfiguration.ALIGNMENT,
122124
fit = convertFit(fit) ?: DefaultConfiguration.FIT,
123125
layoutScaleFactor = layoutScaleFactor?.toFloat() ?: DefaultConfiguration.LAYOUTSCALEFACTOR,
126+
bind = viewModelInstance,
124127
)
125128
view.configure(config, needsReload)
126129

android/src/main/java/com/rive/RiveReactNativeView.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ data class ViewConfiguration(
2929
val riveFile: File,
3030
val alignment: Alignment,
3131
val fit: Fit,
32-
val layoutScaleFactor: Float?
32+
val layoutScaleFactor: Float?,
33+
val bind: ViewModelInstance?
3334
)
3435

3536
@SuppressLint("ViewConstructor")
@@ -67,6 +68,14 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
6768
// TODO: this seems to require a reload for the view to take the new value (bug on Android)
6869
riveAnimationView?.layoutScaleFactor = config.layoutScaleFactor
6970
}
71+
72+
config.bind?.let { vmi ->
73+
val stateMachines = riveAnimationView?.controller?.stateMachines
74+
if (!stateMachines.isNullOrEmpty()) {
75+
stateMachines.first().viewModelInstance = vmi
76+
}
77+
}
78+
7079
viewReadyDeferred.complete(true)
7180
}
7281

example/src/pages/RiveDataBindingExample.tsx

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useEffect, useMemo } from 'react';
33
import {
44
Fit,
55
RiveView,
6-
useRive,
76
useRiveNumber,
87
type ViewModelInstance,
98
type RiveFile,
@@ -61,14 +60,6 @@ function DataBindingExample({
6160
instance: ViewModelInstance;
6261
file: RiveFile;
6362
}) {
64-
const { riveViewRef, setHybridRef } = useRive();
65-
66-
useEffect(() => {
67-
if (riveViewRef) {
68-
riveViewRef.bindViewModelInstance(instance);
69-
}
70-
}, [riveViewRef, instance]);
71-
7263
const { error: coinValueError } = useRiveNumber('Coin/Item_Value', instance);
7364

7465
if (coinValueError) {
@@ -106,10 +97,10 @@ function DataBindingExample({
10697
style={styles.rive}
10798
autoBind={false}
10899
autoPlay={true}
100+
bind={instance}
109101
fit={Fit.Layout}
110102
layoutScaleFactor={1}
111103
file={file}
112-
hybridRef={setHybridRef}
113104
/>
114105
);
115106
}

ios/HybridRiveView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ private struct DefaultConfiguration {
1212
}
1313

1414
class HybridRiveView : HybridRiveViewSpec {
15+
var bind: (any HybridViewModelInstanceSpec)?
16+
1517
// MARK: View Props
1618
var artboardName: String? { didSet { needsReload = true } }
1719
var stateMachineName: String? { didSet { needsReload = true } }
@@ -86,6 +88,7 @@ class HybridRiveView : HybridRiveViewSpec {
8688
guard let hybridFile = file as? HybridRiveFile,
8789
let file = hybridFile.riveFile else { return }
8890

91+
let viewModelInstance = (bind as? HybridViewModelInstance)?.viewModelInstance
8992
let config = ViewConfiguration(
9093
artboardName: artboardName,
9194
stateMachineName: stateMachineName,
@@ -95,7 +98,8 @@ class HybridRiveView : HybridRiveViewSpec {
9598
viewSource: hybridFile,
9699
alignment: convertAlignment(alignment) ?? DefaultConfiguration.alignment,
97100
fit: convertFit(fit) ?? DefaultConfiguration.fit,
98-
layoutScaleFactor: layoutScaleFactor ?? DefaultConfiguration.layoutScaleFactor
101+
layoutScaleFactor: layoutScaleFactor ?? DefaultConfiguration.layoutScaleFactor,
102+
bind: viewModelInstance
99103
)
100104

101105
try? getRiveView().configure(config, reload: needsReload)

ios/RiveReactNativeView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct ViewConfiguration {
1717
let alignment: RiveRuntime.RiveAlignment
1818
let fit: RiveRuntime.RiveFit
1919
let layoutScaleFactor: Double
20+
let bind: RiveDataBindingViewModel.Instance?
2021
}
2122

2223
class RiveReactNativeView: UIView, RiveStateMachineDelegate {
@@ -66,6 +67,10 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
6667
viewReadyContinuation?.resume()
6768
viewReadyContinuation = nil
6869
}
70+
71+
if let bind = config.bind {
72+
baseViewModel?.riveModel?.stateMachine?.bind(viewModelInstance: bind)
73+
}
6974
}
7075

7176
func bindViewModelInstance(viewModelInstance: RiveDataBindingViewModel.Instance) {

nitrogen/generated/android/c++/JHybridRiveViewSpec.cpp

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/JHybridRiveViewSpec.hpp

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/c++/views/JHybridRiveViewStateUpdater.cpp

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveViewSpec.kt

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitrogen/generated/ios/Rive-Swift-Cxx-Bridge.cpp

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)