Impressia/Vernissage/Widgets/ImageRowItem.swift

162 lines
5.8 KiB
Swift
Raw Normal View History

2023-03-16 20:58:39 +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-03-16 20:58:39 +01:00
//
import SwiftUI
struct ImageRowItem: View {
@EnvironmentObject var applicationState: ApplicationState
@EnvironmentObject var client: Client
@EnvironmentObject var routerPath: RouterPath
2023-03-16 20:58:39 +01:00
private let status: StatusData
private let attachmentData: AttachmentData
@State private var uiImage: UIImage?
@State private var showThumbImage = false
@State private var cancelled = true
@State private var error: Error?
2023-03-17 08:00:18 +01:00
@State private var opacity = 0.0
2023-03-16 20:58:39 +01:00
private let onImageDownloaded: (Double, Double) -> Void
init(status: StatusData, attachmentData: AttachmentData, onImageDownloaded: @escaping (_: Double, _: Double) -> Void) {
2023-03-16 20:58:39 +01:00
self.status = status
self.attachmentData = attachmentData
self.onImageDownloaded = onImageDownloaded
2023-03-16 20:58:39 +01:00
if let imageData = attachmentData.data {
self.uiImage = UIImage(data: imageData)
}
}
2023-03-16 20:58:39 +01:00
var body: some View {
if let uiImage {
ZStack {
if self.status.sensitive && !self.applicationState.showSensitive {
ZStack {
ContentWarning(spoilerText: self.status.spoilerText) {
2023-03-16 20:58:39 +01:00
self.imageView(uiImage: uiImage)
2023-03-16 20:58:39 +01:00
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
}
} blurred: {
BlurredImage(blurhash: attachmentData.blurhash)
.onTapGesture {
self.navigateToStatus()
}
2023-03-16 20:58:39 +01:00
}
}
2023-03-29 09:40:14 +02:00
.opacity(self.opacity)
2023-03-17 08:00:18 +01:00
.onAppear {
withAnimation {
self.opacity = 1.0
}
}
2023-03-16 20:58:39 +01:00
} else {
ZStack {
self.imageView(uiImage: uiImage)
2023-03-16 20:58:39 +01:00
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
}
}
2023-03-29 09:40:14 +02:00
.opacity(self.opacity)
2023-03-17 08:00:18 +01:00
.onAppear {
withAnimation {
self.opacity = 1.0
}
}
2023-03-16 20:58:39 +01:00
}
}
} else {
if cancelled {
BlurredImage(blurhash: attachmentData.blurhash)
.task {
await self.downloadImage(attachmentData: attachmentData)
}
} else if let error {
2023-03-16 20:58:39 +01:00
ZStack {
BlurredImage(blurhash: attachmentData.blurhash)
2023-03-16 20:58:39 +01:00
ErrorView(error: error) {
await self.downloadImage(attachmentData: attachmentData)
}
.padding()
}
} else {
BlurredImage(blurhash: attachmentData.blurhash)
.onTapGesture {
2023-03-16 20:58:39 +01:00
self.navigateToStatus()
}
.task {
await self.downloadImage(attachmentData: attachmentData)
}
}
}
}
2023-03-16 20:58:39 +01:00
@ViewBuilder
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 {
2023-03-16 20:58:39 +01:00
self.navigateToStatus()
}
.imageContextMenu(client: self.client, statusData: self.status)
}
2023-03-16 20:58:39 +01:00
private func downloadImage(attachmentData: AttachmentData) async {
do {
if let imageData = try await RemoteFileService.shared.fetchData(url: attachmentData.url),
let downloadedImage = UIImage(data: imageData) {
2023-03-16 20:58:39 +01:00
let size = ImageSizeService.shared.calculate(for: attachmentData.url,
width: downloadedImage.size.width,
height: downloadedImage.size.height)
2023-03-17 13:15:21 +01:00
self.uiImage = downloadedImage
2023-03-16 20:58:39 +01:00
self.onImageDownloaded(size.width, size.height)
2023-03-16 20:58:39 +01:00
HomeTimelineService.shared.update(attachment: attachmentData, withData: imageData, imageWidth: size.width, imageHeight: size.height)
self.error = nil
self.cancelled = false
}
} catch {
if !Task.isCancelled {
ErrorService.shared.handle(error, message: "global.error.errorDuringImageDownload")
self.error = error
} else {
ErrorService.shared.handle(error, message: "global.error.canceledImageDownload")
self.cancelled = true
}
}
}
2023-03-16 20:58:39 +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
))
}
}