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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-16 20:58:39 +01:00
|
|
|
import SwiftUI
|
2023-04-26 14:49:43 +02:00
|
|
|
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
|
|
|
|
2023-10-19 13:24:02 +02:00
|
|
|
@MainActor
|
2023-03-16 20:58:39 +01:00
|
|
|
struct ImageRowItemAsync: View {
|
2023-10-19 13:24:02 +02:00
|
|
|
@Environment(ApplicationState.self) var applicationState
|
|
|
|
@Environment(Client.self) var client
|
|
|
|
@Environment(RouterPath.self) var routerPath
|
2023-03-16 20:58:39 +01:00
|
|
|
|
|
|
|
private var statusViewModel: StatusModel
|
|
|
|
private var attachment: AttachmentModel
|
2023-04-10 11:23:00 +02:00
|
|
|
private let showAvatar: Bool
|
2023-04-26 14:49:43 +02:00
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-16 20:58:39 +01:00
|
|
|
@State private var showThumbImage = false
|
2023-04-26 14:49:43 +02:00
|
|
|
@State private var opacity = 1.0
|
2023-04-14 16:50:47 +02:00
|
|
|
@State private var isFavourited = false
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-16 20:58:39 +01:00
|
|
|
private let onImageDownloaded: (Double, Double) -> Void
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-04-10 11:23:00 +02:00
|
|
|
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) {
|
2023-04-10 11:23:00 +02:00
|
|
|
self.showAvatar = showAvatar
|
2023-03-16 20:58:39 +01:00
|
|
|
self.statusViewModel = statusViewModel
|
|
|
|
self.attachment = attachment
|
|
|
|
self.onImageDownloaded = onImageDownloaded
|
2023-04-26 14:49:43 +02:00
|
|
|
|
2023-05-25 17:33:04 +02:00
|
|
|
self._containerWidth = containerWidth
|
|
|
|
self._showSpoilerText = showSpoilerText
|
|
|
|
self._clipToRectangle = clipToRectangle
|
|
|
|
|
2023-04-26 14:49:43 +02:00
|
|
|
self.imageFromCache = ImagePipeline.shared.cache.containsCachedImage(for: ImageRequest(url: attachment.url))
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02: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) {
|
2023-04-15 08:03:16 +02:00
|
|
|
self.imageContainerView(image: image)
|
2023-04-16 09:05:41 +02:00
|
|
|
.imageContextMenu(statusModel: self.statusViewModel,
|
|
|
|
attachmentModel: self.attachment,
|
|
|
|
uiImage: state.imageResponse?.image)
|
2023-03-29 09:35:01 +02:00
|
|
|
} blurred: {
|
2023-04-15 08:03:16 +02:00
|
|
|
ZStack {
|
|
|
|
BlurredImage(blurhash: attachment.blurhash)
|
2023-05-06 11:47:13 +02:00
|
|
|
if self.showAvatar {
|
|
|
|
ImageAvatar(displayName: self.statusViewModel.account.displayNameWithoutEmojis,
|
2023-09-29 16:03:42 +02:00
|
|
|
avatarUrl: self.statusViewModel.account.avatar,
|
|
|
|
rebloggedAccountDisplayName: self.statusViewModel.reblogStatus?.account.displayNameWithoutEmojis,
|
2023-09-29 16:58:45 +02:00
|
|
|
rebloggedAccountAvatar: self.statusViewModel.reblogStatus?.account.avatar) { isAuthor in
|
|
|
|
if isAuthor {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: self.statusViewModel.account.id,
|
|
|
|
accountDisplayName: self.statusViewModel.account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: self.statusViewModel.account.acct))
|
|
|
|
} else {
|
|
|
|
if let rebloggedAccountId = self.statusViewModel.reblogStatus?.account.id,
|
|
|
|
let rebloggedAccountUsername = self.statusViewModel.reblogStatus?.account.acct {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: rebloggedAccountId,
|
|
|
|
accountDisplayName: self.statusViewModel.reblogStatus?.account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: rebloggedAccountUsername))
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 11:47:13 +02:00
|
|
|
}
|
2023-04-18 13:45:48 +02:00
|
|
|
}
|
2023-04-15 08:03:16 +02:00
|
|
|
}
|
|
|
|
.onTapGesture {
|
|
|
|
self.navigateToStatus()
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-26 14:49:43 +02:00
|
|
|
.opacity(self.opacity)
|
2023-03-16 20:58:39 +01:00
|
|
|
.onAppear {
|
|
|
|
if let uiImage = state.imageResponse?.image {
|
|
|
|
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
|
|
|
|
}
|
2023-04-26 14:49:43 +02:00
|
|
|
|
|
|
|
if self.imageFromCache == false {
|
|
|
|
self.opacity = 0.0
|
|
|
|
withAnimation {
|
|
|
|
self.opacity = 1.0
|
|
|
|
}
|
|
|
|
}
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-04-15 15:05:05 +02:00
|
|
|
self.imageContainerView(image: image)
|
2023-04-16 09:05:41 +02:00
|
|
|
.imageContextMenu(statusModel: self.statusViewModel,
|
|
|
|
attachmentModel: self.attachment,
|
|
|
|
uiImage: state.imageResponse?.image)
|
2023-04-26 14:49:43 +02:00
|
|
|
.opacity(self.opacity)
|
2023-04-15 15:05:05 +02:00
|
|
|
.onAppear {
|
|
|
|
if let uiImage = state.imageResponse?.image {
|
|
|
|
self.recalculateSizeOfDownloadedImage(uiImage: uiImage)
|
|
|
|
}
|
2023-04-26 14:49:43 +02:00
|
|
|
|
|
|
|
if self.imageFromCache == false {
|
|
|
|
self.opacity = 0.0
|
|
|
|
withAnimation {
|
|
|
|
self.opacity = 1.0
|
|
|
|
}
|
|
|
|
}
|
2023-03-29 09:35:01 +02:00
|
|
|
}
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
|
|
|
} else if state.error != nil {
|
|
|
|
ZStack {
|
2023-10-20 17:35:11 +02:00
|
|
|
BlurredImage(blurhash: attachment.blurhash)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-16 20:58:39 +01:00
|
|
|
VStack(alignment: .center) {
|
|
|
|
Spacer()
|
|
|
|
Text("global.error.errorDuringImageDownload", comment: "Cannot download image")
|
2023-10-20 17:35:11 +02:00
|
|
|
.foregroundColor(.white)
|
2023-03-16 20:58:39 +01:00
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
2023-10-20 17:35:11 +02:00
|
|
|
.onTapGesture {
|
|
|
|
self.navigateToStatus()
|
|
|
|
}
|
2023-03-16 20:58:39 +01:00
|
|
|
} else {
|
|
|
|
VStack(alignment: .center) {
|
|
|
|
BlurredImage(blurhash: attachment.blurhash)
|
2023-03-29 09:35:01 +02:00
|
|
|
.onTapGesture {
|
|
|
|
self.navigateToStatus()
|
|
|
|
}
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.priority(.high)
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-04-15 08:03:16 +02:00
|
|
|
@ViewBuilder
|
|
|
|
private func imageContainerView(image: Image) -> some View {
|
2023-04-15 15:05:05 +02:00
|
|
|
ZStack {
|
|
|
|
self.imageView(image: image)
|
2023-04-15 08:03:16 +02:00
|
|
|
|
2023-04-15 15:05:05 +02:00
|
|
|
if self.showAvatar {
|
|
|
|
ImageAvatar(displayName: self.statusViewModel.account.displayNameWithoutEmojis,
|
2023-09-29 16:03:42 +02:00
|
|
|
avatarUrl: self.statusViewModel.account.avatar,
|
|
|
|
rebloggedAccountDisplayName: self.statusViewModel.reblogStatus?.account.displayNameWithoutEmojis,
|
2023-09-29 16:58:45 +02:00
|
|
|
rebloggedAccountAvatar: self.statusViewModel.reblogStatus?.account.avatar) { isAuthor in
|
|
|
|
if isAuthor {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: self.statusViewModel.account.id,
|
|
|
|
accountDisplayName: self.statusViewModel.account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: self.statusViewModel.account.acct))
|
|
|
|
} else {
|
|
|
|
if let rebloggedAccountId = self.statusViewModel.reblogStatus?.account.id,
|
|
|
|
let rebloggedAccountUsername = self.statusViewModel.reblogStatus?.account.acct {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: rebloggedAccountId,
|
|
|
|
accountDisplayName: self.statusViewModel.reblogStatus?.account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: rebloggedAccountUsername))
|
|
|
|
}
|
|
|
|
}
|
2023-04-18 13:45:48 +02:00
|
|
|
}
|
2023-04-15 15:05:05 +02:00
|
|
|
}
|
2023-04-15 08:03:16 +02:00
|
|
|
|
2023-04-16 17:44:35 +02:00
|
|
|
ImageAlternativeText(text: self.attachment.description) { text in
|
|
|
|
self.routerPath.presentedAlert = .alternativeText(text: text)
|
|
|
|
}
|
|
|
|
|
2023-04-15 15:05:05 +02:00
|
|
|
ImageFavourite(isFavourited: $isFavourited)
|
|
|
|
FavouriteTouch(showFavouriteAnimation: $showThumbImage)
|
|
|
|
}
|
2023-04-15 08:03:16 +02:00
|
|
|
}
|
|
|
|
|
2023-03-16 20:58:39 +01:00
|
|
|
@ViewBuilder
|
|
|
|
private func imageView(image: Image) -> some View {
|
|
|
|
image
|
|
|
|
.resizable()
|
2023-09-26 20:01:58 +02:00
|
|
|
.if(self.clipToRectangle == true) {
|
|
|
|
$0
|
|
|
|
.aspectRatio(contentMode: .fill)
|
|
|
|
.frame(width: self.containerWidth, height: self.containerWidth)
|
|
|
|
.clipped()
|
|
|
|
// Fix issue with clickable content area outside of the visible image: https://developer.apple.com/forums/thread/123717.
|
|
|
|
.contentShape(Rectangle())
|
|
|
|
}
|
|
|
|
.if(self.clipToRectangle == false) {
|
|
|
|
$0.aspectRatio(contentMode: .fit)
|
2023-05-25 17:33:04 +02:00
|
|
|
}
|
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-10-21 09:44:40 +02:00
|
|
|
|
|
|
|
// Mark that user performed specific action.
|
|
|
|
TimelineDoubleTapTip().invalidate(reason: .actionPerformed)
|
2023-04-14 16:50:47 +02:00
|
|
|
|
|
|
|
// Mark favourite booleans used to show star in the timeline view.
|
|
|
|
self.statusViewModel.favourited = true
|
2023-04-15 08:03:16 +02:00
|
|
|
withAnimation(.default.delay(2.0)) {
|
|
|
|
self.isFavourited = true
|
|
|
|
}
|
2023-03-16 20:58:39 +01:00
|
|
|
}
|
|
|
|
.onTapGesture {
|
2023-03-29 09:35:01 +02:00
|
|
|
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
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-29 09:35:01 +02: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-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|