Impressia/Vernissage/Widgets/ImageRow.swift

90 lines
4.1 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.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-03 14:09:22 +01:00
//
import SwiftUI
struct ImageRow: View {
2023-01-13 21:11:30 +01:00
private let status: StatusData
2023-03-16 20:58:39 +01:00
private let attachmentsData: [AttachmentData]
private let firstAttachment: AttachmentData?
2023-02-02 17:35:41 +01:00
@State private var imageHeight: Double
@State private var imageWidth: Double
2023-03-16 20:58:39 +01:00
@State private var selected: String
init(statusData: StatusData) {
self.status = statusData
2023-03-16 20:58:39 +01:00
self.attachmentsData = statusData.attachments()
self.firstAttachment = self.attachmentsData.first
self.selected = String.empty()
2023-01-26 20:35:24 +01:00
// Calculate size of frame (first from cache, then from real image, then from metadata).
2023-03-16 20:58:39 +01:00
if let firstAttachment, let size = ImageSizeService.shared.get(for: firstAttachment.url) {
2023-01-26 20:35:24 +01:00
self.imageWidth = size.width
self.imageHeight = size.height
2023-03-16 20:58:39 +01:00
} else if let firstAttachment, firstAttachment.metaImageWidth > 0 && firstAttachment.metaImageHeight > 0 {
let size = ImageSizeService.shared.calculate(for: firstAttachment.url,
width: firstAttachment.metaImageWidth,
height: firstAttachment.metaImageHeight)
2023-01-26 20:35:24 +01:00
self.imageWidth = size.width
self.imageHeight = size.height
} else {
self.imageHeight = UIScreen.main.bounds.width
self.imageWidth = UIScreen.main.bounds.width
}
}
var body: some View {
2023-03-17 08:00:18 +01:00
if self.attachmentsData.count == 1, let firstAttachment = self.firstAttachment {
ImageRowItem(status: self.status, attachmentData: firstAttachment) { (imageWidth, imageHeight) in
// When we download image and calculate real size we have to change view size.
if imageWidth != self.imageWidth || imageHeight != self.imageHeight {
2023-03-17 13:15:21 +01:00
withAnimation(.linear(duration: 0.4)) {
2023-03-16 20:58:39 +01:00
self.imageWidth = imageWidth
self.imageHeight = imageHeight
2023-01-26 15:10:47 +01:00
}
2023-02-07 10:20:24 +01:00
}
}
2023-03-17 08:00:18 +01:00
.frame(width: self.imageWidth, height: self.imageHeight)
} else {
TabView(selection: $selected) {
ForEach(self.attachmentsData, id: \.id) { attachmentData in
ImageRowItem(status: self.status, attachmentData: attachmentData) { (imageWidth, imageHeight) in
// When we download image and calculate real size we have to change view size (only when image is now visible).
if attachmentData.id == self.selected {
if imageWidth != self.imageWidth || imageHeight != self.imageHeight {
2023-03-17 13:15:21 +01:00
withAnimation(.linear(duration: 0.4)) {
2023-03-17 08:00:18 +01:00
self.imageWidth = imageWidth
self.imageHeight = imageHeight
}
}
}
}
2023-03-17 08:00:18 +01:00
.tag(attachmentData.id)
}
2023-01-03 14:09:22 +01:00
}
2023-03-29 09:40:14 +02:00
.onFirstAppear {
self.selected = self.attachmentsData.first?.id ?? String.empty()
}
2023-03-17 08:00:18 +01:00
.onChange(of: selected, perform: { attachmentId in
if let attachment = attachmentsData.first(where: { item in item.id == attachmentId }) {
let doubleImageWidth = Double(attachment.metaImageWidth)
let doubleImageHeight = Double(attachment.metaImageHeight)
2023-03-17 08:00:18 +01:00
if doubleImageWidth != self.imageWidth || doubleImageHeight != self.imageHeight {
2023-03-17 13:15:21 +01:00
withAnimation(.linear(duration: 0.4)) {
2023-03-17 08:00:18 +01:00
self.imageWidth = doubleImageWidth
self.imageHeight = doubleImageHeight
}
}
}
})
.frame(width: self.imageWidth, height: self.imageHeight)
.tabViewStyle(PageTabViewStyle())
}
2023-02-12 08:18:05 +01:00
}
2023-01-03 14:09:22 +01:00
}