Skip to content

Commit

Permalink
Add support for injecting Bundle when creating RiveFile
Browse files Browse the repository at this point in the history
[Downstream community contribution](#250) to support pulling Rive files from bundles other than `.main`

Diffs=
46a0745e6 Add support for injecting Bundle when creating RiveFile (#5335)

Co-authored-by: Robert Dresler <robertdreslerjr@gmail.com>
Co-authored-by: Zachary Plata <plata.zach@gmail.com>
  • Loading branch information
3 people committed May 31, 2023
1 parent c6b0576 commit 1b78705
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
064e7688a88e5160bc999f4387de212777da7283
46a0745e62a9a7b2782e3a7fb60fbec2497e72ed
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, extension: String = ".riv", in bundle: Bundle = .main) throws {
riveFile = try RiveFile(name: fileName, extension: `extension`, in: bundle)
}

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

public init(
fileName: String,
extension: String = ".riv",
in bundle: Bundle = .main,
stateMachineName: String?,
fit: RiveFit = .contain,
alignment: RiveAlignment = .center,
Expand All @@ -87,12 +89,14 @@ 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, extension: `extension`, in: bundle)
sharedInit(artboardName: artboardName, stateMachineName: stateMachineName, animationName: nil)
}

public init(
fileName: String,
extension: String = ".riv",
in bundle: Bundle = .main,
animationName: String? = nil,
fit: RiveFit = .contain,
alignment: RiveAlignment = .center,
Expand All @@ -103,7 +107,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, extension: `extension`, 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,14 +9,14 @@
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", in bundle: Bundle = .main) throws {
let byteArray = RiveFile.getBytes(fileName: fileName, extension: ext, in: 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 {
fatalError("Failed to locate \(fileName) in bundle.")
static func getBytes(fileName: String, extension ext: String = ".riv", in bundle: Bundle = .main) -> [UInt8] {
guard let url = bundle.url(forResource: fileName, withExtension: ext) else {
fatalError("Failed to locate \(fileName) in bundle \(bundle).")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
Expand Down

0 comments on commit 1b78705

Please sign in to comment.