Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Attachment title will be used to populate Draft Subject #1227

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions Mail/Views/New Message/Attachments/Attachable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,32 @@ extension NSItemProvider: Attachable {
return UTType(preferredIdentifier)
}

public func writeToTemporaryURL() async throws -> URL {
public func writeToTemporaryURL() async throws -> (url: URL, title: String?) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe a marginal improvement using a typealias for this (url: URL, title: String?)

switch underlyingType {
case .isURL:
let getPlist = try ItemProviderURLRepresentation(from: self)
return try await getPlist.result.get().url
let result = try await getPlist.result.get()
return (result.url, result.title)

case .isText:
let getText = try ItemProviderTextRepresentation(from: self)
return try await getText.result.get()
let resultURL = try await getText.result.get()
return (resultURL, nil)

case .isUIImage:
let getUIImage = try ItemProviderUIImageRepresentation(from: self)
return try await getUIImage.result.get()
let resultURL = try await getUIImage.result.get()
return (resultURL, nil)

case .isImageData, .isCompressedData, .isMiscellaneous:
let getFile = try ItemProviderFileRepresentation(from: self)
return try await getFile.result.get().url
let result = try await getFile.result.get()
return (result.url, result.title)

case .isDirectory:
let getFile = try ItemProviderZipRepresentation(from: self)
return try await getFile.result.get().url
let result = try await getFile.result.get()
return (result.url, result.title)

case .none:
throw ErrorDomain.UTINotFound
Expand All @@ -75,7 +80,7 @@ extension PHPickerResult: Attachable {
return itemProvider.type
}

public func writeToTemporaryURL() async throws -> URL {
public func writeToTemporaryURL() async throws -> (url: URL, title: String?) {
return try await itemProvider.writeToTemporaryURL()
}
}
Expand All @@ -89,8 +94,8 @@ extension URL: Attachable {
return UTType.data
}

public func writeToTemporaryURL() async throws -> URL {
return self
public func writeToTemporaryURL() async throws -> (url: URL, title: String?) {
return (self, nil)
}
}

Expand All @@ -103,11 +108,11 @@ extension Data: Attachable {
return UTType.image
}

public func writeToTemporaryURL() async throws -> URL {
public func writeToTemporaryURL() async throws -> (url: URL, title: String?) {
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let temporaryFileURL = temporaryURL.appendingPathComponent("attachment").appendingPathExtension("jpeg")
try FileManager.default.createDirectory(at: temporaryURL, withIntermediateDirectories: true)
try write(to: temporaryFileURL)
return temporaryFileURL
return (temporaryFileURL, nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import UniformTypeIdentifiers
public protocol Attachable {
var suggestedName: String? { get }
var type: UTType? { get }
func writeToTemporaryURL() async throws -> URL
func writeToTemporaryURL() async throws -> (url: URL, title: String?)
}

/// Abstracts that some attachment was updated
Expand Down Expand Up @@ -88,6 +88,25 @@ public final class AttachmentsManagerWorker {
liveAttachments.map { $0.detached() }
}

var attachmentsTitles: [String?]? {
Copy link
Contributor Author

@adrien-coye adrien-coye Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe some doc about the DB modified on side effect

didSet {
let realm = backgroundRealm.getRealm()
guard let draft = realm.object(ofType: Draft.self, forPrimaryKey: draftLocalUUID),
!draft.isInvalidated,
draft.subject.isEmpty else {
return
}

guard let attachmentTitle = attachmentsTitles?.compactMap({ $0 }).first else {
return
}

try? realm.write {
draft.subject = attachmentTitle
}
}
}

public init(backgroundRealm: BackgroundRealm, draftLocalUUID: String, mailboxManager: MailboxManager) {
self.backgroundRealm = backgroundRealm
self.draftLocalUUID = draftLocalUUID
Expand Down Expand Up @@ -165,7 +184,6 @@ public final class AttachmentsManagerWorker {
await updateDelegate?.contentWillChange()
}

@discardableResult
func importAttachment(attachment: Attachable, disposition: AttachmentDisposition) async -> String? {
guard let localAttachment = await createLocalAttachment(name: attachment.suggestedName ?? getDefaultFileName(),
type: attachment.type,
Expand All @@ -175,27 +193,26 @@ public final class AttachmentsManagerWorker {

let importTask = Task { () -> String? in
do {
let url = try await attachment.writeToTemporaryURL()
let updatedAttachment = await updateLocalAttachment(url: url, attachment: localAttachment)
let attachmentResult = try await attachment.writeToTemporaryURL()
let attachmentURL = attachmentResult.url
let attachmentTitle = attachmentResult.title

let updatedAttachment = await updateLocalAttachment(url: attachmentURL, attachment: localAttachment)
let totalSize = liveAttachments.map(\.size).reduce(0) { $0 + $1 }
guard totalSize < Constants.maxAttachmentsSize else {
await updateDelegate?.handleGlobalError(MailError.attachmentsSizeLimitReached)
await removeAttachment(updatedAttachment.uuid)
return nil
}

let remoteAttachment = try await sendAttachment(url: url, localAttachment: updatedAttachment)
try await sendAttachment(url: attachmentURL, localAttachment: updatedAttachment)
return attachmentTitle

if disposition == .inline,
Copy link
Contributor Author

@adrien-coye adrien-coye Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dead code, so removed it. I now return the title instead

let cid = remoteAttachment?.contentId {
return cid
}
} catch {
DDLogError("Error while creating attachment: \(error.localizedDescription)")
await updateAttachmentUploadError(localAttachment, error: error)
return nil
}

return nil
}

if let uploadTask = attachmentUploadTasks[localAttachment.uuid] {
Expand All @@ -220,6 +237,7 @@ public final class AttachmentsManagerWorker {
return savedAttachment
}

@discardableResult
func sendAttachment(url: URL, localAttachment: Attachment) async throws -> Attachment? {
let data = try Data(contentsOf: url)
let remoteAttachment = try await mailboxManager.apiFetcher.createAttachment(
Expand Down Expand Up @@ -301,18 +319,23 @@ extension AttachmentsManagerWorker: AttachmentsManagerWorkable {
self.updateDelegate = updateDelegate
}

public func importAttachments(attachments: [Attachable], draft: Draft, disposition: AttachmentDisposition) async {
public func importAttachments(attachments: [Attachable],
draft: Draft,
disposition: AttachmentDisposition) async {
guard !attachments.isEmpty else {
return
}

// Cap max number of attachments, API errors out at 100
let attachmentsSlice = attachments[safe: 0 ..< draft.availableAttachmentsSlots]

await attachmentsSlice.concurrentForEach { attachment in
await self.importAttachment(attachment: attachment, disposition: disposition)
let titles: [String?] = await attachmentsSlice.concurrentMap { attachment in
let title = await self.importAttachment(attachment: attachment, disposition: disposition)
// TODO: - Manage inline attachment
return title
}

attachmentsTitles = titles
}

public func removeAttachment(_ attachmentUUID: String) async {
Expand Down
Loading