Skip to content

Commit

Permalink
Gave RiveViewModel specialized inits so that users are presented with…
Browse files Browse the repository at this point in the history
…out only an animation or a state machine param
  • Loading branch information
duncandoit committed Jun 8, 2022
1 parent a4387fc commit 79f3119
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class MultipleAnimationsController: UIViewController {
@IBOutlet weak var rviewStar: RiveView!

var rSquareGoAround = RiveViewModel(
fileName: "artboard_animations", artboardName: "Square", animationName: "goaround"
fileName: "artboard_animations", animationName: "goaround", artboardName: "Square"
)
var rSquareRollAround = RiveViewModel(
fileName: "artboard_animations", artboardName: "Square", animationName: "rollaround"
fileName: "artboard_animations", animationName: "rollaround", artboardName: "Square"
)
var rCircle = RiveViewModel(
fileName: "artboard_animations", artboardName: "Circle"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ struct SwiftMultipleAnimations: DismissableView {
ScrollView{
VStack {
Text("Square - go around")
RiveViewModel(model(), artboardName: "Square", animationName: "goaround").view()
RiveViewModel(model(), animationName: "goaround", artboardName: "Square").view()
.aspectRatio(1, contentMode: .fit)

Text("Square - roll around")
RiveViewModel(model(), artboardName: "Square", animationName: "rollaround").view()
RiveViewModel(model(), animationName: "rollaround", artboardName: "Square").view()
.aspectRatio(1, contentMode: .fit)

Text("Circle")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct SwiftTestParityAnimSM: DismissableView {
SwiftVMPlayer(
viewModels:
RiveViewModel(fileName: "teststatemachine", stateMachineName: "State Machine 1", autoPlay: false),
RiveViewModel(fileName: "testanimation", autoPlay: false, animationName: "Move")
RiveViewModel(fileName: "testanimation", animationName: "Move", autoPlay: false)
)
}
}
2 changes: 1 addition & 1 deletion Example-iOS/Source/Examples/ViewModel/RiveSwitch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RiveSwitch: RiveViewModel {
}

init() {
super.init(fileName: "switch", fit: .fitCover, animationName: startAnimation)
super.init(fileName: "switch", animationName: startAnimation, fit: .fitCover)
}

func view(_ action: ((Bool) -> Void)? = nil) -> some View {
Expand Down
77 changes: 58 additions & 19 deletions Source/Components/RiveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,59 +44,98 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat

public init(
_ model: RiveModel,
stateMachineName: String? = nil,
stateMachineName: String?,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil,
animationName: String? = nil
artboardName: String? = nil
) {
self.riveModel = model
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay

super.init()

sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: animationName)
riveModel = model
sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: nil)
}

public init(
_ model: RiveModel,
animationName: String? = nil,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil
) {
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay
super.init()
riveModel = model
sharedInit(artboardName: artboardName, stateMachineName: nil, animationName: animationName)
}

public init(
fileName: String,
stateMachineName: String?,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil
) {
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay
super.init()
riveModel = try! RiveModel(fileName: fileName)
sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: nil)
}

public init(
fileName: String,
stateMachineName: String? = nil,
animationName: String? = nil,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil,
animationName: String? = nil
artboardName: String? = nil
) {
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay
super.init()
riveModel = try! RiveModel(fileName: fileName)
sharedInit(artboardName: artboardName, stateMachineName: nil, animationName: animationName)
}

public init(
webURL: String,
stateMachineName: String?,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil
) {
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay

super.init()

sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: animationName)
riveModel = RiveModel(webURL: webURL, delegate: self)
defaultModel = RiveModelBuffer(artboardName: artboardName, stateMachineName: stateMachineName, animationName: nil)
}

public init(
webURL: String,
stateMachineName: String? = nil,
animationName: String? = nil,
fit: RiveRuntime.Fit = .fitContain,
alignment: RiveRuntime.Alignment = .alignmentCenter,
autoPlay: Bool = true,
artboardName: String? = nil,
animationName: String? = nil
artboardName: String? = nil
) {
self.fit = fit
self.alignment = alignment
self.autoPlay = autoPlay

super.init()

riveModel = RiveModel(webURL: webURL, delegate: self)
defaultModel = RiveModelBuffer(artboardName: artboardName, stateMachineName: stateMachineName, animationName: animationName)
defaultModel = RiveModelBuffer(artboardName: artboardName, stateMachineName: nil, animationName: animationName)
}

private func sharedInit(artboardName: String?, stateMachineName: String?, animationName: String?) {
Expand Down
2 changes: 1 addition & 1 deletion Tests/RiveDelegatesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class DelegatesTest: XCTestCase {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false, animationName: "one")
let viewModel = RiveViewModel(model, animationName: "one", autoPlay: false)
let view = viewModel.createRiveView()

view.playerDelegate = delegate
Expand Down

0 comments on commit 79f3119

Please sign in to comment.