From 815b495aa012e345f6e4fb3e1aeaee7368eb5a85 Mon Sep 17 00:00:00 2001 From: Matte23 Date: Tue, 1 Dec 2020 12:02:31 +0100 Subject: [PATCH] Add view and download attachment buttons --- .../circolapp/View/AttachmentView.swift | 50 +++++++++++++++++-- .../circolapp/View/CircularView.swift | 20 ++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/ios/circolapp/circolapp/View/AttachmentView.swift b/ios/circolapp/circolapp/View/AttachmentView.swift index f749e60..cdde91b 100644 --- a/ios/circolapp/circolapp/View/AttachmentView.swift +++ b/ios/circolapp/circolapp/View/AttachmentView.swift @@ -26,10 +26,52 @@ struct AttachmentView: View { var body: some View { Divider() - Text(attachmentName) - .font(.subheadline) - .multilineTextAlignment(.leading) - + HStack { + Text(attachmentName) + .font(.subheadline) + .multilineTextAlignment(.leading) + Spacer() + + Button(action: { + guard let url = URL(string: attachmentUrl) else { return } + openURL(url) + }) { + Image(systemName: "envelope.open") + .foregroundColor(.blue) + .font(.body) + .padding() + } + .buttonStyle(PlainButtonStyle()) + + Button(action: { + guard let url = URL(string: attachmentUrl) else { return } + + let downloadTask = URLSession.shared.downloadTask(with: url) { + urlOrNil, responseOrNil, errorOrNil in + + guard let fileURL = urlOrNil else { return } + do { + let documentsURL = try + FileManager.default.url(for: .documentDirectory, + in: .userDomainMask, + appropriateFor: nil, + create: false) + let savedURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent) + try FileManager.default.moveItem(at: fileURL, to: savedURL) + } catch { + print ("file error: \(error)") + } + } + + downloadTask.resume() + }) { + Image(systemName: "square.and.arrow.down") + .foregroundColor(.blue) + .font(.body) + .padding() + } + .buttonStyle(PlainButtonStyle()) + } } } diff --git a/ios/circolapp/circolapp/View/CircularView.swift b/ios/circolapp/circolapp/View/CircularView.swift index db5f3da..c38c65f 100644 --- a/ios/circolapp/circolapp/View/CircularView.swift +++ b/ios/circolapp/circolapp/View/CircularView.swift @@ -60,6 +60,26 @@ struct CircularView: View { .buttonStyle(PlainButtonStyle()) Button(action: { + guard let url = URL(string: circular.url) else { return } + + let downloadTask = URLSession.shared.downloadTask(with: url) { + urlOrNil, responseOrNil, errorOrNil in + + guard let fileURL = urlOrNil else { return } + do { + let documentsURL = try + FileManager.default.url(for: .documentDirectory, + in: .userDomainMask, + appropriateFor: nil, + create: false) + let savedURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent) + try FileManager.default.moveItem(at: fileURL, to: savedURL) + } catch { + print ("file error: \(error)") + } + } + + downloadTask.resume() }) { Image(systemName: "square.and.arrow.down") .foregroundColor(.blue)