Impressia/Vernissage/Widgets/ImageRow.swift

91 lines
3.8 KiB
Swift
Raw Normal View History

2023-01-03 14:09:22 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
struct ImageRow: View {
2023-01-13 21:11:30 +01:00
private let status: StatusData
private let imageHeight: Double
private let imageWidth: Double
private let attachmentData: AttachmentData?
2023-01-26 15:10:47 +01:00
@State private var uiImage:UIImage?
init(statusData: StatusData) {
self.status = statusData
self.attachmentData = statusData.attachments().first
2023-01-26 15:10:47 +01:00
if let imageData = self.attachmentData?.data, let uiImage = UIImage(data: imageData) {
self.uiImage = uiImage
let imgHeight = uiImage.size.height
let imgWidth = uiImage.size.width
let divider = imgWidth / UIScreen.main.bounds.size.width
let calculatedHeight = imgHeight / divider
2023-01-03 14:09:22 +01:00
2023-01-13 21:11:30 +01:00
self.imageWidth = UIScreen.main.bounds.width
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
2023-01-26 15:10:47 +01:00
} else if let imgWidth = attachmentData?.metaImageWidth, let imgHeight = attachmentData?.metaImageHeight {
let divider = Double(imgWidth) / UIScreen.main.bounds.size.width
let calculatedHeight = Double(imgHeight) / divider
self.uiImage = nil
self.imageWidth = UIScreen.main.bounds.width
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
} else {
self.uiImage = nil
self.imageHeight = UIScreen.main.bounds.width
self.imageWidth = UIScreen.main.bounds.width
}
}
var body: some View {
2023-01-26 15:10:47 +01:00
if let attachmentData {
if let uiImage {
ZStack {
if self.status.sensitive {
ContentWarning(blurhash: attachmentData.blurhash, spoilerText: self.status.spoilerText) {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.transition(.opacity)
}
} else {
2023-01-09 14:48:41 +01:00
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
}
2023-01-26 15:10:47 +01:00
if let count = self.status.attachments().count, count > 1 {
BottomRight {
Text("1 / \(count)")
.padding(.horizontal, 6)
.padding(.vertical, 3)
.font(.caption2)
.foregroundColor(.black)
.background(.ultraThinMaterial, in: Capsule())
}.padding()
}
2023-01-03 14:09:22 +01:00
}
2023-01-26 15:10:47 +01:00
.frame(width: self.imageWidth, height: self.imageHeight)
} else {
BlurredImage(blurhash: attachmentData.blurhash)
.frame(width: self.imageWidth, height: self.imageHeight)
.task {
do {
if let imageData = try await RemoteFileService.shared.fetchData(url: attachmentData.url) {
HomeTimelineService.shared.updateAttachmentDataImage(attachmentData: attachmentData, imageData: imageData)
self.uiImage = UIImage(data: imageData)
}
} catch {
ErrorService.shared.handle(error, message: "Cannot download the image.")
}
}
2023-01-03 14:09:22 +01:00
}
}
}
}