Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix nil safety in RiveReactNativeView.swift #206

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: UIView, 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: UIView, 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: UIView, 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: UIView, 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