Impressia/Vernissage/Widgets/ImagesCarousel.swift

138 lines
4.9 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
2023-01-26 15:10:47 +01:00
import MastodonKit
2023-01-03 14:09:22 +01:00
struct ImagesCarousel: View {
2023-01-10 20:38:02 +01:00
@State public var attachments: [AttachmentViewModel]
2023-01-26 15:10:47 +01:00
@State private var imageHeight: Double
@State private var imageWidth: Double
@State private var selected: String
@State private var heightWasPrecalculated: Bool
2023-01-08 14:50:37 +01:00
2023-01-14 11:57:28 +01:00
@Binding public var selectedAttachmentId: String?
2023-01-08 15:43:55 +01:00
@Binding public var exifCamera: String?
@Binding public var exifExposure: String?
@Binding public var exifCreatedDate: String?
@Binding public var exifLens: String?
2023-01-08 14:50:37 +01:00
2023-01-26 15:10:47 +01:00
init(attachments: [AttachmentViewModel],
selectedAttachmentId: Binding<String?>,
exifCamera: Binding<String?>,
exifExposure: Binding<String?>,
exifCreatedDate: Binding<String?>,
exifLens: Binding<String?>
) {
_selectedAttachmentId = selectedAttachmentId
_exifCamera = exifCamera
_exifExposure = exifExposure
_exifCreatedDate = exifCreatedDate
_exifLens = exifLens
self.attachments = attachments
self.selected = String.empty()
var imgHeight = 0.0
var imgWidth = 0.0
2023-01-26 20:35:24 +01:00
var attachment: AttachmentViewModel?
2023-01-26 15:10:47 +01:00
for item in attachments {
let attachmentheight = Double((item.meta as? ImageMetadata)?.original?.height ?? 0)
if attachmentheight > imgHeight {
2023-01-26 20:35:24 +01:00
attachment = item
2023-01-26 15:10:47 +01:00
imgHeight = attachmentheight
imgWidth = Double((item.meta as? ImageMetadata)?.original?.width ?? 0)
}
}
2023-01-26 20:35:24 +01:00
// Attachments doesn't have any metadata with sizes, thus we have to use first one.
if attachment == nil {
attachment = attachments.first
}
// Calculate size of frame (first from cache, then from metadata).
if let attachment, let size = ImageSizeService.shared.getImageSize(for: attachment.url) {
self.imageWidth = size.width
self.imageHeight = size.height
self.heightWasPrecalculated = true
} else if let attachment, imgHeight > 0 && imgWidth > 0 {
let size = ImageSizeService.shared.calculateSize(for: attachment.url, width: imgWidth, height: imgHeight)
self.imageWidth = size.width
self.imageHeight = size.height
2023-01-26 15:10:47 +01:00
self.heightWasPrecalculated = true
} else {
self.imageWidth = UIScreen.main.bounds.width
self.imageHeight = UIScreen.main.bounds.width * 0.75
self.heightWasPrecalculated = false
}
}
2023-01-03 14:09:22 +01:00
var body: some View {
2023-01-14 11:57:28 +01:00
TabView(selection: $selected) {
2023-01-08 14:50:37 +01:00
ForEach(attachments, id: \.id) { attachment in
2023-01-26 15:10:47 +01:00
ImageCarouselPicture(attachment: attachment) { (attachment, imageData) in
withAnimation {
2023-01-26 20:35:24 +01:00
self.recalculateImageHeight(attachment: attachment, imageData: imageData)
2023-01-26 15:10:47 +01:00
}
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
attachment.set(data: imageData)
}
2023-01-03 14:09:22 +01:00
}
2023-01-26 15:10:47 +01:00
.tag(attachment.id)
2023-01-03 14:09:22 +01:00
}
}
2023-01-26 15:10:47 +01:00
.frame(height: CGFloat(self.imageHeight))
2023-01-03 14:09:22 +01:00
.tabViewStyle(PageTabViewStyle())
2023-01-14 11:57:28 +01:00
.onChange(of: selected, perform: { index in
self.selectedAttachmentId = selected
2023-01-08 14:50:37 +01:00
if let attachment = attachments.first(where: { item in item.id == index }) {
2023-01-08 15:43:55 +01:00
self.exifCamera = attachment.exifCamera
self.exifExposure = attachment.exifExposure
self.exifCreatedDate = attachment.exifCreatedDate
self.exifLens = attachment.exifLens
2023-01-08 14:50:37 +01:00
}
})
2023-01-03 14:09:22 +01:00
.onAppear {
2023-01-14 11:57:28 +01:00
self.selected = self.attachments.first?.id ?? String.empty()
2023-01-03 14:09:22 +01:00
}
}
2023-01-26 20:35:24 +01:00
private func recalculateImageHeight(attachment: AttachmentViewModel, imageData: Data) {
2023-01-26 15:10:47 +01:00
guard heightWasPrecalculated == false else {
return
}
2023-01-03 14:09:22 +01:00
var imageHeight = 0.0
var imageWidth = 0.0
for item in attachments {
2023-01-10 20:38:02 +01:00
if let data = item.data, let image = UIImage(data: data) {
2023-01-03 14:09:22 +01:00
if image.size.height > imageHeight {
imageHeight = image.size.height
imageWidth = image.size.width
}
}
}
2023-01-26 15:10:47 +01:00
if let image = UIImage(data: imageData) {
if image.size.height > imageHeight {
imageHeight = image.size.height
imageWidth = image.size.width
}
}
2023-01-26 20:35:24 +01:00
let size = ImageSizeService.shared.calculateSize(for: attachment.url, width: imageWidth, height: imageHeight)
self.imageWidth = size.width
self.imageHeight = size.height
2023-01-03 14:09:22 +01:00
}
}