Skip to content

Commit 233cd12

Browse files
committed
review fixes
1 parent 21ae834 commit 233cd12

File tree

8 files changed

+66
-71
lines changed

8 files changed

+66
-71
lines changed

example/src/pages/OutOfBandAssets.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
/*Rive, */ Fit,
1212
/*RNRiveError,*/ useRiveFile,
1313
RiveView,
14-
DataBindMode,
1514
} from 'react-native-rive';
1615
import { Picker } from '@react-native-picker/picker';
1716
import { type Metadata } from '../helpers/metadata';

example/src/pages/RiveEventsExample.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
useRiveFile,
88
type RiveEvent,
99
RiveEventType,
10-
DataBindMode,
1110
} from 'react-native-rive';
1211
import { type Metadata } from '../helpers/metadata';
1312

example/src/pages/RiveFileLoadingExample.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
useRive,
1212
useRiveFile,
1313
type RiveFileInput,
14-
DataBindMode,
1514
} from 'react-native-rive';
1615
import { useState, useEffect } from 'react';
1716
import { downloadFileAsArrayBuffer } from '../helpers/fileHelpers';

example/src/pages/RiveStateMachineInputsExample.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
22
import { useEffect } from 'react';
3-
import {
4-
Fit,
5-
RiveView,
6-
useRive,
7-
useRiveFile,
8-
DataBindMode,
9-
} from 'react-native-rive';
3+
import { Fit, RiveView, useRive, useRiveFile } from 'react-native-rive';
104
import { type Metadata } from '../helpers/metadata';
115

