Impressia/Vernissage/Widgets/ImageRowAsync.swift

107 lines
4.3 KiB
Swift
Raw Normal View History

2023-01-05 11:55:20 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
import MastodonSwift
import NukeUI
struct ImageRowAsync: View {
@State public var attachments: [Attachment]
@State private var imageHeight = UIScreen.main.bounds.width
2023-01-09 06:47:34 +01:00
@State private var imageWidth = UIScreen.main.bounds.width
@State private var heightWasPrecalculated = true
2023-01-05 11:55:20 +01:00
var body: some View {
if let attachment = attachments.first {
ZStack {
2023-01-09 06:47:34 +01:00
LazyImage(url: attachment.url) { state in
if let image = state.image {
image
} else if state.error != nil {
ZStack {
Rectangle()
.fill(Color.placeholderText)
2023-01-09 10:06:21 +01:00
.scaledToFill()
2023-01-09 06:47:34 +01:00
VStack(alignment: .center) {
Spacer()
Text("Cannot download image")
.foregroundColor(.systemBackground)
Spacer()
}
}
2023-01-09 10:06:21 +01:00
.frame(width: self.imageWidth, height: self.imageHeight)
2023-01-09 06:47:34 +01:00
} else {
VStack(alignment: .center) {
if let blurhash = attachment.blurhash,
let uiImage = UIImage(blurHash: blurhash, size: CGSize(width: 32, height: 32)) {
Image(uiImage: uiImage)
.resizable()
.scaledToFill()
} else {
Rectangle()
.fill(Color.placeholderText)
2023-01-09 10:06:21 +01:00
.scaledToFill()
2023-01-09 06:47:34 +01:00
}
}
2023-01-09 10:06:21 +01:00
.frame(width: self.imageWidth, height: self.imageHeight)
2023-01-09 06:47:34 +01:00
}
}
.onSuccess { imageResponse in
2023-01-09 08:29:55 +01:00
self.recalculateSizeOfDownloadedImage(imageResponse: imageResponse)
2023-01-09 06:47:34 +01:00
}
.frame(width: self.imageWidth, height: self.imageHeight)
2023-01-05 11:55:20 +01:00
if let count = 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-09 06:47:34 +01:00
.onAppear {
2023-01-09 08:29:55 +01:00
self.recalculateSizeFromMetadata()
2023-01-09 06:47:34 +01:00
}
2023-01-05 11:55:20 +01:00
}
}
2023-01-09 06:47:34 +01:00
2023-01-09 08:29:55 +01:00
private func recalculateSizeOfDownloadedImage(imageResponse: ImageResponse) {
if heightWasPrecalculated == false {
let imgHeight = imageResponse.image.size.height
let imgWidth = imageResponse.image.size.width
let calculatedHeight = self.calculateHeight(width: imgWidth, height: imgHeight)
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
}
}
private func recalculateSizeFromMetadata() {
if let firstAttachment = attachments.first,
let imgHeight = (firstAttachment.meta as? ImageMetadata)?.original?.height,
let imgWidth = (firstAttachment.meta as? ImageMetadata)?.original?.width {
let calculatedHeight = self.calculateHeight(width: Double(imgWidth), height: Double(imgHeight))
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
} else {
heightWasPrecalculated = false
}
}
2023-01-09 06:47:34 +01:00
private func calculateHeight(width: Double, height: Double) -> CGFloat {
let divider = width / UIScreen.main.bounds.size.width
return height / divider
}
2023-01-05 11:55:20 +01:00
}
struct ImageRowAsync_Previews: PreviewProvider {
static var previews: some View {
ImageRow(attachments: [])
}
}