Skip to content

Commit

Permalink
Merge pull request #789 from Infomaniak/download-attachments
Browse files Browse the repository at this point in the history
feat: Download all attachments
  • Loading branch information
valentinperignon committed Jun 9, 2023
2 parents ef4fcc2 + 34fdea5 commit 0727a9c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18 deletions.
6 changes: 3 additions & 3 deletions Mail/Components/MailButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ struct MailButton: View {
}
}
.opacity(loading ? 0 : 1)
if loading {
LoadingButtonProgressView(style: style)
}

LoadingButtonProgressView(style: style)
.opacity(loading ? 1 : 0)
}
.frame(maxWidth: fullWidth ? UIConstants.componentsMaxWidth : nil)
}
Expand Down
26 changes: 20 additions & 6 deletions Mail/Helpers/DocumentPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,29 @@ import UIKit
import UniformTypeIdentifiers

struct DocumentPicker: UIViewControllerRepresentable {
var completion: ([URL]) -> Void
enum PickerType {
case selectContent([UTType], ([URL]) -> Void)
case exportContent([URL])
}

@Environment(\.dismiss) private var dismiss

let pickerType: PickerType

func makeCoordinator() -> DocumentPicker.Coordinator {
return DocumentPicker.Coordinator(parent: self)
}

func makeUIViewController(context: UIViewControllerRepresentableContext<DocumentPicker>) -> UIDocumentPickerViewController {
let supportedTypes: [UTType] = [UTType.item]
let picker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
picker.allowsMultipleSelection = true
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
let picker: UIDocumentPickerViewController
switch pickerType {
case let .selectContent(types, _):
picker = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: true)
picker.allowsMultipleSelection = true
case let .exportContent(urls):
picker = UIDocumentPickerViewController(forExporting: urls, asCopy: true)
}

picker.delegate = context.coordinator
return picker
}
Expand All @@ -51,7 +63,9 @@ struct DocumentPicker: UIViewControllerRepresentable {
}

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
parent.completion(urls)
if case let .selectContent(_, completion) = parent.pickerType {
completion(urls)
}
parent.dismiss()
}
}
Expand Down
6 changes: 4 additions & 2 deletions Mail/Views/New Message/ComposeMessageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,16 @@ struct ComposeMessageView: View {
.ignoresSafeArea()
}
.sheet(isPresented: $isShowingFileSelection) {
DocumentPicker { urls in
DocumentPicker(pickerType: .selectContent([.item]) { urls in
attachmentsManager.importAttachments(attachments: urls)
}
})
.ignoresSafeArea()
}
.sheet(isPresented: $isShowingPhotoLibrary) {
ImagePicker { results in
attachmentsManager.importAttachments(attachments: results)
}
.ignoresSafeArea()
}
.customAlert(isPresented: $alert.isShowing) {
switch alert.state {
Expand Down
27 changes: 24 additions & 3 deletions Mail/Views/Thread/AttachmentsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ import MailResources
import RealmSwift
import SwiftUI

struct AllAttachmentsURL: Identifiable {
var id: String { url.absoluteString }
let url: URL
}

struct AttachmentsView: View {
@State private var showDocumentPicker = false

@State private var previewedAttachment: Attachment?
@State private var downloadInProgress = false
@State private var allAttachmentsURL: AllAttachmentsURL?

@EnvironmentObject var mailboxManager: MailboxManager
@ObservedRealmObject var message: Message

Expand Down Expand Up @@ -71,11 +81,18 @@ struct AttachmentsView: View {
.textStyle(.bodySmallSecondary)

MailButton(label: MailResourcesStrings.Localizable.buttonDownloadAll) {
// TODO: Download all attachments
matomo.track(eventWithCategory: .message, name: "downloadAll")
showWorkInProgressSnackBar()
Task {
await tryOrDisplayError {
matomo.track(eventWithCategory: .message, name: "downloadAll")
downloadInProgress = true
let attachmentURL = try await mailboxManager.apiFetcher.downloadAttachments(message: message)
allAttachmentsURL = AllAttachmentsURL(url: attachmentURL)
downloadInProgress = false
}
}
}
.mailButtonStyle(.smallLink)
.mailButtonLoading(downloadInProgress)

Spacer()
}
Expand All @@ -84,6 +101,10 @@ struct AttachmentsView: View {
.sheet(item: $previewedAttachment) { previewedAttachment in
AttachmentPreview(attachment: previewedAttachment)
}
.sheet(item: $allAttachmentsURL) { allAttachmentsURL in
DocumentPicker(pickerType: .exportContent([allAttachmentsURL.url]))
.ignoresSafeArea()
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions MailCore/API/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ public extension Endpoint {
return .mailbox(uuid: uuid).appending(path: "/message/unstar")
}

static func downloadAttachments(messageResource: String) -> Endpoint {
return .resource(messageResource).appending(path: "/attachmentsArchive")
}

static func blockSender(messageResource: String) -> Endpoint {
return .resource(messageResource).appending(path: "/blacklist")
}
Expand Down
20 changes: 16 additions & 4 deletions MailCore/API/MailApiFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public class MailApiFetcher: ApiFetcher {
) async throws -> (data: T, responseAt: Int?) {
do {
return try await super.perform(request: request)
} catch InfomaniakError.apiError(let apiError) {
} catch let InfomaniakError.apiError(apiError) {
throw MailApiError.mailApiErrorWithFallback(apiErrorCode: apiError.code)
} catch InfomaniakError.serverError(statusCode: let statusCode) {
} catch let InfomaniakError.serverError(statusCode: statusCode) {
throw MailServerError(httpStatus: statusCode)
} catch {
if let afError = error.asAFError {
if case .responseSerializationFailed(let reason) = afError,
case .decodingFailed(let error) = reason {
if case let .responseSerializationFailed(reason) = afError,
case let .decodingFailed(error) = reason {
var rawJson = "No data"
if let data = request.data,
let stringData = String(data: data, encoding: .utf8) {
Expand Down Expand Up @@ -293,6 +293,18 @@ public class MailApiFetcher: ApiFetcher {
parameters: ["uids": messages.map(\.uid)])).data
}

public func downloadAttachments(message: Message) async throws -> URL {
let destination = DownloadRequest.suggestedDownloadDestination(options: [
.createIntermediateDirectories,
.removePreviousFile
])
let download = authenticatedSession.download(
Endpoint.downloadAttachments(messageResource: message.resource).url,
to: destination
)
return try await download.serializingDownloadedFileURL().value
}

public func blockSender(message: Message) async throws -> Bool {
try await perform(request: authenticatedRequest(.blockSender(messageResource: message.resource), method: .post)).data
}
Expand Down

0 comments on commit 0727a9c

Please sign in to comment.