-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from igorcferreira/feature/code_optimisation
Feature/code optimisation
- Loading branch information
Showing
2 changed files
with
73 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Igor Ferreira on 9/9/22. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
private let kDefaultGIFFrameInterval: TimeInterval = 1.0 / 24.0 | ||
|
||
struct PresentationController { | ||
let source: GIFSource | ||
let frameRate: FrameRate | ||
let action: (GIFSource) async throws -> Void | ||
@Binding var animate: Bool | ||
@Binding var loop: Bool | ||
|
||
init(source: GIFSource, frameRate: FrameRate, animate: Binding<Bool>, loop: Binding<Bool>, action: @Sendable @escaping (GIFSource) async throws -> Void = { _ in }) { | ||
self.source = source | ||
self.action = action | ||
self.frameRate = frameRate | ||
self._animate = animate | ||
self._loop = loop | ||
} | ||
|
||
func start(imageLoader: ImageLoader, fallbackImage: RawImage, frameUpdate: (RawImage) async -> Void) async { | ||
do { | ||
repeat { | ||
for try await imageFrame in try await imageLoader.load(source: source) { | ||
try await update(imageFrame, frameUpdate: frameUpdate) | ||
if !animate { break } | ||
} | ||
try await action(source) | ||
} while(self.loop && self.animate) | ||
} catch { | ||
if !(error is CancellationError) { | ||
await frameUpdate(fallbackImage) | ||
} | ||
} | ||
} | ||
|
||
private func update(_ imageFrame: ImageFrame, frameUpdate: (RawImage) async -> Void) async throws { | ||
await frameUpdate(RawImage.create(with: imageFrame.image)) | ||
let calculatedInterval = imageFrame.interval ?? kDefaultGIFFrameInterval | ||
let interval: Double | ||
switch frameRate { | ||
case .static(let fps): | ||
interval = (1.0 / Double(fps)) | ||
case .limited(let fps): | ||
let intervalLimit = (1.0 / Double(fps)) | ||
interval = max(calculatedInterval, intervalLimit) | ||
case .dynamic: | ||
interval = imageFrame.interval ?? kDefaultGIFFrameInterval | ||
} | ||
try await Task.sleep(nanoseconds: UInt64(interval * 1_000_000_000.0)) | ||
} | ||
} |