Impressia/Vernissage/Views/DetailsView.swift

123 lines
5.2 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
import MastodonSwift
2023-01-02 19:12:25 +01:00
import AVFoundation
2022-12-29 17:27:15 +01:00
struct DetailsView: 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 private var statusData: StatusData?
2022-12-29 17:27:15 +01:00
var body: some View {
ScrollView {
2023-01-05 11:55:20 +01:00
if let statusData = self.statusData {
VStack (alignment: .leading) {
ImagesCarousel(attachments: statusData.attachments())
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)) {
UsernameRow(statusData: statusData)
}
HTMLFormattedText(statusData.content)
.padding(.leading, -4)
VStack (alignment: .leading) {
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")
2023-01-03 20:42:20 +01:00
}
2023-01-05 11:55:20 +01:00
.foregroundColor(Color.lightGrayColor)
HStack {
Text("Uploaded")
Text(statusData.createdAt.toRelative(.isoDateTimeMilliSec))
.padding(.horizontal, -4)
if let applicationName = statusData.applicationName {
Text("via \(applicationName)")
}
}
.foregroundColor(Color.lightGrayColor)
.font(.footnote)
InteractionRow(statusData: statusData)
.padding(8)
2022-12-29 17:27:15 +01:00
}
2023-01-05 11:55:20 +01:00
.padding(8)
Rectangle()
.size(width: UIScreen.main.bounds.width, height: 4)
.fill(Color.mainTextColor)
.opacity(0.1)
2023-01-03 14:09:22 +01:00
2023-01-05 11:55:20 +01:00
CommentsSection(statusId: statusData.id)
}
} else {
VStack (alignment: .leading) {
Rectangle()
.fill(Color.placeholderText)
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)
.redacted(reason: .placeholder)
HStack (alignment: .center) {
Circle()
.fill(Color.placeholderText)
.frame(width: 48.0, height: 48.0)
.redacted(reason: .placeholder)
VStack (alignment: .leading) {
Text("Verylong Displayname")
.foregroundColor(Color.mainTextColor)
.redacted(reason: .placeholder)
Text("@username")
.foregroundColor(Color.lightGrayColor)
.font(.footnote)
.redacted(reason: .placeholder)
}
.padding(.leading, 8)
}.padding(8)
2022-12-29 17:27:15 +01:00
}
}
}
2022-12-30 18:20:54 +01:00
.navigationBarTitle("Details")
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.
let status = try await TimelineService.shared.getStatus(withId: self.statusId, and: self.applicationState.accountData)
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
}
}
struct DetailsView_Previews: PreviewProvider {
static var previews: some View {
2023-01-05 11:55:20 +01:00
DetailsView(statusId: "123")
2022-12-29 17:27:15 +01:00
}
}