Impressia/Vernissage/Widgets/ImageRow.swift

70 lines
2.4 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-09 14:48:41 +01:00
@State public var status: StatusData
@State private var imageHeight = UIScreen.main.bounds.width
@State private var imageWidth = UIScreen.main.bounds.width
2023-01-03 14:09:22 +01:00
var body: some View {
2023-01-09 14:48:41 +01:00
if let attachmenData = self.status.attachments().first,
2023-01-03 14:09:22 +01:00
let uiImage = UIImage(data: attachmenData.data) {
ZStack {
2023-01-09 14:48:41 +01:00
if self.status.sensitive {
ContentWarning(blurhash: attachmenData.blurhash, spoilerText: self.status.spoilerText) {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.transition(.opacity)
}
} else {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
}
2023-01-03 14:09:22 +01:00
2023-01-09 14:48:41 +01:00
if let count = self.status.attachments().count, count > 1 {
2023-01-03 14:09:22 +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)
.onAppear {
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
}
2023-01-03 14:09:22 +01:00
}
}
private func recalculateSizeOfDownloadedImage(uiImage: UIImage) {
let imgHeight = uiImage.size.height
let imgWidth = uiImage.size.width
let calculatedHeight = self.calculateHeight(width: imgWidth, height: imgHeight)
self.imageHeight = (calculatedHeight > 0 && calculatedHeight < .infinity) ? calculatedHeight : UIScreen.main.bounds.width
}
private func calculateHeight(width: Double, height: Double) -> CGFloat {
let divider = width / UIScreen.main.bounds.size.width
return height / divider
}
2023-01-03 14:09:22 +01:00
}
struct ImageRow_Previews: PreviewProvider {
static var previews: some View {
2023-01-09 14:48:41 +01:00
Text("")
// ImageRow(status: [])
2023-01-03 14:09:22 +01:00
}
}