Impressia/Vernissage/Views/StatusView.swift

176 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 11:30:30 +01:00
@State private var messageForStatus: Status?
2023-01-06 18:16:08 +01:00
@State private var showCompose = false
2023-01-10 11:30:30 +01:00
2023-01-05 11:55:20 +01:00
@State private var statusData: StatusData?
2023-01-10 11:30:30 +01:00
@State private var status: Status?
2022-12-29 17:27:15 +01:00
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?
2022-12-29 17:27:15 +01:00
var body: some View {
ScrollView {
2023-01-10 11:30:30 +01:00
if let statusData = self.statusData, let status = self.status {
2023-01-08 14:50:37 +01:00
VStack (alignment: .leading) {
2023-01-08 15:43:55 +01:00
ImagesCarousel(attachments: statusData.attachments(),
exifCamera: $exifCamera,
exifExposure: $exifExposure,
exifCreatedDate: $exifCreatedDate,
exifLens: $exifLens)
2022-12-29 17:27:15 +01:00
2023-01-05 11:55:20 +01:00
VStack(alignment: .leading) {
NavigationLink(destination: UserProfileView(
accountId: statusData.accountId,
accountDisplayName: statusData.accountDisplayName,
accountUserName: statusData.accountUsername)
.environmentObject(applicationState)) {
2023-01-05 21:08:19 +01:00
UsernameRow(accountAvatar: statusData.accountAvatar,
accountDisplayName: statusData.accountDisplayName,
accountUsername: statusData.accountUsername)
2023-01-05 11:55:20 +01:00
}
HTMLFormattedText(statusData.content)
.padding(.leading, -4)
VStack (alignment: .leading) {
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")
Text(statusData.createdAt.toRelative(.isoDateTimeMilliSec))
.padding(.horizontal, -4)
if let applicationName = statusData.applicationName {
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 11:30:30 +01:00
InteractionRow(status: status) {
self.messageForStatus = status
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 11:30:30 +01:00
CommentsSection(statusId: statusData.id) { messageForStatus in
self.messageForStatus = messageForStatus
2023-01-06 18:16:08 +01:00
self.showCompose.toggle()
}
2023-01-05 11:55:20 +01:00
}
} else {
VStack (alignment: .leading) {
if let imageBlurhash, let uiImage = UIImage(blurHash: imageBlurhash, size: CGSize(width: 32, height: 32)) {
Image(uiImage: uiImage)
.resizable()
.frame(width: UIScreen.main.bounds.width, height: self.getImageHeight())
} else {
Rectangle()
.fill(Color.placeholderText)
.frame(width: UIScreen.main.bounds.width, height: self.getImageHeight())
.redacted(reason: .placeholder)
}
2023-01-05 12:33:52 +01:00
VStack(alignment: .leading) {
2023-01-05 21:08:19 +01:00
UsernameRow(accountDisplayName: "Verylong Displayname",
accountUsername: "@username")
2023-01-05 12:33:52 +01:00
Text("Lorem ispum text something")
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor)
2023-01-05 12:33:52 +01:00
.font(.footnote)
Text("Lorem ispum text something sdf sdfsdf sdfdsfsdfsdf")
2023-01-06 13:05:21 +01:00
.foregroundColor(.lightGrayColor)
2023-01-05 12:33:52 +01:00
.font(.footnote)
LabelIcon(iconName: "camera", value: "SONY ILCE-7M3")
LabelIcon(iconName: "camera.aperture", value: "Viltrox 24mm F1.8 E")
LabelIcon(iconName: "timelapse", value: "24.0 mm, f/1.8, 1/640s, ISO 100")
LabelIcon(iconName: "calendar", value: "2 Oct 2022")
}
.padding(8)
.redacted(reason: .placeholder)
.animatePlaceholder(isLoading: .constant(true))
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 11:30:30 +01:00
ComposeView(status: $messageForStatus)
2023-01-06 18:16:08 +01:00
})
2023-01-02 19:12:25 +01:00
.onAppear {
2023-01-04 17:56:01 +01:00
Task {
do {
2023-01-05 11:55:20 +01:00
// Get status from API.
2023-01-10 11:30:30 +01:00
self.status = try await TimelineService.shared.getStatus(withId: self.statusId, and: self.applicationState.accountData)
2023-01-05 11:55:20 +01:00
if let status {
// Get status from database.
let statusDataFromDatabase = StatusDataHandler.shared.getStatusData(statusId: self.statusId)
// If we have status in database then we can update data.
if let statusDataFromDatabase {
self.statusData = try await TimelineService.shared.updateStatus(statusDataFromDatabase, basedOn: status)
} else {
self.statusData = try await status.createStatusData()
}
2023-01-04 17:56:01 +01:00
}
} catch {
print("Error \(error.localizedDescription)")
}
}
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))
}
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
}
}