2023-01-26 15:10:47 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ImageCarouselPicture: View {
|
2023-01-31 12:20:49 +01:00
|
|
|
@ObservedObject public var attachment: AttachmentModel
|
2023-01-26 15:10:47 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
private let onImageDownloaded: (AttachmentModel, Data) -> Void
|
2023-01-26 15:10:47 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
init(attachment: AttachmentModel, onImageDownloaded: @escaping (_: AttachmentModel, _: Data) -> Void) {
|
2023-01-26 15:10:47 +01:00
|
|
|
self.attachment = attachment
|
|
|
|
self.onImageDownloaded = onImageDownloaded
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
if let data = attachment.data, let image = UIImage(data: data) {
|
|
|
|
Image(uiImage: image)
|
|
|
|
.resizable()
|
|
|
|
.aspectRatio(contentMode: .fit)
|
|
|
|
} else {
|
|
|
|
BlurredImage(blurhash: attachment.blurhash)
|
|
|
|
.task {
|
|
|
|
do {
|
|
|
|
// Download image and recalculate exif data.
|
|
|
|
if let imageData = try await RemoteFileService.shared.fetchData(url: attachment.url) {
|
|
|
|
self.onImageDownloaded(attachment, imageData)
|
|
|
|
}
|
|
|
|
} catch {
|
2023-01-27 15:46:09 +01:00
|
|
|
ErrorService.shared.handle(error, message: "Cannot download image for status")
|
2023-01-26 15:10:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|