Impressia/Vernissage/Widgets/ImageRow.swift

75 lines
2.6 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 uiImage:UIImage?
private let attachmentData: AttachmentData?
init(statusData: StatusData) {
self.status = statusData
self.attachmentData = statusData.attachments().first
if let attachmenData = self.attachmentData, let uiImage = UIImage(data: attachmenData.data) {
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
} else {
self.uiImage = nil
self.imageHeight = UIScreen.main.bounds.width
self.imageWidth = UIScreen.main.bounds.width
}
}
var body: some View {
if let uiImage, let attachmentData {
2023-01-03 14:09:22 +01:00
ZStack {
2023-01-09 14:48:41 +01:00
if self.status.sensitive {
ContentWarning(blurhash: attachmentData.blurhash, spoilerText: self.status.spoilerText) {
2023-01-09 14:48:41 +01:00
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.transition(.opacity)
}
} else {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
}
2023-01-13 21:11:30 +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)
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
}
}