Impressia/Vernissage/Widgets/ImageRowItemAsync.swift

143 lines
5.1 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
//
2023-03-16 20:58:39 +01:00
import SwiftUI
import PixelfedKit
import NukeUI
struct ImageRowItemAsync: View {
@EnvironmentObject var applicationState: ApplicationState
@EnvironmentObject var client: Client
@EnvironmentObject var routerPath: RouterPath
private var statusViewModel: StatusModel
private var attachment: AttachmentModel
2023-03-16 20:58:39 +01:00
@State private var showThumbImage = false
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(statusViewModel: StatusModel, attachment: AttachmentModel, onImageDownloaded: @escaping (_: Double, _: Double) -> Void) {
2023-03-16 20:58:39 +01:00
self.statusViewModel = statusViewModel
self.attachment = attachment
self.onImageDownloaded = onImageDownloaded
}
2023-03-16 20:58:39 +01:00
var body: some View {
LazyImage(url: attachment.url) { state in
if let image = state.image {
if self.statusViewModel.sensitive && !self.applicationState.showSensitive {
ZStack {
ContentWarning(spoilerText: self.statusViewModel.spoilerText) {
2023-03-16 20:58:39 +01:00
self.imageView(image: image)
} blurred: {
BlurredImage(blurhash: attachment.blurhash)
.onTapGesture {
self.navigateToStatus()
}
2023-03-16 20:58:39 +01:00
}
2023-03-16 20:58:39 +01:00
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
}
}
.opacity(self.opacity)
2023-03-16 20:58:39 +01:00
.onAppear {
if let uiImage = state.imageResponse?.image {
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
}
withAnimation {
self.opacity = 1.0
}
2023-03-16 20:58:39 +01:00
}
} else {
ZStack {
self.imageView(image: image)
2023-03-16 20:58:39 +01:00
if showThumbImage {
FavouriteTouch {
self.showThumbImage = false
}
}
}
.opacity(self.opacity)
2023-03-16 20:58:39 +01:00
.onAppear {
if let uiImage = state.imageResponse?.image {
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
}
withAnimation {
self.opacity = 1.0
}
2023-03-16 20:58:39 +01:00
}
}
} else if state.error != nil {
ZStack {
Rectangle()
.fill(Color.placeholderText)
.scaledToFill()
2023-03-16 20:58:39 +01:00
VStack(alignment: .center) {
Spacer()
Text("global.error.errorDuringImageDownload", comment: "Cannot download image")
.foregroundColor(.systemBackground)
Spacer()
}
}
} else {
VStack(alignment: .center) {
BlurredImage(blurhash: attachment.blurhash)
.onTapGesture {
self.navigateToStatus()
}
2023-03-16 20:58:39 +01:00
}
}
}
.priority(.high)
}
2023-03-16 20:58:39 +01:00
@ViewBuilder
private func imageView(image: Image) -> some View {
image
.resizable()
.aspectRatio(contentMode: .fit)
.onTapGesture(count: 2) {
Task {
try? await self.client.statuses?.favourite(statusId: self.statusViewModel.id)
}
self.showThumbImage = true
HapticService.shared.fireHaptic(of: .buttonPress)
}
.onTapGesture {
self.navigateToStatus()
2023-03-16 20:58:39 +01:00
}
.imageContextMenu(client: self.client, statusModel: self.statusViewModel)
}
private func navigateToStatus() {
self.routerPath.navigate(to: .status(
id: statusViewModel.id,
blurhash: statusViewModel.mediaAttachments.first?.blurhash,
highestImageUrl: statusViewModel.mediaAttachments.getHighestImage()?.url,
metaImageWidth: statusViewModel.getImageWidth(),
metaImageHeight: statusViewModel.getImageHeight()
))
}
2023-03-16 20:58:39 +01:00
private func recalculateSizeOfDownloadedImage(uiImage: UIImage) {
let size = ImageSizeService.shared.calculate(for: attachment.url,
width: uiImage.size.width,
height: uiImage.size.height)
2023-03-16 20:58:39 +01:00
self.onImageDownloaded(size.width, size.height)
}
}