2023-01-05 11:55:20 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2023-01-10 08:04:25 +01:00
|
|
|
import MastodonKit
|
2023-01-05 11:55:20 +01:00
|
|
|
import NukeUI
|
|
|
|
|
|
|
|
struct ImageRowAsync: 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-31 12:20:49 +01:00
|
|
|
@State public var statusViewModel: StatusModel
|
2023-01-10 07:16:54 +01:00
|
|
|
|
2023-01-23 09:47:04 +01:00
|
|
|
@State private var imageHeight: Double
|
|
|
|
@State private var imageWidth: Double
|
|
|
|
@State private var heightWasPrecalculated: Bool
|
2023-02-02 17:35:41 +01:00
|
|
|
@State private var showThumbImage = false
|
2023-01-05 11:55:20 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
init(statusViewModel: StatusModel) {
|
2023-01-21 18:01:17 +01:00
|
|
|
self.statusViewModel = statusViewModel
|
2023-01-23 09:47:04 +01:00
|
|
|
|
2023-01-26 20:35:24 +01:00
|
|
|
// Calculate size of frame (first from cache, then from metadata).
|
2023-01-23 09:47:04 +01:00
|
|
|
if let firstAttachment = statusViewModel.mediaAttachments.first,
|
2023-01-28 21:09:31 +01:00
|
|
|
let size = ImageSizeService.shared.get(for: firstAttachment.url) {
|
2023-01-26 20:35:24 +01:00
|
|
|
self.imageWidth = size.width
|
|
|
|
self.imageHeight = size.height
|
|
|
|
|
|
|
|
self.heightWasPrecalculated = true
|
|
|
|
} else if let firstAttachment = statusViewModel.mediaAttachments.first,
|
2023-01-23 09:47:04 +01:00
|
|
|
let imgHeight = (firstAttachment.meta as? ImageMetadata)?.original?.height,
|
|
|
|
let imgWidth = (firstAttachment.meta as? ImageMetadata)?.original?.width {
|
|
|
|
|
2023-01-28 21:09:31 +01:00
|
|
|
let size = ImageSizeService.shared.calculate(for: firstAttachment.url, width: imgWidth, height: imgHeight)
|
2023-01-26 20:35:24 +01:00
|
|
|
self.imageWidth = size.width
|
|
|
|
self.imageHeight = size.height
|
|
|
|
|
2023-01-23 09:47:04 +01:00
|
|
|
self.heightWasPrecalculated = true
|
|
|
|
} else {
|
|
|
|
self.imageWidth = UIScreen.main.bounds.width
|
2023-01-26 20:35:24 +01:00
|
|
|
self.imageHeight = UIScreen.main.bounds.width * 0.75
|
2023-01-23 09:47:04 +01:00
|
|
|
heightWasPrecalculated = false
|
|
|
|
}
|
2023-01-21 18:01:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-05 11:55:20 +01:00
|
|
|
var body: some View {
|
2023-01-10 20:38:02 +01:00
|
|
|
if let attachment = statusViewModel.mediaAttachments.first {
|
2023-01-05 11:55:20 +01:00
|
|
|
ZStack {
|
2023-01-09 06:47:34 +01:00
|
|
|
LazyImage(url: attachment.url) { state in
|
|
|
|
if let image = state.image {
|
2023-01-10 20:38:02 +01:00
|
|
|
if self.statusViewModel.sensitive {
|
2023-02-02 17:35:41 +01:00
|
|
|
ZStack {
|
|
|
|
ContentWarning(blurhash: attachment.blurhash, spoilerText: self.statusViewModel.spoilerText) {
|
|
|
|
self.imageView(image: image)
|
|
|
|
}
|
|
|
|
|
|
|
|
if showThumbImage {
|
|
|
|
FavouriteTouch {
|
|
|
|
self.showThumbImage = false
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 14:48:41 +01:00
|
|
|
}
|
|
|
|
} else {
|
2023-02-02 17:35:41 +01:00
|
|
|
ZStack {
|
|
|
|
self.imageView(image: image)
|
|
|
|
|
|
|
|
if showThumbImage {
|
|
|
|
FavouriteTouch {
|
|
|
|
self.showThumbImage = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 14:48:41 +01:00
|
|
|
}
|
2023-01-09 06:47:34 +01:00
|
|
|
} else if state.error != nil {
|
|
|
|
ZStack {
|
|
|
|
Rectangle()
|
|
|
|
.fill(Color.placeholderText)
|
2023-01-09 10:06:21 +01:00
|
|
|
.scaledToFill()
|
2023-01-09 06:47:34 +01:00
|
|
|
|
|
|
|
VStack(alignment: .center) {
|
|
|
|
Spacer()
|
|
|
|
Text("Cannot download image")
|
|
|
|
.foregroundColor(.systemBackground)
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 10:06:21 +01:00
|
|
|
.frame(width: self.imageWidth, height: self.imageHeight)
|
2023-01-09 06:47:34 +01:00
|
|
|
} else {
|
|
|
|
VStack(alignment: .center) {
|
2023-01-09 14:48:41 +01:00
|
|
|
BlurredImage(blurhash: attachment.blurhash)
|
2023-01-09 06:47:34 +01:00
|
|
|
}
|
2023-01-09 10:06:21 +01:00
|
|
|
.frame(width: self.imageWidth, height: self.imageHeight)
|
2023-01-09 06:47:34 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-23 18:20:59 +01:00
|
|
|
.priority(.high)
|
2023-01-09 06:47:34 +01:00
|
|
|
.onSuccess { imageResponse in
|
2023-01-09 08:29:55 +01:00
|
|
|
self.recalculateSizeOfDownloadedImage(imageResponse: imageResponse)
|
2023-01-09 06:47:34 +01:00
|
|
|
}
|
2023-01-05 11:55:20 +01:00
|
|
|
|
2023-01-10 20:38:02 +01:00
|
|
|
if let count = self.statusViewModel.mediaAttachments.count, count > 1 {
|
2023-01-05 11:55:20 +01:00
|
|
|
BottomRight {
|
|
|
|
Text("1 / \(count)")
|
|
|
|
.padding(.horizontal, 6)
|
|
|
|
.padding(.vertical, 3)
|
|
|
|
.font(.caption2)
|
|
|
|
.foregroundColor(.black)
|
|
|
|
.background(.ultraThinMaterial, in: Capsule())
|
|
|
|
}.padding()
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 10:56:20 +01:00
|
|
|
.frame(width: self.imageWidth, height: self.imageHeight)
|
|
|
|
} else {
|
|
|
|
EmptyView()
|
2023-01-05 11:55:20 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-09 06:47:34 +01:00
|
|
|
|
2023-02-02 17:35:41 +01:00
|
|
|
private func imageView(image: NukeUI.Image) -> some View {
|
|
|
|
image
|
|
|
|
.onTapGesture{
|
|
|
|
self.routerPath.navigate(to: .status(
|
|
|
|
id: statusViewModel.id,
|
|
|
|
blurhash: statusViewModel.mediaAttachments.first?.blurhash,
|
|
|
|
highestImageUrl: statusViewModel.mediaAttachments.getHighestImage()?.url,
|
|
|
|
metaImageWidth: statusViewModel.getImageWidth(),
|
|
|
|
metaImageHeight: statusViewModel.getImageHeight()
|
|
|
|
))
|
|
|
|
}
|
|
|
|
.onLongPressGesture(minimumDuration: 0.2) {
|
|
|
|
Task {
|
2023-02-03 15:16:30 +01:00
|
|
|
try? await self.client.statuses?.favourite(statusId: self.statusViewModel.id)
|
2023-02-02 17:35:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.showThumbImage = true
|
|
|
|
HapticService.shared.touch()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-09 08:29:55 +01:00
|
|
|
private func recalculateSizeOfDownloadedImage(imageResponse: ImageResponse) {
|
2023-01-26 15:10:47 +01:00
|
|
|
guard heightWasPrecalculated == false else {
|
|
|
|
return
|
2023-01-09 08:29:55 +01:00
|
|
|
}
|
2023-01-26 20:35:24 +01:00
|
|
|
|
|
|
|
if let attachment = statusViewModel.mediaAttachments.first {
|
2023-01-28 21:09:31 +01:00
|
|
|
let size = ImageSizeService.shared.calculate(for: attachment.url,
|
|
|
|
width: imageResponse.image.size.width,
|
|
|
|
height: imageResponse.image.size.height)
|
2023-01-26 20:35:24 +01:00
|
|
|
self.imageWidth = size.width
|
|
|
|
self.imageHeight = size.height
|
|
|
|
}
|
2023-01-09 06:47:34 +01:00
|
|
|
}
|
2023-01-05 11:55:20 +01:00
|
|
|
}
|