@@ -45,7 +45,6 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
4545 private var eventListeners: MutableList <RiveFileController .RiveEventListener > = mutableListOf ()
4646 private val viewReadyDeferred = CompletableDeferred <Boolean >()
4747 private var _activeStateMachineName : String? = null
48- private var isFirstConfigure = true
4948
5049 init {
5150 riveAnimationView = RiveAnimationView (context)
@@ -58,8 +57,6 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
5857 }
5958
6059 fun configure (config : ViewConfiguration , reload : Boolean = false) {
61- val wasFirstConfigure = isFirstConfigure
62-
6360 if (reload) {
6461 riveAnimationView?.setRiveFile(
6562 config.riveFile,
@@ -71,47 +68,14 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
7168 fit = config.fit
7269 )
7370 _activeStateMachineName = getSafeStateMachineName()
74-
75- // Play state machine after reload (but not on first configure)
76- if (! wasFirstConfigure) {
77- _activeStateMachineName ?.let { smName ->
78- riveAnimationView?.play(smName, isStateMachine = true )
79- }
80- }
81-
82- isFirstConfigure = false
8371 } else {
8472 riveAnimationView?.alignment = config.alignment
8573 riveAnimationView?.fit = config.fit
8674 // TODO: this seems to require a reload for the view to take the new value (bug on Android)
8775 riveAnimationView?.layoutScaleFactor = config.layoutScaleFactor
8876 }
8977
90- val stateMachines = riveAnimationView?.controller?.stateMachines
91- when (val bindData = config.bindData) {
92- is BindData .None -> {
93- // No binding
94- }
95- is BindData .Auto -> {
96- // Auto-binding handled by setRiveFile above
97- }
98- is BindData .Instance -> {
99- if (! stateMachines.isNullOrEmpty()) {
100- stateMachines.first().viewModelInstance = bindData.instance
101- }
102- }
103- is BindData .ByName -> {
104- val artboard = riveAnimationView?.controller?.activeArtboard
105- val file = riveAnimationView?.controller?.file
106- if (artboard != null && file != null ) {
107- val viewModel = file.defaultViewModelForArtboard(artboard)
108- val instance = viewModel.createInstanceFromName(bindData.name)
109- if (! stateMachines.isNullOrEmpty()) {
110- stateMachines.first().viewModelInstance = instance
111- }
112- }
113- }
114- }
78+ applyDataBinding(config.bindData, shouldRefresh = false )
11579
11680 viewReadyDeferred.complete(true )
11781 }
@@ -132,6 +96,46 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
13296 }
13397 }
13498
99+ fun applyDataBinding (bindData : BindData , shouldRefresh : Boolean = false) {
100+ val stateMachines = riveAnimationView?.controller?.stateMachines
101+ if (stateMachines.isNullOrEmpty()) return
102+
103+ val stateMachine = stateMachines.first()
104+
105+ when (bindData) {
106+ is BindData .None -> {
107+ stateMachine.viewModelInstance = null
108+ }
109+ is BindData .Auto -> {
110+ val artboard = riveAnimationView?.controller?.activeArtboard
111+ val file = riveAnimationView?.controller?.file
112+ if (artboard != null && file != null ) {
113+ val viewModel = file.defaultViewModelForArtboard(artboard)
114+ val instance = viewModel.createDefaultInstance()
115+ stateMachine.viewModelInstance = instance
116+ }
117+ }
118+ is BindData .Instance -> {
119+ stateMachine.viewModelInstance = bindData.instance
120+ }
121+ is BindData .ByName -> {
122+ val artboard = riveAnimationView?.controller?.activeArtboard
123+ val file = riveAnimationView?.controller?.file
124+ if (artboard != null && file != null ) {
125+ val viewModel = file.defaultViewModelForArtboard(artboard)
126+ val instance = viewModel.createInstanceFromName(bindData.name)
127+ stateMachine.viewModelInstance = instance
128+ }
129+ }
130+ }
131+
132+ if (shouldRefresh) {
133+ stateMachine.name.let { smName ->
134+ riveAnimationView?.play(smName, isStateMachine = true )
135+ }
136+ }
137+ }
138+
135139 fun play () = riveAnimationView?.play()
136140
137141 fun pause () = riveAnimationView?.pause();
0 commit comments