Skip to content

Commit

Permalink
Merge pull request #12 from LottieFiles/feat/multi-animation
Browse files Browse the repository at this point in the history
Feat/multi animation
  • Loading branch information
samuelOsborne authored Feb 7, 2024
2 parents 2034e7c + c7a0918 commit 9f9ae83
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class AnimationViewController: UIViewController {
| `isPaused()` | Bool | Reflects whether the animation is paused or not. |
| `isStopped()` | Bool | Reflects whether the animation is stopped or not. |
| `isPlaying()` | Bool | Reflects whether the animation is playing or not. |
| `manifest()` | Manifst | Returns the .lottie's manifest file. |
| `segments()` | (Float, Float) | Reflects the frames range of the animations. where segments\[0] is the start frame and segments\[1] is the end frame. |
| `backgroundColor()` | CIImage | Gets the background color of the canvas. |
| `autoplay()` | Bool | Indicates if the animation is set to auto play. |
Expand All @@ -122,6 +123,7 @@ class AnimationViewController: UIViewController {
| `setLoop(loop: Bool)` | Configures whether the animation should loop continuously. |
| `setFrame(frame: Float)` | Directly navigates the animation to a specified frame. |
| `load(config: Config)` | Loads a new configuration or a new animation. |
| `loadAnimation(animationId: String)` | Loads the animation by id. Animation id's are visible inside the manifest, recoverable via the manifest() method. |
| `setMode(mode: Mode)` | Sets the animation play mode. |
| `setSegments(segments: (Float, Float))` | Sets the start and end frame of the animation. |
| `setBackgroundColor(color: CIImage)` | Sets the background color of the animation. |
Expand All @@ -143,6 +145,9 @@ class YourDotLottieObserver: Observer {
func onLoad() {
}

func onLoadError() {
}

func onLoop(loopCount: UInt32) {
}

Expand Down Expand Up @@ -175,6 +180,7 @@ animationView.subscribe(observer: myObserver)
| `onComplete` | Emitted when the animation completes. |
| `onFrame(frameNo: Float)` | Emitted when the animation reaches a new frame. |
| `onLoad` | Emitted when the animation is loaded. |
| `onLoadError` | Emitted when the animation failed to load. |
| `onLoop(loopCount: UIint32)` | Emitted when the animation completes a loop. |
| `onPause` | Emitted when the animation is paused. |
| `onPlay` | Emitted when the animation starts playing. |
Expand Down
17 changes: 17 additions & 0 deletions Sources/DotLottie/Public/DotLottieAnimation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,19 @@ public class DotLottieAnimation: ObservableObject {
}
}

/// Loads animation with the id passed as argument.
/// - Parameter animationData: Animation data (.json).
public func loadAnimationById(_ animationId: String) throws {
do {
try player.loadAnimation(animationId: animationId, width: self.animationModel.width, height: self.animationModel.height)
} catch let error {
animationModel.error = true
animationModel.errorMessage = error.localizedDescription

throw error
}
}

private func initWidthHeight(animationData: String?, animationFilePath: URL?) {
// Parse width and height of animation
do {
Expand Down Expand Up @@ -389,6 +402,10 @@ public class DotLottieAnimation: ObservableObject {
return player.config().mode
}

public func manifest() -> Manifest? {
return player.manifest()
}

public func resize(width: Int, height: Int) {
self.animationModel.width = width
self.animationModel.height = height
Expand Down
16 changes: 16 additions & 0 deletions Sources/DotLottie/Public/Player.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ class Player: ObservableObject, Observer {
}
}

public func loadAnimation(animationId: String, width: Int, height: Int) throws {
self.WIDTH = UInt32(width)
self.HEIGHT = UInt32(height)

if (!dotLottiePlayer.loadAnimation(animationId: animationId,
width: self.WIDTH,
height: self.HEIGHT)) {
self.setPlayerState(state: .error)
throw AnimationLoadErrors.loadFromPathError
}
}

public func render() -> CGImage? {
if (!self.isLoaded() || !dotLottiePlayer.render()) {
return nil
Expand Down Expand Up @@ -213,4 +225,8 @@ class Player: ObservableObject, Observer {
func onStop() {
self.setPlayerState(state: .stopped)
}

func onLoadError() {
self.setPlayerState(state: .error)
}
}
55 changes: 51 additions & 4 deletions Sources/DotLottie/Public/dotlottie_player.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ public protocol DotLottiePlayerProtocol: AnyObject {

func resize(width: UInt32, height: UInt32) -> Bool

func seek(no: Float) -> Bool

func setConfig(config: Config)

func setFrame(no: Float) -> Bool
Expand Down Expand Up @@ -731,6 +733,16 @@ public class DotLottiePlayer:
)
}

public func seek(no: Float) -> Bool {
return try! FfiConverterBool.lift(
try!
rustCall {
uniffi_dotlottie_player_fn_method_dotlottieplayer_seek(self.uniffiClonePointer(),
FfiConverterFloat.lower(no), $0)
}
)
}

public func setConfig(config: Config) {
try!
rustCall {
Expand Down Expand Up @@ -829,6 +841,8 @@ public protocol Observer: AnyObject {

func onLoad()

func onLoadError()

func onLoop(loopCount: UInt32)

func onPause()
Expand Down Expand Up @@ -882,6 +896,13 @@ public class ObserverImpl:
}
}

public func onLoadError() {
try!
rustCall {
uniffi_dotlottie_player_fn_method_observer_on_load_error(self.uniffiClonePointer(), $0)
}
}

public func onLoop(loopCount: UInt32) {
try!
rustCall {
Expand Down Expand Up @@ -1018,6 +1039,15 @@ private let uniffiCallbackInterfaceObserver: ForeignCallback = { (handle: UniFFI
return try makeCall()
}

func invokeOnLoadError(_ swiftCallbackInterface: Observer, _: UnsafePointer<UInt8>, _: Int32, _: UnsafeMutablePointer<RustBuffer>) throws -> Int32 {
func makeCall() throws -> Int32 {
swiftCallbackInterface.onLoadError(
)
return UNIFFI_CALLBACK_SUCCESS
}
return try makeCall()
}

func invokeOnLoop(_ swiftCallbackInterface: Observer, _ argsData: UnsafePointer<UInt8>, _ argsLen: Int32, _: UnsafeMutablePointer<RustBuffer>) throws -> Int32 {
var reader = createReader(data: Data(bytes: argsData, count: Int(argsLen)))
func makeCall() throws -> Int32 {
Expand Down Expand Up @@ -1112,7 +1142,7 @@ private let uniffiCallbackInterfaceObserver: ForeignCallback = { (handle: UniFFI
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
do {
return try invokeOnLoop(cb, argsData, argsLen, out_buf)
return try invokeOnLoadError(cb, argsData, argsLen, out_buf)
} catch {
out_buf.pointee = FfiConverterString.lower(String(describing: error))
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
Expand All @@ -1123,7 +1153,7 @@ private let uniffiCallbackInterfaceObserver: ForeignCallback = { (handle: UniFFI
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
do {
return try invokeOnPause(cb, argsData, argsLen, out_buf)
return try invokeOnLoop(cb, argsData, argsLen, out_buf)
} catch {
out_buf.pointee = FfiConverterString.lower(String(describing: error))
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
Expand All @@ -1134,7 +1164,7 @@ private let uniffiCallbackInterfaceObserver: ForeignCallback = { (handle: UniFFI
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
do {
return try invokeOnPlay(cb, argsData, argsLen, out_buf)
return try invokeOnPause(cb, argsData, argsLen, out_buf)
} catch {
out_buf.pointee = FfiConverterString.lower(String(describing: error))
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
Expand All @@ -1145,12 +1175,23 @@ private let uniffiCallbackInterfaceObserver: ForeignCallback = { (handle: UniFFI
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
do {
return try invokeOnRender(cb, argsData, argsLen, out_buf)
return try invokeOnPlay(cb, argsData, argsLen, out_buf)
} catch {
out_buf.pointee = FfiConverterString.lower(String(describing: error))
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
case 8:
guard let cb = FfiConverterTypeObserver.handleMap.get(handle: handle) else {
out_buf.pointee = FfiConverterString.lower("No callback in handlemap; this is a Uniffi bug")
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
do {
return try invokeOnRender(cb, argsData, argsLen, out_buf)
} catch {
out_buf.pointee = FfiConverterString.lower(String(describing: error))
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
}
case 9:
guard let cb = FfiConverterTypeObserver.handleMap.get(handle: handle) else {
out_buf.pointee = FfiConverterString.lower("No callback in handlemap; this is a Uniffi bug")
return UNIFFI_CALLBACK_UNEXPECTED_ERROR
Expand Down Expand Up @@ -2078,6 +2119,9 @@ private var initializationResult: InitializationResult {
if uniffi_dotlottie_player_checksum_method_dotlottieplayer_resize() != 16787 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_dotlottie_player_checksum_method_dotlottieplayer_seek() != 60656 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_dotlottie_player_checksum_method_dotlottieplayer_set_config() != 39472 {
return InitializationResult.apiChecksumMismatch
}
Expand Down Expand Up @@ -2105,6 +2149,9 @@ private var initializationResult: InitializationResult {
if uniffi_dotlottie_player_checksum_method_observer_on_load() != 56735 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_dotlottie_player_checksum_method_observer_on_load_error() != 51239 {
return InitializationResult.apiChecksumMismatch
}
if uniffi_dotlottie_player_checksum_method_observer_on_loop() != 7035 {
return InitializationResult.apiChecksumMismatch
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ float uniffi_dotlottie_player_fn_method_dotlottieplayer_request_frame(void*_Nonn
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_resize(void*_Nonnull ptr, uint32_t width, uint32_t height, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_seek(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_dotlottieplayer_set_config(void*_Nonnull ptr, RustBuffer config, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_set_frame(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
Expand All @@ -127,6 +129,8 @@ void uniffi_dotlottie_player_fn_method_observer_on_frame(void*_Nonnull ptr, floa
);
void uniffi_dotlottie_player_fn_method_observer_on_load(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_load_error(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_loop(void*_Nonnull ptr, uint32_t loop_count, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_pause(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -317,6 +321,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_request_frame(v
);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_resize(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_seek(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_set_config(void

Expand Down Expand Up @@ -344,6 +351,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_observer_on_frame(void
);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load_error(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_loop(void

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ float uniffi_dotlottie_player_fn_method_dotlottieplayer_request_frame(void*_Nonn
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_resize(void*_Nonnull ptr, uint32_t width, uint32_t height, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_seek(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_dotlottieplayer_set_config(void*_Nonnull ptr, RustBuffer config, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_set_frame(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
Expand All @@ -127,6 +129,8 @@ void uniffi_dotlottie_player_fn_method_observer_on_frame(void*_Nonnull ptr, floa
);
void uniffi_dotlottie_player_fn_method_observer_on_load(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_load_error(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_loop(void*_Nonnull ptr, uint32_t loop_count, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_pause(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -317,6 +321,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_request_frame(v
);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_resize(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_seek(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_set_config(void

Expand Down Expand Up @@ -344,6 +351,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_observer_on_frame(void
);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load_error(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_loop(void

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ float uniffi_dotlottie_player_fn_method_dotlottieplayer_request_frame(void*_Nonn
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_resize(void*_Nonnull ptr, uint32_t width, uint32_t height, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_seek(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_dotlottieplayer_set_config(void*_Nonnull ptr, RustBuffer config, RustCallStatus *_Nonnull out_status
);
int8_t uniffi_dotlottie_player_fn_method_dotlottieplayer_set_frame(void*_Nonnull ptr, float no, RustCallStatus *_Nonnull out_status
Expand All @@ -127,6 +129,8 @@ void uniffi_dotlottie_player_fn_method_observer_on_frame(void*_Nonnull ptr, floa
);
void uniffi_dotlottie_player_fn_method_observer_on_load(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_load_error(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_loop(void*_Nonnull ptr, uint32_t loop_count, RustCallStatus *_Nonnull out_status
);
void uniffi_dotlottie_player_fn_method_observer_on_pause(void*_Nonnull ptr, RustCallStatus *_Nonnull out_status
Expand Down Expand Up @@ -317,6 +321,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_request_frame(v
);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_resize(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_seek(void

);
uint16_t uniffi_dotlottie_player_checksum_method_dotlottieplayer_set_config(void

Expand Down Expand Up @@ -344,6 +351,9 @@ uint16_t uniffi_dotlottie_player_checksum_method_observer_on_frame(void
);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_load_error(void

);
uint16_t uniffi_dotlottie_player_checksum_method_observer_on_loop(void

Expand Down

0 comments on commit 9f9ae83

Please sign in to comment.