diff --git a/enzevalos_iphone/SwiftUI/Read/ReadMainView.swift b/enzevalos_iphone/SwiftUI/Read/ReadMainView.swift index 18f403310708ab18dd55e675eb9fd8dc251a7f0d..14516db7e727f5254d8bd73ea572ae6b95178d83 100644 --- a/enzevalos_iphone/SwiftUI/Read/ReadMainView.swift +++ b/enzevalos_iphone/SwiftUI/Read/ReadMainView.swift @@ -66,7 +66,7 @@ struct ReadMainView <M: DisplayMail>: View { tab: ReadPart.Attachments.value, image: Image(systemName: "rectangle.and.paperclip"), description: "Tab.Label.Attachments", - content: AnyView(AttachmentsViewMain(attachments: model.mail.displayAttachments, dlAll: true)) + content: AnyView(AttachmentsViewMain(attachments: model.mail.displayAttachments, downloadAll: true)) ) ] } diff --git a/enzevalos_iphone/SwiftUI/Read/Tabbed Views/AttachmentsViewMain.swift b/enzevalos_iphone/SwiftUI/Read/Tabbed Views/AttachmentsViewMain.swift index 463c912428954c05569ce4a5ef77e49d894b56ee..df7007be328c63b9012127e45afa2ef3d72069d6 100644 --- a/enzevalos_iphone/SwiftUI/Read/Tabbed Views/AttachmentsViewMain.swift +++ b/enzevalos_iphone/SwiftUI/Read/Tabbed Views/AttachmentsViewMain.swift @@ -15,29 +15,40 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. +/// a file that preview the attachments of incoming mails +// currently this view only previews a ProxyAttachment import SwiftUI struct AttachmentsViewMain: View { // TODO: Refactor to model? + + //a list that contains all the attachment files @State var attachments: [DisplayAttachment] // Whether the Download all button was clicked - @State var dlAll = true; + @State var downloadAll = true; var body: some View { VStack{ Spacer() // TODO: Add link section - // check if there even are attachments to be displayed + // default view if there are no attachments to be displayed if attachments.count == 0{ noAttachments } + // otherwise preview the attachments else { attachmentsView } } } + /// default view if there are no attachments to be displayed + var noAttachments: some View { + return Text(NSLocalizedString("ReadView.Attachments.No", comment: "")).font(.footnote).padding(30) + } + + /// a view that previews all the attachment files var attachmentsView: some View { return VStack(alignment: .leading, spacing: 5){ // headline @@ -54,7 +65,7 @@ struct AttachmentsViewMain: View { ForEach(0..<attachments.count) { i in AttPrev( attachment: self.attachments[i], - preload: self.dlAll + shouldBeDownloaded: self.downloadAll ) } } @@ -63,34 +74,37 @@ struct AttachmentsViewMain: View { .padding(.top, 30) .padding(.bottom,10) } - - var noAttachments: some View { - return Text(NSLocalizedString("ReadView.Attachments.No", comment: "")).font(.footnote).padding(30) - } - } struct AttachmentsViewMain_Previews: PreviewProvider { static var previews: some View { + // here we call the view with a single ProxyAttachment() for + // demonstration purposes AttachmentsViewMain(attachments: [ProxyAttachment()]) } } -// MARK: one AttachmentPreview +/// a view that previews one attachment file struct AttPrev:View{ + // the attachment that is going to be previewed var attachment: DisplayAttachment - var preload = false - + // by default false + var shouldBeDownloaded = false + + // whether this attachment is downloaded to the documents directory @State var isDownloaded: Bool = false @State var isFullScreen: Bool = false - func download(){ - func getDocumentsDirectory() -> URL { - let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - return paths[0] - } + /// a func to find the documents directory + func getDocumentsDirectory() -> URL { + let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) + return paths[0] + } + + /// a func that downloads the attachment into the documents directory + func download() { let filename = getDocumentsDirectory().appendingPathComponent(self.attachment.myName) do { @@ -101,19 +115,16 @@ struct AttPrev:View{ self.isDownloaded = true } - //is the file already downloaded? + /// a func that checks if an attachment is downloaded into the documents directory or not + /// updates the 'isDownloaded' property accordingly func getDownloadedState(){ - func getDocumentsDirectory() -> URL { - let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - return paths[0] - } let filename = getDocumentsDirectory().appendingPathComponent(self.attachment.myName) if FileManager.default.fileExists(atPath: filename.path){self.isDownloaded = true} } var body: some View{ - if preload {self.download()} + if shouldBeDownloaded {self.download()} //this QuickLookView has to be defined here so it reloads on every state-change let QLV = QuickLookView(name: self.attachment.myName, data: self.attachment.myData, shallDL: self.isDownloaded) @@ -122,7 +133,7 @@ struct AttPrev:View{ CardV(title: self.attachment.myName, //those are the actions visable under the cklicked preview - actions: self.isDownloaded||self.preload ? + actions: self.isDownloaded||self.shouldBeDownloaded ? [ /* self.ActionView(NSLocalizedString("delete", comment: ""), @@ -181,13 +192,9 @@ struct AttPrev:View{ self.getDownloadedState() //download all functionality - if self.preload {self.isDownloaded=true; self.download()} + if self.shouldBeDownloaded {self.isDownloaded=true; self.download()} }) .onDisappear(perform: { - func getDocumentsDirectory() -> URL { - let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) - return paths[0] - } let filename = getDocumentsDirectory().appendingPathComponent(self.attachment.myName) do {