Skip to content

Commit b3a94ee

Browse files
committed
fix: dataBindingChanged
1 parent 78599a8 commit b3a94ee

File tree

4 files changed

+21
-37
lines changed

4 files changed

+21
-37
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
4949
//region State
5050
override val view: RiveReactNativeView = RiveReactNativeView(context)
5151
private var needsReload = false
52-
private var firstUpdate = true
52+
private var dataBindingChanged = false
5353
private var registeredFile: HybridRiveFile? = null
5454
//endregion
5555

@@ -81,7 +81,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
8181
set(value) {
8282
if (field != value) {
8383
field = value
84-
applyDataBinding()
84+
dataBindingChanged = true
8585
}
8686
}
8787
//endregion
@@ -136,20 +136,13 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
136136
view.getTextRunValue(name, path)
137137
//endregion
138138

139-
//region Data Binding
140-
private fun applyDataBinding() {
141-
view.applyDataBinding(dataBind.toBindData(), shouldRefresh = !firstUpdate)
142-
}
143-
//endregion
144-
145139
//region Update
146140
fun refreshAfterAssetChange() {
147141
afterUpdate()
148142
}
149143

150144
override fun afterUpdate() {
151145
logged(TAG, "afterUpdate") {
152-
firstUpdate = false
153146
val hybridFile = file as? HybridRiveFile
154147
val riveFile = hybridFile?.riveFile ?: return@logged
155148

@@ -163,14 +156,15 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
163156
layoutScaleFactor = layoutScaleFactor?.toFloat() ?: DefaultConfiguration.LAYOUTSCALEFACTOR,
164157
bindData = dataBind.toBindData()
165158
)
166-
view.configure(config, needsReload)
159+
view.configure(config, dataBindingChanged=dataBindingChanged, needsReload)
167160

168161
if (needsReload && hybridFile != null) {
169162
hybridFile.registerView(this)
170163
registeredFile = hybridFile
171164
}
172165

173166
needsReload = false
167+
dataBindingChanged = false
174168
super.afterUpdate()
175169
}
176170
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
5757
return viewReadyDeferred.await()
5858
}
5959

60-
fun configure(config: ViewConfiguration, reload: Boolean = false) {
60+
fun configure(config: ViewConfiguration, dataBindingChanged: Boolean, reload: Boolean = false) {
6161
if (reload) {
6262
riveAnimationView?.setRiveFile(
6363
config.riveFile,
@@ -76,7 +76,9 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
7676
riveAnimationView?.layoutScaleFactor = config.layoutScaleFactor
7777
}
7878

79-
applyDataBinding(config.bindData, shouldRefresh = false)
79+
if (dataBindingChanged) {
80+
applyDataBinding(config.bindData)
81+
}
8082

8183
viewReadyDeferred.complete(true)
8284
}
@@ -97,7 +99,7 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
9799
}
98100
}
99101

100-
fun applyDataBinding(bindData: BindData, shouldRefresh: Boolean = false) {
102+
fun applyDataBinding(bindData: BindData) {
101103
val stateMachines = riveAnimationView?.controller?.stateMachines
102104
if (stateMachines.isNullOrEmpty()) return
103105

@@ -135,10 +137,8 @@ class RiveReactNativeView(context: ThemedReactContext) : FrameLayout(context) {
135137
}
136138
}
137139

138-
if (shouldRefresh) {
139-
stateMachine.name.let { smName ->
140-
riveAnimationView?.play(smName, isStateMachine = true)
141-
}
140+
stateMachine.name.let { smName ->
141+
riveAnimationView?.play(smName, isStateMachine = true)
142142
}
143143
}
144144

ios/HybridRiveView.swift

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class HybridRiveView: HybridRiveViewSpec {
4343
// MARK: View Props
4444
var dataBind: HybridDataBindMode? = nil {
4545
didSet {
46-
applyDataBinding()
46+
dataBindingChanged = true
4747
}
4848
}
4949
var artboardName: String? { didSet { needsReload = true } }
@@ -120,18 +120,8 @@ class HybridRiveView: HybridRiveViewSpec {
120120
return riveView
121121
}
122122

123-
// MARK: Data Binding
124-
private func applyDataBinding() {
125-
logged(tag: "HybridRiveView", note: "applyDataBinding") {
126-
guard let riveView = view as? RiveReactNativeView else { return }
127-
let bindData = try dataBind.toDataBingMode()
128-
riveView.applyDataBinding(bindData, refresh: !firstUpdate)
129-
}
130-
}
131-
132123
// MARK: Update
133124
func afterUpdate() {
134-
firstUpdate = false
135125
logged(tag: "HybridRiveView", note: "afterUpdate") {
136126
guard let hybridFile = file as? HybridRiveFile,
137127
let file = hybridFile.riveFile
@@ -149,14 +139,15 @@ class HybridRiveView: HybridRiveViewSpec {
149139
bindData: try dataBind.toDataBingMode()
150140
)
151141

152-
try getRiveView().configure(config, reload: needsReload)
142+
try getRiveView().configure(config, dataBindingChanged: dataBindingChanged, reload: needsReload)
153143
needsReload = false
144+
dataBindingChanged = false
154145
}
155146
}
156147

157148
// MARK: Internal State
158-
private var firstUpdate = true
159149
private var needsReload = false
150+
private var dataBindingChanged = false
160151

161152
// MARK: Helpers
162153
private func convertAlignment(_ alignment: Alignment?) -> RiveAlignment? {

ios/RiveReactNativeView.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
3232
private var baseViewModel: RiveViewModel?
3333
private var eventListeners: [(UnifiedRiveEvent) -> Void] = []
3434
private var viewReadyContinuation: CheckedContinuation<Void, Never>?
35-
private var isFirstConfigure = true
3635
private var isViewReady = false
3736
private weak var viewSource: RiveViewSource?
3837

@@ -51,7 +50,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
5150
return true
5251
}
5352

54-
func configure(_ config: ViewConfiguration, reload: Bool = false) {
53+
func configure(_ config: ViewConfiguration, dataBindingChanged: Bool = false, reload: Bool = false) {
5554
if reload {
5655
cleanup()
5756
let model = RiveModel(riveFile: config.riveFile)
@@ -75,7 +74,9 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
7574
viewReadyContinuation = nil
7675
}
7776

78-
applyDataBinding(config.bindData, refresh: false)
77+
if dataBindingChanged {
78+
applyDataBinding(config.bindData)
79+
}
7980
}
8081

8182
func bindViewModelInstance(viewModelInstance: RiveDataBindingViewModel.Instance) {
@@ -86,7 +87,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
8687
return baseViewModel?.riveModel?.stateMachine?.viewModelInstance
8788
}
8889

89-
func applyDataBinding(_ bindData: BindData, refresh: Bool = false) {
90+
func applyDataBinding(_ bindData: BindData) {
9091
let stateMachine = baseViewModel?.riveModel?.stateMachine
9192
let artboard = baseViewModel?.riveModel?.artboard
9293

@@ -115,9 +116,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
115116
stateMachine?.bind(viewModelInstance: instance)
116117
artboard?.bind(viewModelInstance: instance)
117118
}
118-
if refresh {
119-
baseViewModel?.play()
120-
}
119+
baseViewModel?.play()
121120
}
122121

123122
func play() {

0 commit comments

Comments
 (0)