Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.margelo.nitro.core.Promise
import com.margelo.nitro.NitroModules
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File as JavaFile
import java.net.URI
import java.net.URL

@Keep
Expand All @@ -33,6 +35,31 @@ class HybridRiveFileFactory : HybridRiveFileFactorySpec() {
}
}

override fun fromFileURL(fileURL: String, loadCdn: Boolean): Promise<HybridRiveFileSpec> {
if (!fileURL.startsWith("file://")) {
throw Error("fromFileURL: URL must be a file URL: $fileURL")
}

return Promise.async {
try {
val uri = URI(fileURL)
val path = uri.path ?: throw Error("fromFileURL: Invalid URL: $fileURL")

val riveFile = withContext(Dispatchers.IO) {
val file = JavaFile(path)
val riveData = file.readBytes()
File(riveData)
}

val hybridRiveFile = HybridRiveFile()
hybridRiveFile.riveFile = riveFile
hybridRiveFile
} catch (e: Exception) {
throw Error("Failed to load Rive file: ${e.message}")
}
}
}

@SuppressLint("DiscouragedApi")
override fun fromResource(resource: String, loadCdn: Boolean): Promise<HybridRiveFileSpec> {
return Promise.async {
Expand Down
39 changes: 39 additions & 0 deletions ios/HybridRiveFileFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,45 @@ class HybridRiveFileFactory: HybridRiveFileFactorySpec {
}
}

func fromFileURL(fileURL: String, loadCdn: Bool) throws -> Promise<(any HybridRiveFileSpec)> {
guard let url = URL(string:fileURL) else {
throw RuntimeError.error(withMessage: "fromFileURL: Invalid URL: \(fileURL)")
}

guard url.isFileURL else {
throw RuntimeError.error(withMessage: "fromFileURL: URL must be a file URL: \(fileURL)")
}

return Promise.async {
do {
let riveFile = try await withCheckedThrowingContinuation { continuation in
DispatchQueue.global(qos: .userInitiated).async {
do {
let data = try Data(contentsOf: url)

let riveFile = try RiveFile(data: data, loadCdn: loadCdn)
DispatchQueue.main.async {
continuation.resume(returning: riveFile)
}
} catch {
DispatchQueue.main.async {
continuation.resume(throwing: error)
}
}
}
}

let hybridRiveFile = HybridRiveFile()
hybridRiveFile.riveFile = riveFile
return hybridRiveFile
} catch let error as NSError {
throw RuntimeError.error(withMessage: "Failed to load Rive file: \(error.localizedDescription)")
} catch {
throw RuntimeError.error(withMessage: "Unknown error occurred while loading Rive file")
}
}
}

func fromResource(resource: String, loadCdn: Bool) throws -> Promise<(any HybridRiveFileSpec)> {
guard let _ = Bundle.main.path(forResource: resource, ofType: "riv") else {
throw RuntimeError.error(withMessage: "Could not find Rive file: \(resource).riv")
Expand Down
16 changes: 16 additions & 0 deletions nitrogen/generated/android/c++/JHybridRiveFileFactorySpec.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions nitrogen/generated/ios/c++/HybridRiveFileFactorySpecSwift.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions nitrogen/generated/ios/swift/HybridRiveFileFactorySpec_cxx.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/core/RiveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ export namespace RiveFileFactory {
return RiveFileInternal.fromURL(url, loadCdn);
}

/**
* Creates a RiveFile instance from a local file path URL.
* @param pathURL - The local file path of the Rive graphic file
* @param loadCdn - Whether to load from CDN (default: true)
Comment thread
mfazekas marked this conversation as resolved.
* @returns Promise that resolves to a RiveFile instance
*/
export async function fromFileURL(
fileURL: string,
loadCdn: boolean = true
): Promise<RiveFile> {
return RiveFileInternal.fromFileURL(fileURL, loadCdn);
}

/**
* Creates a RiveFile instance from a local resource.
* @param resource - The name of the local resource
Expand Down Expand Up @@ -100,11 +113,7 @@ export namespace RiveFileFactory {

// handle iOS bundled asset
if (assetURI.match(/file:\/\//)) {
const match = assetURI.match(/file:\/\/(.*\/)+(.*)\.riv/);
if (!match) {
throw new Error(`Invalid iOS asset path format: ${assetURI}`);
}
return RiveFileFactory.fromResource(match[2], loadCdn);
return RiveFileFactory.fromFileURL(assetURI, loadCdn);
}

// handle Android bundled asset or resource name uri
Expand Down
1 change: 1 addition & 0 deletions src/specs/RiveFile.nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface RiveFile
export interface RiveFileFactory
extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
fromURL(url: string, loadCdn: boolean): Promise<RiveFile>;
fromFileURL(fileURL: string, loadCdn: boolean): Promise<RiveFile>;
Comment thread
mfazekas marked this conversation as resolved.
fromResource(resource: string, loadCdn: boolean): Promise<RiveFile>;
fromBytes(bytes: ArrayBuffer, loadCdn: boolean): Promise<RiveFile>;
}
Loading