Skip to content
This repository has been archived by the owner on Jun 2, 2023. It is now read-only.

Commit

Permalink
add support to configure bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
davidscheutz committed Apr 26, 2023
1 parent 8fa20fc commit 82bde93
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Source/RiveModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ open class RiveModel: ObservableObject {
self.riveFile = riveFile
}

public init(fileName: String) throws {
riveFile = try RiveFile(name: fileName)
public init(fileName: String, in bundle: Bundle) throws {
riveFile = try RiveFile(name: fileName, bundle: bundle)
}

public init(webURL: String, delegate: RiveFileDelegate) {
Expand Down
6 changes: 4 additions & 2 deletions Source/RiveViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat

public init(
fileName: String,
bundle: Bundle = .main,
stateMachineName: String?,
fit: RiveFit = .contain,
alignment: RiveAlignment = .center,
Expand All @@ -87,12 +88,13 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat
self.alignment = alignment
self.autoPlay = autoPlay
super.init()
riveModel = try! RiveModel(fileName: fileName)
riveModel = try! RiveModel(fileName: fileName, in: bundle)
sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: nil)
}

public init(
fileName: String,
bundle: Bundle = .main,
animationName: String? = nil,
fit: RiveFit = .contain,
alignment: RiveAlignment = .center,
Expand All @@ -103,7 +105,7 @@ open class RiveViewModel: NSObject, ObservableObject, RiveFileDelegate, RiveStat
self.alignment = alignment
self.autoPlay = autoPlay
super.init()
riveModel = try! RiveModel(fileName: fileName)
riveModel = try! RiveModel(fileName: fileName, in: bundle)
sharedInit(artboardName: artboardName, stateMachineName: nil, animationName: animationName)
}

Expand Down
10 changes: 5 additions & 5 deletions Source/Utils/RiveFile+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import Foundation

public extension RiveFile {
convenience init(name fileName: String, extension ext: String = ".riv") throws {
let byteArray = RiveFile.getBytes(fileName: fileName, extension: ext)
convenience init(name fileName: String, extension ext: String = ".riv", bundle: Bundle = .main) throws {
let byteArray = RiveFile.getBytes(fileName: fileName, extension: ext, bundle: bundle)
try self.init(byteArray: byteArray)
}

static func getBytes(fileName: String, extension ext: String = ".riv") -> [UInt8] {
guard let url = Bundle.main.url(forResource: fileName, withExtension: ext) else {
static func getBytes(fileName: String, extension ext: String = ".riv", bundle: Bundle) -> [UInt8] {
guard let url = bundle.url(forResource: fileName, withExtension: ext) else {
fatalError("Failed to locate \(fileName) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
fatalError("Failed to load \(url) from bundle \(bundle).")
}

// Import the data into a RiveFile
Expand Down

0 comments on commit 82bde93

Please sign in to comment.