Impressia/Vernissage/Widgets/ImageRow.swift

178 lines
7.3 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-02-02 17:35:41 +01:00
@EnvironmentObject var applicationState: ApplicationState
2023-02-03 15:16:30 +01:00
@EnvironmentObject var client: Client
2023-02-02 17:35:41 +01:00
@EnvironmentObject var routerPath: RouterPath
2023-01-13 21:11:30 +01:00
private let status: StatusData
private let attachmentData: AttachmentData?
2023-02-02 17:35:41 +01:00
@State private var imageHeight: Double
@State private var imageWidth: Double
2023-01-26 15:10:47 +01:00
@State private var uiImage:UIImage?
2023-02-02 17:35:41 +01:00
@State private var showThumbImage = false
2023-02-07 10:20:24 +01:00
@State private var error: Error?
2023-02-13 16:10:16 +01:00
@State private var cancelled = true
2023-01-26 15:10:47 +01:00
init(statusData: StatusData) {
self.status = statusData
self.attachmentData = statusData.attachments().first
2023-01-26 20:35:24 +01:00
// Calculate size of frame (first from cache, then from real image, then from metadata).
2023-01-28 21:09:31 +01:00
if let attachmentData, let size = ImageSizeService.shared.get(for: attachmentData.url) {
2023-01-26 20:35:24 +01:00
self.imageWidth = size.width
self.imageHeight = size.height
} else if let attachmentData, attachmentData.metaImageWidth > 0 && attachmentData.metaImageHeight > 0 {
2023-01-28 21:09:31 +01:00
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: attachmentData.metaImageWidth,
height: attachmentData.metaImageHeight)
2023-01-26 20:35:24 +01:00
self.imageWidth = size.width
self.imageHeight = size.height
} else {
self.uiImage = nil
self.imageHeight = UIScreen.main.bounds.width
self.imageWidth = UIScreen.main.bounds.width
}
}
var body: some View {
2023-01-26 15:10:47 +01:00
if let attachmentData {
if let uiImage {
ZStack {
2023-03-05 09:53:06 +01:00
if self.status.sensitive && !self.applicationState.showSensitive {
ZStack {
ContentWarning(blurhash: attachmentData.blurhash, spoilerText: self.status.spoilerText) {
self.imageView(uiImage: uiImage)
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
2023-02-12 08:18:05 +01:00
}
}
2023-01-26 15:10:47 +01:00
}
} else {
2023-02-02 17:35:41 +01:00
ZStack {
self.imageView(uiImage: uiImage)
2023-02-02 17:35:41 +01:00
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
}
}
2023-01-09 14:48:41 +01:00
}
2023-01-26 15:10:47 +01:00
if let count = self.status.attachments().count, count > 1 {
BottomRight {
Text("1 / \(count)")
.padding(.horizontal, 6)
.padding(.vertical, 3)
.font(.caption2)
.foregroundColor(.black)
.background(.ultraThinMaterial, in: Capsule())
}.padding()
}
2023-01-03 14:09:22 +01:00
}
2023-01-26 15:10:47 +01:00
.frame(width: self.imageWidth, height: self.imageHeight)
} else {
2023-02-13 16:10:16 +01:00
if cancelled {
BlurredImage(blurhash: attachmentData.blurhash)
.frame(width: self.imageWidth, height: self.imageHeight)
.task {
await self.downloadImage(attachmentData: attachmentData)
}
}
else if let error {
2023-02-07 10:20:24 +01:00
ZStack {
BlurredImage(blurhash: attachmentData.blurhash)
.frame(width: self.imageWidth, height: self.imageHeight)
ErrorView(error: error) {
await self.downloadImage(attachmentData: attachmentData)
2023-01-26 15:10:47 +01:00
}
2023-02-07 10:20:24 +01:00
.padding()
2023-01-26 15:10:47 +01:00
}
2023-02-07 10:20:24 +01:00
} else {
BlurredImage(blurhash: attachmentData.blurhash)
.frame(width: self.imageWidth, height: self.imageHeight)
2023-02-12 08:18:05 +01:00
.onTapGesture{
self.navigateToStatus()
}
2023-02-07 10:20:24 +01:00
.task {
await self.downloadImage(attachmentData: attachmentData)
}
}
}
}
}
private func imageView(uiImage: UIImage) -> some View {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.onTapGesture(count: 2) {
Task {
try? await self.client.statuses?.favourite(statusId: self.status.id)
}
self.showThumbImage = true
HapticService.shared.fireHaptic(of: .buttonPress)
}
.onTapGesture{
self.navigateToStatus()
}
.imageContextMenu(client: self.client, statusData: self.status)
}
2023-02-07 10:20:24 +01:00
private func downloadImage(attachmentData: AttachmentData) async {
do {
2023-02-09 19:24:41 +01:00
if let imageData = try await RemoteFileService.shared.fetchData(url: attachmentData.url),
let downloadedImage = UIImage(data: imageData) {
2023-02-07 10:20:24 +01:00
2023-02-09 19:24:41 +01:00
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: downloadedImage.size.width,
height: downloadedImage.size.height)
if self.imageHeight != size.height || self.imageWidth != size.width {
withAnimation(.linear) {
self.imageWidth = size.width
self.imageHeight = size.height
}
}
2023-02-09 19:24:41 +01:00
self.uiImage = downloadedImage
HomeTimelineService.shared.update(attachment: attachmentData, withData: imageData, imageWidth: size.width, imageHeight: size.height)
2023-02-13 16:10:16 +01:00
self.error = nil
self.cancelled = false
2023-01-03 14:09:22 +01:00
}
2023-02-07 10:20:24 +01:00
} catch {
2023-02-13 16:10:16 +01:00
if !Task.isCancelled {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "global.error.errorDuringImageDownload")
2023-02-13 16:10:16 +01:00
self.error = error
} else {
2023-03-13 13:53:36 +01:00
ErrorService.shared.handle(error, message: "global.error.canceledImageDownload")
2023-02-13 16:10:16 +01:00
self.cancelled = true
}
2023-01-03 14:09:22 +01:00
}
}
2023-02-12 08:18:05 +01:00
private func navigateToStatus() {
self.routerPath.navigate(to: .status(
id: status.rebloggedStatusId ?? status.id,
blurhash: status.attachments().first?.blurhash,
highestImageUrl: status.attachments().getHighestImage()?.url,
metaImageWidth: status.attachments().first?.metaImageWidth,
metaImageHeight: status.attachments().first?.metaImageHeight
))
}
2023-01-03 14:09:22 +01:00
}