2022-12-29 17:27:15 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
|
|
|
// Copyright © 2022 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-02 19:12:25 +01:00
|
|
|
import AVFoundation
|
2022-12-29 17:27:15 +01:00
|
|
|
|
2023-01-06 13:05:21 +01:00
|
|
|
struct StatusView: View {
|
2023-01-04 17:56:01 +01:00
|
|
|
@EnvironmentObject var applicationState: ApplicationState
|
2023-02-03 15:16:30 +01:00
|
|
|
@EnvironmentObject var client: Client
|
2023-01-23 18:01:27 +01:00
|
|
|
@EnvironmentObject var routerPath: RouterPath
|
|
|
|
|
2023-01-15 12:41:55 +01:00
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
2023-01-05 11:55:20 +01:00
|
|
|
@State var statusId: String
|
2023-01-09 13:05:02 +01:00
|
|
|
@State var imageBlurhash: String?
|
2023-01-27 15:46:09 +01:00
|
|
|
@State var highestImageUrl: URL?
|
2023-01-09 13:05:02 +01:00
|
|
|
@State var imageWidth: Int32?
|
|
|
|
@State var imageHeight: Int32?
|
2023-01-05 11:55:20 +01:00
|
|
|
|
2023-01-13 21:11:30 +01:00
|
|
|
@State private var showImageViewer = false
|
2023-02-01 18:40:28 +01:00
|
|
|
@State private var state: ViewState = .loading
|
2023-01-10 11:30:30 +01:00
|
|
|
|
2023-01-31 12:20:49 +01:00
|
|
|
@State private var statusViewModel: StatusModel?
|
2022-12-29 17:27:15 +01:00
|
|
|
|
2023-01-14 11:57:28 +01:00
|
|
|
@State private var selectedAttachmentId: String?
|
2023-01-08 14:50:37 +01:00
|
|
|
@State private var exifCamera: String?
|
|
|
|
@State private var exifExposure: String?
|
|
|
|
@State private var exifCreatedDate: String?
|
|
|
|
@State private var exifLens: String?
|
2023-02-12 20:55:20 +01:00
|
|
|
|
2022-12-29 17:27:15 +01:00
|
|
|
var body: some View {
|
2023-02-01 18:40:28 +01:00
|
|
|
self.mainBody()
|
|
|
|
.navigationBarTitle("Details")
|
|
|
|
.fullScreenCover(isPresented: $showImageViewer, content: {
|
|
|
|
if let statusViewModel = self.statusViewModel {
|
|
|
|
ImagesViewer(statusViewModel: statusViewModel, selectedAttachmentId: selectedAttachmentId ?? String.empty())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@ViewBuilder
|
|
|
|
private func mainBody() -> some View {
|
|
|
|
switch state {
|
|
|
|
case .loading:
|
|
|
|
StatusPlaceholder(imageHeight: self.getImageHeight(), imageBlurhash: self.imageBlurhash)
|
|
|
|
.task {
|
|
|
|
await self.loadData()
|
|
|
|
}
|
|
|
|
case .loaded:
|
2023-01-10 20:38:02 +01:00
|
|
|
if let statusViewModel = self.statusViewModel {
|
2023-02-01 18:40:28 +01:00
|
|
|
ScrollView {
|
|
|
|
VStack (alignment: .leading) {
|
|
|
|
ImagesCarousel(attachments: statusViewModel.mediaAttachments,
|
|
|
|
selectedAttachmentId: $selectedAttachmentId,
|
|
|
|
exifCamera: $exifCamera,
|
|
|
|
exifExposure: $exifExposure,
|
|
|
|
exifCreatedDate: $exifCreatedDate,
|
|
|
|
exifLens: $exifLens)
|
2023-01-26 15:10:47 +01:00
|
|
|
.onTapGesture {
|
2023-02-01 18:40:28 +01:00
|
|
|
withoutAnimation {
|
|
|
|
self.showImageViewer.toggle()
|
|
|
|
}
|
2023-01-23 18:01:27 +01:00
|
|
|
}
|
2023-01-05 11:55:20 +01:00
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
VStack(alignment: .leading) {
|
|
|
|
self.reblogInformation()
|
2023-01-23 18:01:27 +01:00
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
UsernameRow(accountId: statusViewModel.account.id,
|
|
|
|
accountAvatar: statusViewModel.account.avatar,
|
|
|
|
accountDisplayName: statusViewModel.account.displayNameWithoutEmojis,
|
|
|
|
accountUsername: statusViewModel.account.acct)
|
|
|
|
.onTapGesture {
|
|
|
|
self.routerPath.navigate(to: .userProfile(accountId: statusViewModel.account.id,
|
|
|
|
accountDisplayName: statusViewModel.account.displayNameWithoutEmojis,
|
|
|
|
accountUserName: statusViewModel.account.acct))
|
2023-01-10 21:11:04 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
MarkdownFormattedText(statusViewModel.content.asMarkdown)
|
|
|
|
.environment(\.openURL, OpenURLAction { url in
|
2023-02-03 15:16:30 +01:00
|
|
|
routerPath.handle(url: url)
|
2023-02-01 18:40:28 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
VStack (alignment: .leading) {
|
|
|
|
if let name = statusViewModel.place?.name, let country = statusViewModel.place?.country {
|
|
|
|
LabelIcon(iconName: "mappin.and.ellipse", value: "\(name), \(country)")
|
|
|
|
}
|
|
|
|
|
|
|
|
LabelIcon(iconName: "camera", value: self.exifCamera)
|
|
|
|
LabelIcon(iconName: "camera.aperture", value: self.exifLens)
|
|
|
|
LabelIcon(iconName: "timelapse", value: self.exifExposure)
|
|
|
|
LabelIcon(iconName: "calendar", value: self.exifCreatedDate?.toDate(.isoDateTimeSec)?.formatted())
|
2023-01-05 11:55:20 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.padding(.bottom, 2)
|
|
|
|
.foregroundColor(.lightGrayColor)
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Uploaded")
|
|
|
|
Text(statusViewModel.createdAt.toRelative(.isoDateTimeMilliSec))
|
|
|
|
.padding(.horizontal, -4)
|
|
|
|
if let applicationName = statusViewModel.application?.name {
|
|
|
|
Text("via \(applicationName)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.foregroundColor(.lightGrayColor)
|
|
|
|
.font(.footnote)
|
|
|
|
|
|
|
|
InteractionRow(statusViewModel: statusViewModel)
|
|
|
|
.foregroundColor(.accentColor)
|
|
|
|
.padding(8)
|
2023-01-05 11:55:20 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.padding(8)
|
|
|
|
|
|
|
|
CommentsSection(statusId: statusViewModel.id)
|
2023-01-11 13:16:43 +01:00
|
|
|
}
|
2023-01-04 17:56:01 +01:00
|
|
|
}
|
2023-01-15 12:41:55 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
|
|
|
|
case .error(let error):
|
|
|
|
ErrorView(error: error) {
|
|
|
|
self.state = .loading
|
|
|
|
await self.loadData()
|
2023-01-04 17:56:01 +01:00
|
|
|
}
|
2023-02-01 18:40:28 +01:00
|
|
|
.padding()
|
2023-01-02 19:12:25 +01:00
|
|
|
}
|
2022-12-29 17:27:15 +01:00
|
|
|
}
|
2023-01-08 14:50:37 +01:00
|
|
|
|
2023-01-26 15:10:47 +01:00
|
|
|
@ViewBuilder func reblogInformation() -> some View {
|
|
|
|
if let reblogStatus = self.statusViewModel?.reblogStatus {
|
|
|
|
HStack(alignment: .center, spacing: 4) {
|
|
|
|
UserAvatar(accountAvatar: reblogStatus.account.avatar, size: .mini)
|
|
|
|
Text(reblogStatus.account.displayNameWithoutEmojis)
|
|
|
|
Image(systemName: "paperplane")
|
|
|
|
.padding(.trailing, 8)
|
|
|
|
}
|
|
|
|
.font(.footnote)
|
|
|
|
.foregroundColor(Color.mainTextColor.opacity(0.4))
|
|
|
|
.background(Color.mainTextColor.opacity(0.1))
|
|
|
|
.clipShape(Capsule())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:40:28 +01:00
|
|
|
private func loadData() async {
|
|
|
|
do {
|
|
|
|
// Get status from API.
|
2023-02-03 15:16:30 +01:00
|
|
|
if let status = try await self.client.statuses?.status(withId: self.statusId) {
|
2023-02-12 20:55:20 +01:00
|
|
|
var statusModel = StatusModel(status: status)
|
|
|
|
|
|
|
|
// We have to always open main status (even if the user is redirected from notifications to comment).
|
|
|
|
statusModel = try await self.getMainStatus(status: statusModel)
|
|
|
|
if status.id != statusModel.id {
|
|
|
|
self.highestImageUrl = statusModel.mediaAttachments.getHighestImage()?.url
|
|
|
|
self.imageWidth = statusModel.getImageWidth()
|
|
|
|
self.imageHeight = statusModel.getImageHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
self.statusViewModel = statusModel
|
|
|
|
self.selectedAttachmentId = statusModel.mediaAttachments.first?.id ?? String.empty()
|
2023-02-01 18:40:28 +01:00
|
|
|
|
|
|
|
// If we have status in database then we can update data.
|
|
|
|
if let accountData = self.applicationState.account,
|
|
|
|
let statusDataFromDatabase = StatusDataHandler.shared.getStatusData(accountId: accountData.id, statusId: self.statusId) {
|
|
|
|
_ = try await HomeTimelineService.shared.update(status: statusDataFromDatabase, basedOn: status, for: accountData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.state = .loaded
|
|
|
|
} catch NetworkError.notSuccessResponse(let response) {
|
|
|
|
if response.statusCode() == HTTPStatusCode.notFound, let accountId = self.applicationState.account?.id {
|
|
|
|
StatusDataHandler.shared.remove(accountId: accountId, statusId: self.statusId)
|
|
|
|
ErrorService.shared.handle(NetworkError.notSuccessResponse(response), message: "Status not existing anymore.", showToastr: true)
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
if !Task.isCancelled {
|
|
|
|
ErrorService.shared.handle(error, message: "Error during download status from server.", showToastr: true)
|
|
|
|
self.state = .loaded
|
|
|
|
} else {
|
|
|
|
ErrorService.shared.handle(error, message: "Error during download status from server.", showToastr: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-08 14:50:37 +01:00
|
|
|
private func setAttachment(_ attachmentData: AttachmentData) {
|
|
|
|
exifCamera = attachmentData.exifCamera
|
|
|
|
exifExposure = attachmentData.exifExposure
|
|
|
|
exifCreatedDate = attachmentData.exifCreatedDate
|
|
|
|
exifLens = attachmentData.exifLens
|
|
|
|
}
|
2023-01-09 13:05:02 +01:00
|
|
|
|
|
|
|
private func getImageHeight() -> Double {
|
2023-01-28 21:09:31 +01:00
|
|
|
if let highestImageUrl = self.highestImageUrl, let imageSize = ImageSizeService.shared.get(for: highestImageUrl) {
|
2023-01-27 15:46:09 +01:00
|
|
|
return imageSize.height
|
|
|
|
}
|
|
|
|
|
2023-01-09 13:05:02 +01:00
|
|
|
if let imageHeight = self.imageHeight, let imageWidth = self.imageWidth, imageHeight > 0 && imageWidth > 0 {
|
|
|
|
return self.calculateHeight(width: Double(imageWidth), height: Double(imageHeight))
|
|
|
|
}
|
|
|
|
|
2023-01-14 08:52:51 +01:00
|
|
|
// If we don't have image height and width in metadata, we have to use some constant height.
|
2023-01-09 13:05:02 +01:00
|
|
|
return UIScreen.main.bounds.width * 0.75
|
|
|
|
}
|
|
|
|
|
|
|
|
private func calculateHeight(width: Double, height: Double) -> CGFloat {
|
|
|
|
let divider = width / UIScreen.main.bounds.size.width
|
|
|
|
return height / divider
|
|
|
|
}
|
2023-02-12 20:55:20 +01:00
|
|
|
|
|
|
|
private func getMainStatus(status: StatusModel) async throws -> StatusModel {
|
|
|
|
guard let inReplyToId = status.inReplyToId else {
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let previousStatus = try await self.client.statuses?.status(withId: inReplyToId) else {
|
|
|
|
throw ClientError.cannotRetrieveStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
let previousStatusModel = StatusModel(status: previousStatus)
|
|
|
|
return try await self.getMainStatus(status: previousStatusModel)
|
|
|
|
}
|
2022-12-29 17:27:15 +01:00
|
|
|
}
|
|
|
|
|