Skip to content

Commit

Permalink
iOS - Fixed setArtboard bug in RiveModel
Browse files Browse the repository at this point in the history
Fixed bug in RiveModel where using setArtboard(...) was not properly releasing references to old StateMachine and Animation objects.
Updated RiveViewModel's configureModel(...) method to have open access to make ad hoc configuration easier.

Diffs=
ce1c62889 Fixed bug in RiveModel where using setArtboard(...) was not properly releasing references to old StateMachine and Animation objects. Updated RiveViewModel's configureModel(...) method to have open access to make ad hoc configuration easier.
  • Loading branch information
duncandoit committed Aug 24, 2022
1 parent fffe9ea commit bb229e0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
c1a6b2946685948fefae37ab17d74fcaea6d0d3f
ce1c62889d15349c96c1eca311f2e2ee9bb4c14d
14 changes: 12 additions & 2 deletions Source/Components/RiveModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ open class RiveModel: ObservableObject {

// MARK: - Setters

/// Sets a new Artboard and makes the current StateMachine and Animation nil
open func setArtboard(_ name: String) throws {
do { artboard = try riveFile.artboard(fromName: name) }
do {
stateMachine = nil
animation = nil
artboard = try riveFile.artboard(fromName: name)
}
catch { throw RiveModelError.invalidArtboard("Name \(name) not found") }
}

/// Sets a new Artboard and makes the current StateMachine and Animation nil
open func setArtboard(_ index: Int? = nil) throws {
if let index = index {
do { artboard = try riveFile.artboard(from: index) }
do {
stateMachine = nil
animation = nil
artboard = try riveFile.artboard(from: index)
}
catch { throw RiveModelError.invalidArtboard("Index \(index) not found") }
} else {
// This tries to find the 'default' Artboard
Expand Down
8 changes: 4 additions & 4 deletions Source/Components/RiveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,12 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat
// MARK: - RiveModel

/// Instantiates elements in the model needed to play in a `RiveView`
private func configureModel(artboardName: String? = nil, stateMachineName: String? = nil, animationName: String? = nil) throws {
open func configureModel(artboardName: String? = nil, stateMachineName: String? = nil, animationName: String? = nil) throws {
guard let model = riveModel else { fatalError("Cannot configure nil RiveModel") }

model.animation = nil
model.stateMachine = nil

if let name = artboardName {
try model.setArtboard(name)
} else {
Expand All @@ -238,9 +241,6 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat
}
}

model.animation = nil
model.stateMachine = nil

if let name = stateMachineName {
try model.setStateMachine(name)
}
Expand Down

0 comments on commit bb229e0

Please sign in to comment.