Impressia/Vernissage/Views/PhotoEditorView.swift

94 lines
3.3 KiB
Swift
Raw Normal View History

2023-02-17 14:47:59 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
struct PhotoEditorView: View {
2023-02-18 13:43:22 +01:00
@EnvironmentObject var client: Client
2023-02-17 17:10:09 +01:00
@Environment(\.dismiss) private var dismiss
2023-02-17 14:47:59 +01:00
2023-02-17 17:10:09 +01:00
@State private var description: String = String.empty()
@ObservedObject public var photoAttachment: PhotoAttachment
2023-02-17 14:47:59 +01:00
var body: some View {
2023-02-18 14:17:18 +01:00
NavigationView {
VStack(alignment: .leading) {
if let uiImage = UIImage(data: photoAttachment.photoData) {
List {
Section(header: Text("Photo")) {
HStack {
Spacer()
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.clipShape(RoundedRectangle(cornerRadius: 10))
.frame(maxHeight: 300)
Spacer()
}
2023-02-18 11:47:49 +01:00
}
2023-02-18 14:17:18 +01:00
Section(header: Text("Accessibility")) {
TextField("Sescription for the visually impaired", text: $description, axis: .vertical)
.keyboardType(.default)
2023-02-18 21:06:04 +01:00
.lineLimit(3...6)
2023-02-18 14:17:18 +01:00
.multilineTextAlignment(.leading)
}
}.listStyle(.grouped)
2023-02-17 17:10:09 +01:00
2023-02-18 14:17:18 +01:00
Spacer()
}
}
.onDisappear {
self.hideKeyboard()
}
.onAppear {
self.description = self.photoAttachment.uploadedAttachment?.description ?? String.empty()
}
2023-02-21 08:36:14 +01:00
.navigationTitle("Photo details")
.navigationBarTitleDisplayMode(.inline)
2023-02-18 14:17:18 +01:00
.toolbar {
self.getTrailingToolbar()
2023-02-18 11:47:49 +01:00
}
2023-02-17 17:10:09 +01:00
}
}
@ToolbarContentBuilder
private func getTrailingToolbar() -> some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
2023-02-18 14:17:18 +01:00
ActionButton(showLoader: false) {
2023-02-18 13:43:22 +01:00
await self.update()
2023-02-17 17:10:09 +01:00
} label: {
2023-02-19 09:41:35 +01:00
Text("Save")
2023-02-17 17:10:09 +01:00
}.buttonStyle(.borderedProminent)
}
2023-02-18 14:17:18 +01:00
ToolbarItem(placement: .cancellationAction) {
Button("Cancel", role: .cancel) {
dismiss()
}
}
2023-02-17 14:47:59 +01:00
}
2023-02-18 13:43:22 +01:00
private func update() async {
2023-02-19 12:49:44 +01:00
HapticService.shared.fireHaptic(of: .buttonPress)
2023-02-18 13:43:22 +01:00
self.hideKeyboard()
if let uploadedAttachment = self.photoAttachment.uploadedAttachment {
do {
let updated = try await self.client.media?.update(id: uploadedAttachment.id,
description: self.description,
focus: nil)
self.photoAttachment.uploadedAttachment = updated
self.dismiss()
} catch {
2023-02-21 21:37:38 +01:00
ErrorService.shared.handle(error, message: "Cannot update attachment.", showToastr: true)
2023-02-18 13:43:22 +01:00
}
}
}
2023-02-17 14:47:59 +01:00
}
2023-02-17 17:10:09 +01:00