Skip to content

Commit d07d887

Browse files
committed
refator: simplify changes
1 parent 9bf2fbd commit d07d887

File tree

3 files changed

+43
-101
lines changed

3 files changed

+43
-101
lines changed

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

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,47 +136,7 @@ class HybridRiveView(val context: ThemedReactContext) : HybridRiveViewSpec() {
136136

137137
//region Data Binding
138138
private fun applyDataBinding() {
139-
val stateMachines = view.riveAnimationView?.controller?.stateMachines
140-
if (stateMachines.isNullOrEmpty()) return
141-
142-
val bindData = dataBind.toBindData()
143-
val stateMachine = stateMachines.first()
144-
145-
when (bindData) {
146-
is BindData.None -> {
147-
// Unbind by setting to null
148-
stateMachine.viewModelInstance = null
149-
}
150-
is BindData.Auto -> {
151-
// Get the default view model and create default instance
152-
val artboard = view.riveAnimationView?.controller?.activeArtboard
153-
val file = view.riveAnimationView?.controller?.file
154-
if (artboard != null && file != null) {
155-
val viewModel = file.defaultViewModelForArtboard(artboard)
156-
val instance = viewModel.createDefaultInstance()
157-
stateMachine.viewModelInstance = instance
158-
}
159-
}
160-
is BindData.Instance -> {
161-
stateMachine.viewModelInstance = bindData.instance
162-
}
163-
is BindData.ByName -> {
164-
val artboard = view.riveAnimationView?.controller?.activeArtboard
165-
val file = view.riveAnimationView?.controller?.file
166-
if (artboard != null && file != null) {
167-
val viewModel = file.defaultViewModelForArtboard(artboard)
168-
val instance = viewModel.createInstanceFromName(bindData.name)
169-
stateMachine.viewModelInstance = instance
170-
}
171-
}
172-
}
173-
174-
// Only play on subsequent updates, not first
175-
if (!firstUpdate) {
176-
view.riveAnimationView?.controller?.stateMachines?.first()?.name?.let { smName ->
177-
view.riveAnimationView?.play(smName, isStateMachine = true)
178-
}
179-
}
139+
view.applyDataBinding(dataBind.toBindData(), shouldRefresh = !firstUpdate)
180140
}
181141
//endregion
182142

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

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

ios/RiveReactNativeView.swift

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,30 +74,8 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
7474
viewReadyContinuation?.resume()
7575
viewReadyContinuation = nil
7676
}
77-
78-
let stateMachine = baseViewModel?.riveModel?.stateMachine
79-
let artboard = baseViewModel?.riveModel?.artboard
80-
switch config.bindData {
81-
case .none:
82-
baseViewModel?.riveModel?.disableAutoBind()
83-
84-
case .auto:
85-
baseViewModel?.riveModel?.enableAutoBind { [weak self] instance in
86-
// Callback invoked when default instance is auto-bound
87-
}
88-
89-
case .byName(let name):
90-
guard let artboard = artboard,
91-
let riveFile = baseViewModel?.riveModel?.riveFile,
92-
let viewModel = riveFile.defaultViewModel(for: artboard),
93-
let instance = viewModel.createInstance(fromName: name) else {
94-
break
95-
}
96-
stateMachine?.bind(viewModelInstance: instance)
9777

98-
case .instance(let instance):
99-
stateMachine?.bind(viewModelInstance: instance)
100-
}
78+
applyDataBinding(config.bindData, refresh: false)
10179
}
10280

10381
func bindViewModelInstance(viewModelInstance: RiveDataBindingViewModel.Instance) {

0 commit comments

Comments
 (0)