From f4b010f8b5ac11219c7feecf0667c2b51fc242e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Krzy=C5=BCowski?= Date: Mon, 24 May 2021 14:37:47 +0200 Subject: [PATCH] feat: added robust play method (#41) --- ios/RNDirection.swift | 24 ++++++++++++++++++++++++ ios/RNFit.swift | 2 +- ios/RNLoopMode.swift | 27 +++++++++++++++++++++++++++ ios/RiveReactNativeView.swift | 11 +++++++++-- ios/RiveReactNativeViewManager.swift | 3 ++- 5 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 ios/RNDirection.swift create mode 100644 ios/RNLoopMode.swift diff --git a/ios/RNDirection.swift b/ios/RNDirection.swift new file mode 100644 index 0000000..9f1b6e8 --- /dev/null +++ b/ios/RNDirection.swift @@ -0,0 +1,24 @@ +enum RNDirection: String { + case Backwards = "backwards" + case Auto = "auto" + case Forwards = "forwards" + + static func mapToRNDirection(value: String) -> RNDirection { + if let rnEnum = RNDirection(rawValue: value) { + return rnEnum + } else { + fatalError("Unsupported direction type: \(value)") + } + } + + static func mapToRiveDirection(rnDirection: RNDirection) -> Direction { + switch rnDirection { + case .Backwards: + return .directionBackwards + case .Auto: + return .directionAuto + case .Forwards: + return .directionForwards + } + } +} diff --git a/ios/RNFit.swift b/ios/RNFit.swift index 1c416bd..9cd82e2 100644 --- a/ios/RNFit.swift +++ b/ios/RNFit.swift @@ -18,7 +18,7 @@ enum RNFit: String { } } - static func mapToRiveFit(rnFit: RNFit) -> Fit{ + static func mapToRiveFit(rnFit: RNFit) -> Fit { switch rnFit { case .Contain: return Fit.fitContain diff --git a/ios/RNLoopMode.swift b/ios/RNLoopMode.swift new file mode 100644 index 0000000..0c82e0c --- /dev/null +++ b/ios/RNLoopMode.swift @@ -0,0 +1,27 @@ +enum RNLoopMode: String { + case OneShot = "oneShot" + case Loop = "loop" + case PingPong = "pingPong" + case Auto = "none" + + static func mapToRNLoopMode(value: String) -> RNLoopMode { + if let rnEnum = RNLoopMode(rawValue: value) { + return rnEnum + } else { + fatalError("Unsupported loop mode type: \(value)") + } + } + + static func mapToRiveLoop(rnLoopMode: RNLoopMode) -> Loop{ + switch rnLoopMode { + case .OneShot: + return .loopOneShot + case .Loop: + return .loopLoop + case .PingPong: + return .loopPingPong + case .Auto: + return .loopAuto + } + } +} diff --git a/ios/RiveReactNativeView.swift b/ios/RiveReactNativeView.swift index 44525a5..b2406b6 100644 --- a/ios/RiveReactNativeView.swift +++ b/ios/RiveReactNativeView.swift @@ -167,8 +167,15 @@ class RiveReactNativeView: UIView, PlayDelegate, PauseDelegate, StopDelegate, Lo } } - @objc func play() { - riveView.play() + func play(animationNames: [String], rnLoopMode: RNLoopMode, rnDirection: RNDirection, areStateMachines: Bool) { + let loop = RNLoopMode.mapToRiveLoop(rnLoopMode: rnLoopMode) + let direction = RNDirection.mapToRiveDirection(rnDirection: rnDirection) + if animationNames.isEmpty { + riveView.play(loop: loop, direction: direction) + } else { + riveView.play(animationName: animationNames[0], loop: loop, direction: direction, isStateMachine: areStateMachines) + } + } @objc func pause() { diff --git a/ios/RiveReactNativeViewManager.swift b/ios/RiveReactNativeViewManager.swift index a63a8a7..b051ea6 100644 --- a/ios/RiveReactNativeViewManager.swift +++ b/ios/RiveReactNativeViewManager.swift @@ -12,7 +12,8 @@ class RiveReactNativeViewManager: RCTViewManager { @objc func play(_ node: NSNumber, animationNames: [String], loopMode: String, direction: String, areStateMachines: Bool) { DispatchQueue.main.async { let component = self.bridge.uiManager.view(forReactTag: node) as! RiveReactNativeView - component.play() + component.play(animationNames: animationNames, rnLoopMode: RNLoopMode.mapToRNLoopMode(value: loopMode), rnDirection: RNDirection.mapToRNDirection(value: direction), areStateMachines: areStateMachines) + } }