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
2023-01-10 08:04:25 +01:00
import MastodonKit
2023-01-05 11:55:20 +01:00
import NukeUI
struct ImageRowAsync: View {
2023-01-10 20:38:02 +01:00
@State public var statusViewModel: StatusViewModel
2023-01-05 11:55:20 +01:00
@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 {
2023-01-10 20:38:02 +01:00
if let attachment = statusViewModel.mediaAttachments.first {
2023-01-05 11:55:20 +01:00
ZStack {
2023-01-09 06:47:34 +01:00
LazyImage(url: attachment.url) { state in
if let image = state.image {
2023-01-10 20:38:02 +01:00
if self.statusViewModel.sensitive {
ContentWarning(blurhash: attachment.blurhash, spoilerText: self.statusViewModel.spoilerText) {
2023-01-09 14:48:41 +01:00
image
}
} else {
image
}
2023-01-09 06:47:34 +01:00
} 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) {
2023-01-09 14:48:41 +01:00
BlurredImage(blurhash: attachment.blurhash)
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
}
2023-01-05 11:55:20 +01:00
2023-01-10 20:38:02 +01:00
if let count = self.statusViewModel.mediaAttachments.count, count > 1 {
2023-01-05 11:55:20 +01:00
BottomRight {
Text("1 / \(count)")
.padding(.horizontal, 6)
.padding(.vertical, 3)
.font(.caption2)
.foregroundColor(.black)
.background(.ultraThinMaterial, in: Capsule())
}.padding()
}
}
.frame(width: self.imageWidth, height: self.imageHeight)
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
}
} else {
EmptyView()
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() {
2023-01-10 20:38:02 +01:00
if let firstAttachment = self.statusViewModel.mediaAttachments.first,
2023-01-09 08:29:55 +01:00
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 {
2023-01-09 14:48:41 +01:00
Text("")
// ImageRowAsync(status: Status())
2023-01-05 11:55:20 +01:00
}
}