-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Abstract handling of NSItemProvider
feat: Full Integration testing of ItemProviderRepresentation
- Loading branch information
1 parent
3e858f4
commit 593a5fe
Showing
13 changed files
with
993 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
121 changes: 121 additions & 0 deletions
121
Sources/InfomaniakCore/ItemProviderRepresentation/ItemProviderFileRepresentation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Infomaniak Mail - iOS App | ||
Copyright (C) 2022 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#if canImport(MobileCoreServices) | ||
|
||
import Combine | ||
import Foundation | ||
import InfomaniakDI | ||
|
||
/// Something that can provide a `Progress` and an async `Result` in order to load an url from a `NSItemProvider` | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public final class ItemProviderFileRepresentation: NSObject, ProgressResultable { | ||
public enum ErrorDomain: Error, Equatable{ | ||
case UTINotFound | ||
case UnableToLoadFile | ||
} | ||
|
||
public typealias Success = URL | ||
public typealias Failure = Error | ||
|
||
/// Track task progress with internal Combine pipe | ||
private let resultProcessed = PassthroughSubject<Success, Failure>() | ||
|
||
/// Internal observation of the Combine progress Pipe | ||
private var resultProcessedObserver: AnyCancellable? | ||
|
||
/// Internal Task that wraps the combine result observation | ||
private var computeResultTask: Task<Success, Failure>? | ||
|
||
private let fileManager = FileManager.default | ||
|
||
public init(from itemProvider: NSItemProvider) throws { | ||
guard let typeIdentifier = itemProvider.registeredTypeIdentifiers.first else { | ||
throw ErrorDomain.UTINotFound | ||
} | ||
|
||
// Keep compiler happy | ||
progress = Progress(totalUnitCount: 1) | ||
|
||
super.init() | ||
|
||
// Set progress and hook completion closure to a combine pipe | ||
progress = itemProvider.loadFileRepresentation(forTypeIdentifier: typeIdentifier) { fileProviderURL, error in | ||
guard let fileProviderURL, error == nil else { | ||
self.resultProcessed.send(completion: .failure(error ?? ErrorDomain.UnableToLoadFile)) | ||
return | ||
} | ||
|
||
do { | ||
let UTI = UTI(rawValue: typeIdentifier as CFString) | ||
@InjectService var pathProvider: AppGroupPathProvidable | ||
let temporaryURL = pathProvider.tmpDirectoryURL | ||
.appendingPathComponent(UUID().uuidString, isDirectory: true) | ||
try self.fileManager.createDirectory(at: temporaryURL, withIntermediateDirectories: true) | ||
|
||
let fileName = fileProviderURL.appendingPathExtension(for: UTI).lastPathComponent | ||
let temporaryFileURL = temporaryURL.appendingPathComponent(fileName) | ||
try self.fileManager.copyItem(atPath: fileProviderURL.path, toPath: temporaryFileURL.path) | ||
self.resultProcessed.send(temporaryFileURL) | ||
self.resultProcessed.send(completion: .finished) | ||
} catch { | ||
self.resultProcessed.send(completion: .failure(error)) | ||
} | ||
} | ||
|
||
/// Wrap the Combine pipe to a native Swift Async Task for convenience | ||
computeResultTask = Task { | ||
do { | ||
let result: URL = try await withCheckedThrowingContinuation { continuation in | ||
self.resultProcessedObserver = resultProcessed.sink { result in | ||
switch result { | ||
case .finished: | ||
break | ||
case .failure(let error): | ||
continuation.resume(throwing: error) | ||
} | ||
self.resultProcessedObserver?.cancel() | ||
} receiveValue: { value in | ||
continuation.resume(with: .success(value)) | ||
} | ||
} | ||
|
||
return result | ||
|
||
} catch { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
// MARK: Public | ||
|
||
public var progress: Progress | ||
|
||
public var result: Result<URL, Error> { | ||
get async { | ||
guard let computeResultTask else { | ||
fatalError("This never should be nil") | ||
} | ||
|
||
return await computeResultTask.result | ||
} | ||
} | ||
} | ||
|
||
#endif |
156 changes: 156 additions & 0 deletions
156
Sources/InfomaniakCore/ItemProviderRepresentation/ItemProviderTextRepresentation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
Infomaniak Mail - iOS App | ||
Copyright (C) 2022 Infomaniak Network SA | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#if canImport(MobileCoreServices) | ||
|
||
import Combine | ||
import Foundation | ||
import InfomaniakDI | ||
|
||
/// Something that can provide a `Progress` and an async `Result` in order to make a raw text file from a `NSItemProvider` | ||
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) | ||
public final class ItemProviderTextRepresentation: NSObject, ProgressResultable { | ||
public enum ErrorDomain: Error, Equatable { | ||
case UTINotFound | ||
case UTINotSupported | ||
case unknown | ||
} | ||
|
||
public typealias Success = URL | ||
public typealias Failure = Error | ||
|
||
private static let progressStep: Int64 = 1 | ||
|
||
/// Track task progress with internal Combine pipe | ||
private let resultProcessed = PassthroughSubject<Success, Failure>() | ||
|
||
/// Internal observation of the Combine progress Pipe | ||
private var resultProcessedObserver: AnyCancellable? | ||
|
||
/// Internal Task that wraps the combine result observation | ||
private var computeResultTask: Task<Success, Failure>? | ||
|
||
private let fileManager = FileManager.default | ||
|
||
public init(from itemProvider: NSItemProvider) throws { | ||
guard let typeIdentifier = itemProvider.registeredTypeIdentifiers.first else { | ||
throw ErrorDomain.UTINotFound | ||
} | ||
|
||
// Keep compiler happy | ||
progress = Progress(totalUnitCount: 1) | ||
|
||
super.init() | ||
|
||
let childProgress = Progress() | ||
progress.addChild(childProgress, withPendingUnitCount: Self.progressStep) | ||
|
||
itemProvider.loadItem(forTypeIdentifier: typeIdentifier) { coding, error in | ||
defer { | ||
childProgress.completedUnitCount += Self.progressStep | ||
} | ||
|
||
guard error == nil, coding != nil else { | ||
self.resultProcessed.send(completion: .failure(error ?? ErrorDomain.unknown)) | ||
return | ||
} | ||
|
||
do { | ||
// Build dedicated storage path | ||
@InjectService var pathProvider: AppGroupPathProvidable | ||
let temporaryURL = pathProvider.tmpDirectoryURL | ||
.appendingPathComponent(UUID().uuidString, isDirectory: true) | ||
try self.fileManager.createDirectory(at: temporaryURL, withIntermediateDirectories: true) | ||
|
||
// Is String | ||
if let text = coding as? String { | ||
let targetURL = temporaryURL.appendingPathComponent("\(UUID().uuidString).txt") | ||
|
||
try text.write(to: targetURL, atomically: true, encoding: .utf8) | ||
self.resultProcessed.send(targetURL) | ||
self.resultProcessed.send(completion: .finished) | ||
} | ||
|
||
// Is Data | ||
else if let data = coding as? Data { | ||
guard let uti = UTI(typeIdentifier) else { | ||
self.resultProcessed.send(completion: .failure(ErrorDomain.UTINotFound)) | ||
return | ||
} | ||
|
||
let targetURL = temporaryURL | ||
.appendingPathComponent(UUID().uuidString) | ||
.appendingPathExtension(for: uti) | ||
|
||
try data.write(to: targetURL) | ||
self.resultProcessed.send(targetURL) | ||
self.resultProcessed.send(completion: .finished) | ||
} | ||
|
||
// Not supported | ||
else { | ||
self.resultProcessed.send(completion: .failure(ErrorDomain.UTINotSupported)) | ||
} | ||
|
||
} catch { | ||
self.resultProcessed.send(completion: .failure(error)) | ||
return | ||
} | ||
} | ||
|
||
/// Wrap the Combine pipe to a native Swift Async Task for convenience | ||
computeResultTask = Task { | ||
do { | ||
let result: URL = try await withCheckedThrowingContinuation { continuation in | ||
self.resultProcessedObserver = resultProcessed.sink { result in | ||
switch result { | ||
case .finished: | ||
break | ||
case .failure(let error): | ||
continuation.resume(throwing: error) | ||
} | ||
self.resultProcessedObserver?.cancel() | ||
} receiveValue: { value in | ||
continuation.resume(with: .success(value)) | ||
} | ||
} | ||
|
||
return result | ||
|
||
} catch { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
// MARK: Public | ||
|
||
public var progress: Progress | ||
|
||
public var result: Result<URL, Error> { | ||
get async { | ||
guard let computeResultTask else { | ||
fatalError("This never should be nil") | ||
} | ||
|
||
return await computeResultTask.result | ||
} | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.