Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom Bundles in RiveFile convenience init #259

Closed
ilyakuteev opened this issue May 28, 2023 · 2 comments
Closed

Support custom Bundles in RiveFile convenience init #259

ilyakuteev opened this issue May 28, 2023 · 2 comments

Comments

@ilyakuteev
Copy link

Problem

By now RiveFile+Extensions.swift does not support user-defined modules. This results in boilerplate code when using Rive animations from SPM projects with a single bundle for each target/module.

Consider the following example:
Repo layout:

Sources/ModuleA/Resources/Animation.riv
Sources/ModuleA/A.swift
Sources/ModuleA/Resources/Animation.riv
Sources/ModuleA/B.swift

Code inside A.swift:

struct AView: View {
  var animation = { () -> RiveViewModel? in
    guard
      let url = Bundle.module.url(forResource: "Animation", withExtension: "riv"),
      var data = try? Data(contentsOf: url),
      let file = try? data.withUnsafeMutableBytes({ [count=data.count] (bytes) -> RiveFile? in
        guard let baseAddress = bytes.baseAddress else { return nil }
        return try RiveFile(
          bytes: baseAddress.bindMemory(to: UInt8.self, capacity: count),
          byteLength: UInt64(count)
        )
      })
    else { return nil }
    let model = RiveModel(riveFile: file)
    return RiveViewModel(model)
  }()
}

Proposal

Add bundle parameter to RiveFile.init(name:extension:), like this:

convenience init(name fileName: String, extension ext: String = ".riv", bundle: Bundle = .main) throws {
@jaanus
Copy link

jaanus commented May 29, 2023

I agree that it’s currently suboptimal for multiple bundles, something better is needed. I use a similar workaround for data.

For safer and shorter code when initializing from data, I just cast the data.

let data = Data(whatever)
let riveFile = RiveFile(byteArray: [UInt8](data))

@ilyakuteev
Copy link
Author

Was implemented here: #250

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants