Skip to content

Commit

Permalink
fix nil safety in RiveReactNativeView.swift
Browse files Browse the repository at this point in the history
I was able to consistently crash my React Native app when Hot Module
Reloading was triggered. Removing the implicit optional unwrapping and
making that explicit within RiveReactNativeView fixed the crashes I was
running into.
  • Loading branch information
ckknight authored and HayesGordon committed Jan 2, 2024
1 parent 227d48b commit a1e1483
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions ios/RiveReactNativeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
@objc var isUserHandlingErrors: Bool

// MARK: RiveRuntime Bindings
var riveView: RiveView!
var viewModel: RiveViewModel!
var riveView: RiveView?
var viewModel: RiveViewModel?

@objc var resourceName: String? = nil {
didSet {
Expand Down Expand Up @@ -123,8 +123,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
removeReactSubview(riveView)

viewModel = updatedViewModel
riveView = viewModel.createRiveView();
addSubview(riveView)
riveView = viewModel!.createRiveView();
addSubview(riveView!)
riveView?.playerDelegate = self
riveView?.stateMachineDelegate = self
}
Expand Down Expand Up @@ -192,37 +192,37 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
let loop = RNLoopMode.mapToRiveLoop(rnLoopMode: rnLoopMode)
let direction = RNDirection.mapToRiveDirection(rnDirection: rnDirection)
if (animationName ?? "").isEmpty || isStateMachine {
viewModel.play(loop: loop, direction: direction)
viewModel?.play(loop: loop, direction: direction)
} else {
viewModel.play(animationName: animationName, loop: loop, direction: direction)
viewModel?.play(animationName: animationName, loop: loop, direction: direction)
}
}

func pause() {
viewModel.pause()
viewModel?.pause()
}

func stop() {
viewModel.stop()
viewModel?.stop()
}

func reset() {
viewModel.reset()
viewModel?.reset()
reloadView()
}

// MARK: - StateMachine Inputs

func fireState(stateMachineName: String, inputName: String) {
viewModel.triggerInput(inputName)
viewModel?.triggerInput(inputName)
}

func setNumberState(stateMachineName: String, inputName: String, value: Float) {
viewModel.setInput(inputName, value: value)
viewModel?.setInput(inputName, value: value)
}

func setBooleanState(stateMachineName: String, inputName: String, value: Bool) {
viewModel.setInput(inputName, value: value)
viewModel?.setInput(inputName, value: value)
}

// MARK: - Text Runs
Expand Down Expand Up @@ -332,8 +332,8 @@ class RiveReactNativeView: RCTView, RivePlayerDelegate, RiveStateMachineDelegate
}

private func handleTouch(location: CGPoint, action: (RiveStateMachineInstance, CGPoint)->Void) {
if (viewModel.riveView != nil) {
let artboardLocation = viewModel.riveView!.artboardLocation(
if let viewModel = viewModel, let riveView = viewModel.riveView {
let artboardLocation = riveView.artboardLocation(
fromTouchLocation: location,
inArtboard: viewModel.riveModel!.artboard!.bounds(),
fit: viewModel.fit,
Expand Down

0 comments on commit a1e1483

Please sign in to comment.