Impressia/Vernissage/Widgets/ImageRowAsync.swift

106 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-23 09:47:04 +01:00
@State private var imageHeight: Double
@State private var imageWidth: Double
@State private var heightWasPrecalculated: Bool
2023-01-05 11:55:20 +01:00
2023-01-21 18:01:17 +01:00
init(statusViewModel: StatusViewModel) {
self.statusViewModel = statusViewModel
2023-01-23 09:47:04 +01:00
if let firstAttachment = statusViewModel.mediaAttachments.first,
let imgHeight = (firstAttachment.meta as? ImageMetadata)?.original?.height,
let imgWidth = (firstAttachment.meta as? ImageMetadata)?.original?.width {
let divider = Double(imgWidth) / UIScreen.main.bounds.size.width
let calculatedHeight = Double(imgHeight) / divider
self.imageWidth = UIScreen.main.bounds.width
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
self.heightWasPrecalculated = true
} else {
self.imageWidth = UIScreen.main.bounds.width
self.imageHeight = UIScreen.main.bounds.width
heightWasPrecalculated = false
}
2023-01-21 18:01:17 +01:00
}
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)
} 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
}
}
2023-01-23 09:47:04 +01:00
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
}