126
export default function StateMachineInputsExample() {

example/src/pages/RiveTextRunExample.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
22
import { useEffect } from 'react';
3-
import {
4-
Fit,
5-
RiveView,
6-
useRive,
7-
useRiveFile,
8-
DataBindMode,
9-
} from 'react-native-rive';
3+
import { Fit, RiveView, useRive, useRiveFile } from 'react-native-rive';
104
import { type Metadata } from '../helpers/metadata';
115

126
export default function TextRunExample() {

example/src/pages/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ export { default as EventsExample } from './RiveEventsExample';
55
export { default as StateMachineInputsExample } from './RiveStateMachineInputsExample';
66
export { default as TextRunExample } from './RiveTextRunExample';
77
export { default as OutOfBandAssets } from './OutOfBandAssets';
8-
export { default as MiklosViewModels } from './MIklosViewModels';
8+
export { default as ManyViewModels } from './ManyViewModels';

ios/HybridRiveView.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ private struct DefaultConfiguration {
1010
static let layoutScaleFactor = RiveRuntime.RiveViewModel.layoutScaleFactorAutomatic
1111
}
1212

13+
typealias HybridDataBindMode = Variant__any_HybridViewModelInstanceSpec__DataBindMode_DataBindByName
14+
1315
extension Optional
14-
where Wrapped == Variant__any_HybridViewModelInstanceSpec__DataBindMode_DataBindByName {
16+
where Wrapped == HybridDataBindMode {
1517
func toDataBingMode() throws -> BindData {
1618
guard let value = self else {
1719
return .auto
@@ -38,14 +40,12 @@ where Wrapped == Variant__any_HybridViewModelInstanceSpec__DataBindMode_DataBind
3840
}
3941

4042
class HybridRiveView: HybridRiveViewSpec {
41-
var firstUpdate = true
42-
var dataBind: Variant__any_HybridViewModelInstanceSpec__DataBindMode_DataBindByName? = nil {
43+
// MARK: View Props
44+
var dataBind: HybridDataBindMode? = nil {
4345
didSet {
4446
applyDataBinding()
4547
}
4648
}
47-
48-
// MARK: View Props
4949
var artboardName: String? { didSet { needsReload = true } }
5050
var stateMachineName: String? { didSet { needsReload = true } }
5151
var autoPlay: Bool? { didSet { needsReload = true } }
@@ -155,6 +155,7 @@ class HybridRiveView: HybridRiveViewSpec {
155155
}
156156

157157
// MARK: Internal State
158+
private var firstUpdate = true
158159
private var needsReload = false
159160

160161
// MARK: Helpers

ios/RiveReactNativeView.swift

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import UIKit
2-
import RiveRuntime
31
import NitroModules
2+
import RiveRuntime
3+
import UIKit
44

55
protocol RiveViewSource: AnyObject {
66
func registerView(_ view: RiveReactNativeView)
@@ -38,10 +38,10 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
3838

3939
// MARK: Public Config Properties
4040
var autoPlay: Bool = true
41-
41+
4242
// MARK: - Public Methods
43-
44-
func awaitViewReady()async -> Bool {
43+
44+
func awaitViewReady() async -> Bool {
4545
if !isViewReady {
4646
await withCheckedContinuation { continuation in
4747
viewReadyContinuation = continuation
@@ -50,7 +50,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
5050
}
5151
return true
5252
}
53-
53+
5454
func configure(_ config: ViewConfiguration, reload: Bool = false) {
5555
if reload {
5656
cleanup()
@@ -77,7 +77,7 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
7777

7878
applyDataBinding(config.bindData, refresh: false)
7979
}
80-
80+
8181
func bindViewModelInstance(viewModelInstance: RiveDataBindingViewModel.Instance) {
8282
baseViewModel?.riveModel?.stateMachine?.bind(viewModelInstance: viewModelInstance)
8383
}
@@ -93,36 +93,37 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
9393
switch bindData {
9494
case .none:
9595
baseViewModel?.riveModel?.disableAutoBind()
96-
9796

9897
case .auto:
99-
baseViewModel?.riveModel?.enableAutoBind { [weak self] instance in
98+
baseViewModel?.riveModel?.enableAutoBind { instance in
10099
// Auto-bind callback
101100
}
102101

103102
case .byName(let name):
104103
guard let artboard = artboard,
105-
let riveFile = baseViewModel?.riveModel?.riveFile,
106-
let viewModel = riveFile.defaultViewModel(for: artboard),
107-
let instance = viewModel.createInstance(fromName: name) else {
104+
let riveFile = baseViewModel?.riveModel?.riveFile,
105+
let viewModel = riveFile.defaultViewModel(for: artboard),
106+
let instance = viewModel.createInstance(fromName: name)
107+
else {
108108
return
109109
}
110110
stateMachine?.bind(viewModelInstance: instance)
111-
artboard.bind(viewModelInstance: instance)
111+
// this should be added if we support only playing artboards on their own - https://github.com/rive-app/rive-nitro-react-native/pull/23#discussion_r2534698281
112+
// artboard.bind(viewModelInstance: instance)
112113

113114
case .instance(let instance):
114115
stateMachine?.bind(viewModelInstance: instance)
115116
artboard?.bind(viewModelInstance: instance)
116117
}
117-
if (refresh) {
118+
if refresh {
118119
baseViewModel?.play()
119120
}
120121
}
121122

122123
func play() {
123124
baseViewModel?.play()
124125
}
125-
126+
126127
func pause() {
127128
baseViewModel?.pause()
128129
}
@@ -136,74 +137,76 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
136137
func addEventListener(_ onEvent: @escaping (UnifiedRiveEvent) -> Void) {
137138
eventListeners.append(onEvent)
138139
}
139-
140+
140141
func removeEventListeners() {
141142
eventListeners.removeAll()
142143
}
143-
144+
144145
func setNumberInputValue(name: String, value: Float, path: String?) throws {
145146
try handleInput(name: name, path: path, type: .number) { (input: RiveRuntime.RiveSMINumber) in
146147
input.setValue(value)
147148
}
148149
}
149-
150+
150151
func getNumberInputValue(name: String, path: String?) throws -> Float {
151152
try handleInput(name: name, path: path, type: .number) { (input: RiveRuntime.RiveSMINumber) in
152153
input.value()
153154
}
154155
}
155-
156+
156157
func setBooleanInputValue(name: String, value: Bool, path: String?) throws {
157158
try handleInput(name: name, path: path, type: .boolean) { (input: RiveRuntime.RiveSMIBool) in
158159
input.setValue(value)
159160
}
160161
}
161-
162+
162163
func getBooleanInputValue(name: String, path: String?) throws -> Bool {
163164
try handleInput(name: name, path: path, type: .boolean) { (input: RiveRuntime.RiveSMIBool) in
164165
input.value()
165166
}
166167
}
167-
168+
168169
func triggerInput(name: String, path: String?) throws {
169170
try handleInput(name: name, path: path, type: .trigger) { (input: RiveRuntime.RiveSMITrigger) in
170171
input.fire()
171172
}
172173
}
173-
174+
174175
func setTextRunValue(name: String, value: String, path: String?) throws {
175176
let textRun = try textRunOptionPath(name: name, path: path)
176177
textRun.setText(value)
177178
}
178-
179+
179180
func getTextRunValue(name: String, path: String?) throws -> String {
180181
let textRun = try textRunOptionPath(name: name, path: path)
181182
return textRun.text()
182183
}
183-
184-
private func textRunOptionPath(name: String, path: String?) throws -> RiveRuntime.RiveTextValueRun {
184+
185+
private func textRunOptionPath(name: String, path: String?) throws -> RiveRuntime.RiveTextValueRun
186+
{
185187
let textRun: RiveRuntime.RiveTextValueRun?
186188
if let path = path {
187189
textRun = baseViewModel?.riveModel?.artboard?.textRun(name, path: path)
188190
} else {
189191
textRun = baseViewModel?.riveModel?.artboard?.textRun(name)
190192
}
191-
193+
192194
guard let textRun = textRun else {
193-
throw RuntimeError.error(withMessage: "Could not find text run `\(name)`\(path.map { " at path `\($0)`" } ?? "")")
195+
throw RuntimeError.error(
196+
withMessage: "Could not find text run `\(name)`\(path.map { " at path `\($0)`" } ?? "")")
194197
}
195-
198+
196199
return textRun
197200
}
198-
201+
199202
// MARK: - Internal
200203
deinit {
201204
cleanup()
202205
}
203-
206+
204207
private func createViewFromViewModel() {
205208
riveView = baseViewModel?.createRiveView()
206-
209+
207210
if let riveView = riveView {
208211
riveView.translatesAutoresizingMaskIntoConstraints = false
209212
addSubview(riveView)
@@ -212,11 +215,11 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
212215
riveView.leadingAnchor.constraint(equalTo: leadingAnchor),
213216
riveView.trailingAnchor.constraint(equalTo: trailingAnchor),
214217
riveView.topAnchor.constraint(equalTo: topAnchor),
215-
riveView.bottomAnchor.constraint(equalTo: bottomAnchor)
218+
riveView.bottomAnchor.constraint(equalTo: bottomAnchor),
216219
])
217220
}
218221
}
219-
222+
220223
private func cleanup() {
221224
riveView?.removeFromSuperview()
222225
riveView?.stateMachineDelegate = nil
@@ -227,27 +230,30 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
227230
self.viewSource = nil
228231
}
229232
}
230-
233+
231234
@objc func onRiveEventReceived(onRiveEvent riveEvent: RiveRuntime.RiveEvent) {
232235
let eventType = UnifiedRiveEvent(
233236
name: riveEvent.name(),
234-
type: riveEvent is RiveRuntime.RiveOpenUrlEvent ? RiveEventType.openurl : RiveEventType.general,
237+
type: riveEvent is RiveRuntime.RiveOpenUrlEvent
238+
? RiveEventType.openurl : RiveEventType.general,
235239
delay: Double(riveEvent.delay()),
236240
properties: convertEventProperties(riveEvent.properties()),
237241
url: (riveEvent as? RiveRuntime.RiveOpenUrlEvent)?.url(),
238242
target: (riveEvent as? RiveRuntime.RiveOpenUrlEvent)?.target()
239243
)
240-
244+
241245
for listener in eventListeners {
242246
listener(eventType)
243247
}
244248
}
245-
246-
private func convertEventProperties(_ properties: Dictionary<String, Any>?) -> Dictionary<String, EventPropertiesOutput>?{
249+
250+
private func convertEventProperties(_ properties: [String: Any]?) -> [String:
251+
EventPropertiesOutput]?
252+
{
247253
guard let properties = properties else { return nil }
248-
249-
var newMap: Dictionary<String, EventPropertiesOutput> = [:]
250-
254+
255+
var newMap: [String: EventPropertiesOutput] = [:]
256+
251257
for (key, value) in properties {
252258
if let string = value as? String {
253259
newMap[key] = .string(string)
@@ -257,17 +263,19 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
257263
newMap[key] = .boolean(boolean)
258264
}
259265
}
260-
266+
261267
return newMap
262268
}
263-
269+
264270
private enum InputType {
265271
case number
266272
case boolean
267273
case trigger
268274
}
269-
270-
private func handleInput<P: RiveRuntime.RiveSMIInput, T>(name: String, path: String?, type: InputType, onSuccess: (P) throws -> T) throws -> T {
275+
276+
private func handleInput<P: RiveRuntime.RiveSMIInput, T>(
277+
name: String, path: String?, type: InputType, onSuccess: (P) throws -> T
278+
) throws -> T {
271279
let input: P?
272280
if let path = path {
273281
switch type {
@@ -288,11 +296,12 @@ class RiveReactNativeView: UIView, RiveStateMachineDelegate {
288296
input = baseViewModel?.riveModel?.stateMachine?.getTrigger(name) as? P
289297
}
290298
}
291-
299+
292300
guard let input = input else {
293-
throw RuntimeError.error(withMessage: "Could not find input `\(name)`\(path.map { " at path `\($0)`" } ?? "")")
301+
throw RuntimeError.error(
302+
withMessage: "Could not find input `\(name)`\(path.map { " at path `\($0)`" } ?? "")")
294303
}
295-
304+
296305
return try onSuccess(input)
297306
}
298307
}

0 commit comments

Comments
 (0)