Skip to content

Commit

Permalink
feat: added robust play method (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
TMaszko authored May 24, 2021
1 parent f87f92b commit f4b010f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
24 changes: 24 additions & 0 deletions ios/RNDirection.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion ios/RNFit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions ios/RNLoopMode.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
11 changes: 9 additions & 2 deletions ios/RiveReactNativeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion ios/RiveReactNativeViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}

Expand Down

0 comments on commit f4b010f

Please sign in to comment.