diff --git a/enzevalos_iphone/SwiftUI/Compose/AddAttachmentsView.swift b/enzevalos_iphone/SwiftUI/Compose/AddAttachmentsView.swift index f57c47be613b59d4d9de675bad059e321fd03fb3..9530120a226737b7f973fac6cf6fc28d01879cf6 100644 --- a/enzevalos_iphone/SwiftUI/Compose/AddAttachmentsView.swift +++ b/enzevalos_iphone/SwiftUI/Compose/AddAttachmentsView.swift @@ -21,15 +21,10 @@ struct AddAttachmentsView: View { // properties to handle several sheet views (only one .sheet(...) is allowed) @State var showSheet = false - @State var sheetState = SheetStates.none { - willSet { - showSheet = newValue != .none - } - } + @State var currentSheetState: SheetStates? /// SheetStates to controll several sheet views enum SheetStates { - case none // default case imageImport // user presses imageUploadButton case fullScreenFilePreview // user taps on a file preview } @@ -37,10 +32,12 @@ struct AddAttachmentsView: View { /// a func that returns the right sheet content according to the sheetState @ViewBuilder private func sheetContent() -> some View { - switch sheetState { - case .imageImport: // open the gallery and let user pick an image + switch currentSheetState { + case .imageImport: + // open the gallery and let user pick an image ImagePicker(image: self.$inputImage) - case .fullScreenFilePreview: // open the attachment in 'fullScreenAttachment' fullscreen + case .fullScreenFilePreview: + // open the attachment in 'fullScreenAttachment' fullscreen if let attachment = fullScreenAttachment { // file name Text(attachment.myName) @@ -58,14 +55,15 @@ struct AddAttachmentsView: View { /// a func that returns the right onDismiss parameter according to the sheetState private func sheetOnDismiss() { - switch sheetState { - case .imageImport: // TODO: currently no inputImage is imported even if user chose one + switch currentSheetState { + case .imageImport: + // TODO: currently no inputImage is imported even if user chose one guard let inputImage = inputImage else { return } image = Image(uiImage: inputImage) - self.sheetState = .none + self.currentSheetState = nil case .fullScreenFilePreview: self.showSheet = false - self.sheetState = .none + self.currentSheetState = nil self.fullScreenAttachment = nil default: return } @@ -119,13 +117,13 @@ struct AddAttachmentsView: View { // Error while loading file print("Error while importing file.") } - } // end of fileImport + } } /// a view that contains the upload button for pictures var imageUploadButton: some View { Button { - self.sheetState = .imageImport + self.currentSheetState = .imageImport showSheet.toggle() } label: { // try to imitate SF Symbol "doc.badge.plus" @@ -148,11 +146,11 @@ struct AddAttachmentsView: View { /// a view that contains several file previews together with their delete buttons var filePreviews: some View { - ForEach(model.attachments, id: \.self.myID) { currentFile in + ForEach(model.attachments.reversed(), id: \.self.myID) { currentFile in ZStack { // file preview using Quicklook - // shallDL has to be true here, because QuickLookView then downloads - // the file into the Documents Directory + // shallDL has to be true here, because QuickLookView will download + // the file into the Documents Directory for us // it then uses that DD file to create a preview of the content QuickLookView(name: currentFile.myName, data: currentFile.myData, shallDL: true) .padding(3) @@ -166,6 +164,7 @@ struct AddAttachmentsView: View { // delete button in upper right corner Button { // remove from attachments list and remove from Documents Directory + // to keep DD clean if let deleteIndex = model.attachments.firstIndex(of: currentFile) { model.attachments[deleteIndex].removeFileFromDocumentsDirectory() model.attachments.remove(at: deleteIndex)