Impressia/Vernissage/Widgets/ImageRowItemAsync.swift

213 lines
8.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
//
2023-03-16 20:58:39 +01:00
import SwiftUI
import Nuke
import NukeUI
2023-03-16 20:58:39 +01:00
import PixelfedKit
2023-04-07 14:20:12 +02:00
import ClientKit
2023-04-07 14:38:50 +02:00
import ServicesKit
2023-04-07 16:59:18 +02:00
import EnvironmentKit
import WidgetsKit
2023-03-16 20:58:39 +01:00
struct ImageRowItemAsync: View {
@EnvironmentObject var applicationState: ApplicationState
@EnvironmentObject var client: Client
@EnvironmentObject var routerPath: RouterPath
private var statusViewModel: StatusModel
private var attachment: AttachmentModel
private let showAvatar: Bool
private let imageFromCache: Bool
2023-05-25 17:33:04 +02:00
@Binding private var containerWidth: Double
@Binding private var showSpoilerText: Bool
@Binding private var clipToRectangle: Bool
2023-03-16 20:58:39 +01:00
@State private var showThumbImage = false
@State private var opacity = 1.0
2023-04-14 16:50:47 +02:00
@State private var isFavourited = false
2023-03-16 20:58:39 +01:00
private let onImageDownloaded: (Double, Double) -> Void
init(statusViewModel: StatusModel,
attachment: AttachmentModel,
2023-05-05 18:36:15 +02:00
withAvatar showAvatar: Bool = true,
2023-05-25 17:33:04 +02:00
containerWidth: Binding<Double>,
clipToRectangle: Binding<Bool> = Binding.constant(false),
showSpoilerText: Binding<Bool> = Binding.constant(true),
2023-05-05 18:36:15 +02:00
onImageDownloaded: @escaping (_: Double, _: Double) -> Void) {
self.showAvatar = showAvatar
2023-03-16 20:58:39 +01:00
self.statusViewModel = statusViewModel
self.attachment = attachment
self.onImageDownloaded = onImageDownloaded
2023-05-25 17:33:04 +02:00
self._containerWidth = containerWidth
self._showSpoilerText = showSpoilerText
self._clipToRectangle = clipToRectangle
self.imageFromCache = ImagePipeline.shared.cache.containsCachedImage(for: ImageRequest(url: attachment.url))
2023-03-16 20:58:39 +01:00
}
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 {
2023-05-25 17:33:04 +02:00
ContentWarning(spoilerText: self.showSpoilerText ? self.statusViewModel.spoilerText : nil) {
self.imageContainerView(image: image)
.imageContextMenu(statusModel: self.statusViewModel,
attachmentModel: self.attachment,
uiImage: state.imageResponse?.image)
} blurred: {
ZStack {
BlurredImage(blurhash: attachment.blurhash)
if self.showAvatar {
ImageAvatar(displayName: self.statusViewModel.account.displayNameWithoutEmojis,
avatarUrl: self.statusViewModel.account.avatar) {
self.routerPath.navigate(to: .userProfile(accountId: self.statusViewModel.account.id,
accountDisplayName: self.statusViewModel.account.displayNameWithoutEmojis,
accountUserName: self.statusViewModel.account.acct))
}
2023-04-18 13:45:48 +02:00
}
}
.onTapGesture {
self.navigateToStatus()
2023-03-16 20:58:39 +01:00
}
}
}
.opacity(self.opacity)
2023-03-16 20:58:39 +01:00
.onAppear {
if let uiImage = state.imageResponse?.image {
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
}
if self.imageFromCache == false {
self.opacity = 0.0
withAnimation {
self.opacity = 1.0
}
}
2023-03-16 20:58:39 +01:00
}
} else {
self.imageContainerView(image: image)
.imageContextMenu(statusModel: self.statusViewModel,
attachmentModel: self.attachment,
uiImage: state.imageResponse?.image)
.opacity(self.opacity)
.onAppear {
if let uiImage = state.imageResponse?.image {
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
}
if self.imageFromCache == false {
self.opacity = 0.0
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)
}
@ViewBuilder
private func imageContainerView(image: Image) -> some View {
ZStack {
self.imageView(image: image)
if self.showAvatar {
ImageAvatar(displayName: self.statusViewModel.account.displayNameWithoutEmojis,
2023-04-18 13:45:48 +02:00
avatarUrl: self.statusViewModel.account.avatar) {
self.routerPath.navigate(to: .userProfile(accountId: self.statusViewModel.account.id,
accountDisplayName: self.statusViewModel.account.displayNameWithoutEmojis,
accountUserName: self.statusViewModel.account.acct))
}
}
2023-04-16 17:44:35 +02:00
ImageAlternativeText(text: self.attachment.description) { text in
self.routerPath.presentedAlert = .alternativeText(text: text)
}
ImageFavourite(isFavourited: $isFavourited)
FavouriteTouch(showFavouriteAnimation: $showThumbImage)
}
}
2023-03-16 20:58:39 +01:00
@ViewBuilder
private func imageView(image: Image) -> some View {
image
.resizable()
2023-05-25 17:33:04 +02:00
.aspectRatio(contentMode: self.clipToRectangle ? .fill : .fit)
.if(self.clipToRectangle) {
$0.frame(width: self.containerWidth, height: self.containerWidth).clipped()
}
2023-03-16 20:58:39 +01:00
.onTapGesture(count: 2) {
Task {
2023-04-14 16:50:47 +02:00
// Update favourite in Pixelfed server.
2023-03-16 20:58:39 +01:00
try? await self.client.statuses?.favourite(statusId: self.statusViewModel.id)
}
2023-04-14 16:50:47 +02:00
// Run adnimation and haptic feedback.
2023-03-16 20:58:39 +01:00
self.showThumbImage = true
HapticService.shared.fireHaptic(of: .buttonPress)
2023-04-14 16:50:47 +02:00
// Mark favourite booleans used to show star in the timeline view.
self.statusViewModel.favourited = true
withAnimation(.default.delay(2.0)) {
self.isFavourited = true
}
2023-03-16 20:58:39 +01:00
}
.onTapGesture {
self.navigateToStatus()
2023-03-16 20:58:39 +01:00
}
2023-04-14 16:50:47 +02:00
.onAppear {
self.isFavourited = self.statusViewModel.favourited
}
2023-03-16 20:58:39 +01:00
}
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) {
2023-05-25 17:33:04 +02:00
ImageSizeService.shared.save(for: attachment.url,
width: uiImage.size.width,
height: uiImage.size.height)
2023-05-25 17:33:04 +02:00
let size = ImageSizeService.shared.calculate(for: attachment.url, andContainerWidth: UIScreen.main.bounds.size.width)
2023-03-16 20:58:39 +01:00
self.onImageDownloaded(size.width, size.height)
}
}