Impressia/Vernissage/Views/StatusView.swift

173 lines
7.8 KiB
Swift
Raw Normal View History

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-01-05 11:55:20 +01:00
@State var statusId: String
@State var imageBlurhash: String?
@State var imageWidth: Int32?
@State var imageHeight: Int32?
2023-01-05 11:55:20 +01:00
2023-01-10 20:38:02 +01:00
@State private var messageForStatus: StatusViewModel?
2023-01-06 18:16:08 +01:00
@State private var showCompose = false
2023-01-13 21:11:30 +01:00
@State private var showImageViewer = false
@State private var firstLoadFinished = false
2023-01-10 11:30:30 +01:00
2023-01-10 20:38:02 +01:00
@State private var statusViewModel: StatusViewModel?
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-01-13 21:11:30 +01:00
2022-12-29 17:27:15 +01:00
var body: some View {
ScrollView {
2023-01-10 20:38:02 +01:00
if let statusViewModel = self.statusViewModel {
2023-01-08 14:50:37 +01:00
VStack (alignment: .leading) {
2023-01-10 20:38:02 +01:00
ImagesCarousel(attachments: statusViewModel.mediaAttachments,
2023-01-14 11:57:28 +01:00
selectedAttachmentId: $selectedAttachmentId,
2023-01-08 15:43:55 +01:00
exifCamera: $exifCamera,
exifExposure: $exifExposure,
exifCreatedDate: $exifCreatedDate,
exifLens: $exifLens)
2023-01-13 21:11:30 +01:00
.onTapGesture {
2023-01-14 20:45:49 +01:00
// withoutAnimation {
2023-01-13 21:11:30 +01:00
self.showImageViewer.toggle()
2023-01-14 20:45:49 +01:00
// }
2023-01-13 21:11:30 +01:00
}
2022-12-29 17:27:15 +01:00
2023-01-05 11:55:20 +01:00
VStack(alignment: .leading) {
NavigationLink(destination: UserProfileView(
2023-01-10 20:38:02 +01:00
accountId: statusViewModel.account.id,
accountDisplayName: statusViewModel.account.displayName,
accountUserName: statusViewModel.account.username)
2023-01-05 11:55:20 +01:00
.environmentObject(applicationState)) {
2023-01-10 20:38:02 +01:00
UsernameRow(accountAvatar: statusViewModel.account.avatar,
accountDisplayName: statusViewModel.account.displayName,
accountUsername: statusViewModel.account.username)
2023-01-05 11:55:20 +01:00
}
2023-01-10 20:38:02 +01:00
HTMLFormattedText(statusViewModel.content)
2023-01-05 11:55:20 +01:00
.padding(.leading, -4)
VStack (alignment: .leading) {
2023-01-10 21:11:04 +01:00
if let name = statusViewModel.place?.name, let country = statusViewModel.place?.country {
LabelIcon(iconName: "mappin.and.ellipse", value: "\(name), \(country)")
}
2023-01-08 14:50:37 +01:00
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-03 20:42:20 +01:00
}
2023-01-05 21:08:19 +01:00
.padding(.bottom, 2)
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor)
2023-01-05 11:55:20 +01:00
HStack {
Text("Uploaded")
2023-01-10 20:38:02 +01:00
Text(statusViewModel.createdAt.toRelative(.isoDateTimeMilliSec))
2023-01-05 11:55:20 +01:00
.padding(.horizontal, -4)
2023-01-10 20:38:02 +01:00
if let applicationName = statusViewModel.application?.name {
2023-01-05 11:55:20 +01:00
Text("via \(applicationName)")
}
}
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor)
2023-01-05 11:55:20 +01:00
.font(.footnote)
2023-01-10 20:38:02 +01:00
InteractionRow(statusViewModel: statusViewModel) {
self.messageForStatus = statusViewModel
2023-01-06 18:16:08 +01:00
self.showCompose.toggle()
}
2023-01-08 11:32:39 +01:00
.foregroundColor(.accentColor)
2023-01-06 18:16:08 +01:00
.padding(8)
2022-12-29 17:27:15 +01:00
}
2023-01-05 11:55:20 +01:00
.padding(8)
2023-01-05 21:08:19 +01:00
2023-01-10 20:38:02 +01:00
CommentsSection(statusId: statusViewModel.id) { messageForStatus in
2023-01-10 11:30:30 +01:00
self.messageForStatus = messageForStatus
2023-01-06 18:16:08 +01:00
self.showCompose.toggle()
}
2023-01-05 11:55:20 +01:00
}
} else {
2023-01-11 13:16:43 +01:00
StatusPlaceholder(imageHeight: self.getImageHeight(), imageBlurhash: self.imageBlurhash)
2022-12-29 17:27:15 +01:00
}
}
2022-12-30 18:20:54 +01:00
.navigationBarTitle("Details")
2023-01-06 18:16:08 +01:00
.sheet(isPresented: $showCompose, content: {
2023-01-10 20:38:02 +01:00
ComposeView(statusViewModel: $messageForStatus)
2023-01-06 18:16:08 +01:00
})
2023-01-13 21:11:30 +01:00
.fullScreenCover(isPresented: $showImageViewer, content: {
if let statusViewModel = self.statusViewModel {
2023-01-14 11:57:28 +01:00
ImagesViewer(statusViewModel: statusViewModel, selectedAttachmentId: selectedAttachmentId ?? String.empty())
2023-01-13 21:11:30 +01:00
}
})
2023-01-11 13:16:43 +01:00
.task {
do {
guard firstLoadFinished == false else {
return
}
2023-01-11 13:16:43 +01:00
// Get status from API.
if let status = try await TimelineService.shared.getStatus(withId: self.statusId, and: self.applicationState.accountData) {
let statusViewModel = StatusViewModel(status: status)
// Download images and recalculate exif data.
let allImages = await TimelineService.shared.fetchAllImages(statuses: [status])
for attachment in statusViewModel.mediaAttachments {
if let data = allImages[attachment.id] {
attachment.set(data: data)
2023-01-05 11:55:20 +01:00
}
2023-01-04 17:56:01 +01:00
}
2023-01-11 13:16:43 +01:00
self.statusViewModel = statusViewModel
2023-01-14 11:57:28 +01:00
self.selectedAttachmentId = statusViewModel.mediaAttachments.first?.id ?? String.empty()
self.firstLoadFinished = true
2023-01-11 13:16:43 +01:00
// If we have status in database then we can update data.
if let accountData = self.applicationState.accountData,
let statusDataFromDatabase = StatusDataHandler.shared.getStatusData(accountId: accountData.id, statusId: self.statusId) {
_ = try await TimelineService.shared.updateStatus(statusDataFromDatabase, accountData: accountData, basedOn: status)
}
2023-01-04 17:56:01 +01:00
}
2023-01-11 13:16:43 +01:00
} catch {
print("Error \(error.localizedDescription)")
2023-01-04 17:56:01 +01:00
}
2023-01-02 19:12:25 +01:00
}
2022-12-29 17:27:15 +01:00
}
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
}
private func getImageHeight() -> Double {
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.
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
}
2022-12-29 17:27:15 +01:00
}
2023-01-06 13:05:21 +01:00
struct StatusView_Previews: PreviewProvider {
2022-12-29 17:27:15 +01:00
static var previews: some View {
2023-01-06 13:05:21 +01:00
StatusView(statusId: "123")
2022-12-29 17:27:15 +01:00
}
}