-
-
Save laevandus/852b8d09e7f53fc18949bf0627cc3428 to your computer and use it in GitHub Desktop.
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
struct AsyncPhoto<ID, Content, Progress, Placeholder>: View where ID: Hashable, Content: View, Progress: View, Placeholder: View { | |
// redacted | |
init(id value: ID = "", | |
scaledSize: CGSize, | |
cache: AsyncPhotoCaching = AsyncPhotoCache.shared, | |
data: @escaping (ID) async -> Data?, | |
content: @escaping (Image) -> Content = { $0 }, | |
progress: @escaping () -> Progress = { ProgressView() }, | |
placeholder: @escaping () -> Placeholder = { Color(white: 0.839) }) { | |
// redacted | |
} | |
var body: some View { | |
// redacted | |
} | |
@MainActor func load() async { | |
// Here we access the cache | |
if let image = cache.image(for: id, size: scaledSize) { | |
phase = .success(Image(uiImage: image)) | |
} | |
else { | |
phase = .loading | |
if let image = await prepareScaledImage(for: id) { | |
guard !Task.isCancelled else { return } | |
phase = .success(image) | |
} | |
else { | |
guard !Task.isCancelled else { return } | |
phase = .placeholder | |
} | |
} | |
} | |
private func prepareScaledImage(for id: ID) async -> Image? { | |
guard let photoData = await data(id) else { return nil } | |
guard let originalImage = UIImage(data: photoData) else { return nil } | |
let scaledImage = await originalImage.scaled(toFill: scaledSize) | |
guard let finalImage = await scaledImage.byPreparingForDisplay() else { return nil } | |
// Here we store the scaled down image in the cache | |
cache.store(finalImage, forID: id) | |
return Image(uiImage: finalImage) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment