Replaces UIDocumentPickerViewController with UIDocumentBrowserViewController on iOS 11
This commit is contained in:
parent
c0b3a04110
commit
2da455bc2f
@ -504,10 +504,12 @@ extension GameCollectionViewController: SaveStatesViewControllerDelegate
|
|||||||
/// ImportControllerDelegate
|
/// ImportControllerDelegate
|
||||||
extension GameCollectionViewController: ImportControllerDelegate
|
extension GameCollectionViewController: ImportControllerDelegate
|
||||||
{
|
{
|
||||||
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>)
|
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>, errors: [Error])
|
||||||
{
|
{
|
||||||
guard let game = self._changingArtworkGame else { return }
|
guard let game = self._changingArtworkGame else { return }
|
||||||
|
|
||||||
|
var errors = errors
|
||||||
|
|
||||||
var imageURL: URL?
|
var imageURL: URL?
|
||||||
|
|
||||||
if let url = urls.first
|
if let url = urls.first
|
||||||
@ -533,7 +535,7 @@ extension GameCollectionViewController: ImportControllerDelegate
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
print(error)
|
errors.append(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -542,6 +544,11 @@ extension GameCollectionViewController: ImportControllerDelegate
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for error in errors
|
||||||
|
{
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
|
||||||
if let imageURL = imageURL
|
if let imageURL = imageURL
|
||||||
{
|
{
|
||||||
// Remove previous artwork from cache.
|
// Remove previous artwork from cache.
|
||||||
|
|||||||
@ -283,8 +283,13 @@ extension GamesViewController: ImportControllerDelegate
|
|||||||
self.present(importController, animated: true, completion: nil)
|
self.present(importController, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>)
|
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>, errors: [Error])
|
||||||
{
|
{
|
||||||
|
for error in errors
|
||||||
|
{
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
|
||||||
let gameURLs = urls.filter { $0.pathExtension.lowercased() != "deltaskin" }
|
let gameURLs = urls.filter { $0.pathExtension.lowercased() != "deltaskin" }
|
||||||
DatabaseManager.shared.importGames(at: Set(gameURLs)) { (games, errors) in
|
DatabaseManager.shared.importGames(at: Set(gameURLs)) { (games, errors) in
|
||||||
if errors.count > 0
|
if errors.count > 0
|
||||||
|
|||||||
@ -16,7 +16,7 @@ import Roxas
|
|||||||
|
|
||||||
protocol ImportControllerDelegate
|
protocol ImportControllerDelegate
|
||||||
{
|
{
|
||||||
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>)
|
func importController(_ importController: ImportController, didImportItemsAt urls: Set<URL>, errors: [Error])
|
||||||
|
|
||||||
/** Optional **/
|
/** Optional **/
|
||||||
func importControllerDidCancel(_ importController: ImportController)
|
func importControllerDidCancel(_ importController: ImportController)
|
||||||
@ -37,20 +37,35 @@ class ImportController: NSObject
|
|||||||
var delegate: ImportControllerDelegate?
|
var delegate: ImportControllerDelegate?
|
||||||
var importOptions: [ImportOption]?
|
var importOptions: [ImportOption]?
|
||||||
|
|
||||||
|
fileprivate weak var presentingViewController: UIViewController?
|
||||||
|
|
||||||
|
// Store presentedViewController separately, since when we dismiss we don't know if it has already been dismissed.
|
||||||
|
// Calling dismiss on presentingViewController in that case would dismiss presentingViewController, which is bad.
|
||||||
|
fileprivate weak var presentedViewController: UIViewController?
|
||||||
|
|
||||||
|
fileprivate let importQueue: OperationQueue
|
||||||
|
fileprivate let fileCoordinator: NSFileCoordinator
|
||||||
|
|
||||||
init(documentTypes: Set<String>)
|
init(documentTypes: Set<String>)
|
||||||
{
|
{
|
||||||
self.documentTypes = documentTypes
|
self.documentTypes = documentTypes
|
||||||
|
|
||||||
|
let dispatchQueue = DispatchQueue(label: "com.rileytestut.Delta.ImportController.dispatchQueue", qos: .userInitiated, attributes: .concurrent)
|
||||||
|
|
||||||
|
self.importQueue = OperationQueue()
|
||||||
|
self.importQueue.name = "com.rileytestut.Delta.ImportController.importQueue"
|
||||||
|
self.importQueue.underlyingQueue = dispatchQueue
|
||||||
|
|
||||||
|
self.fileCoordinator = NSFileCoordinator(filePresenter: nil)
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate weak var presentingViewController: UIViewController?
|
|
||||||
|
|
||||||
fileprivate func presentImportController(from presentingViewController: UIViewController, animated: Bool, completionHandler: ((Void) -> Void)?)
|
fileprivate func presentImportController(from presentingViewController: UIViewController, animated: Bool, completionHandler: ((Void) -> Void)?)
|
||||||
{
|
{
|
||||||
self.presentingViewController = presentingViewController
|
self.presentingViewController = presentingViewController
|
||||||
|
|
||||||
#if IMPACTOR
|
#if IMPACTOR
|
||||||
|
|
||||||
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
|
||||||
alertController.addAction(UIAlertAction.cancel)
|
alertController.addAction(UIAlertAction.cancel)
|
||||||
@ -59,13 +74,16 @@ class ImportController: NSObject
|
|||||||
{
|
{
|
||||||
for importOption in importOptions
|
for importOption in importOptions
|
||||||
{
|
{
|
||||||
alertController.add(importOption, completionHandler: finish(with:))
|
alertController.add(importOption) { [unowned self] (urls) in
|
||||||
|
self.finish(with: urls, errors: [])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.presentedViewController = alertController
|
||||||
self.presentingViewController?.present(alertController, animated: true, completion: nil)
|
self.presentingViewController?.present(alertController, animated: true, completion: nil)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
let documentMenuController = UIDocumentMenuViewController(documentTypes: Array(self.documentTypes), in: .import)
|
let documentMenuController = UIDocumentMenuViewController(documentTypes: Array(self.documentTypes), in: .import)
|
||||||
documentMenuController.delegate = self
|
documentMenuController.delegate = self
|
||||||
@ -74,26 +92,36 @@ class ImportController: NSObject
|
|||||||
{
|
{
|
||||||
for importOption in reversedImportOptions
|
for importOption in reversedImportOptions
|
||||||
{
|
{
|
||||||
documentMenuController.add(importOption, order: .first, completionHandler: finish(with:))
|
documentMenuController.add(importOption, order: .first) { [unowned self] (urls) in
|
||||||
|
self.finish(with: urls, errors: [])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.presentedViewController = documentMenuController
|
||||||
self.presentingViewController?.present(documentMenuController, animated: true, completion: nil)
|
self.presentingViewController?.present(documentMenuController, animated: true, completion: nil)
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate func finish(with urls: Set<URL>?)
|
@objc fileprivate func cancel()
|
||||||
|
{
|
||||||
|
self.finish(with: nil, errors: [])
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func finish(with urls: Set<URL>?, errors: [Error])
|
||||||
{
|
{
|
||||||
if let urls = urls
|
if let urls = urls
|
||||||
{
|
{
|
||||||
self.delegate?.importController(self, didImportItemsAt: urls)
|
self.delegate?.importController(self, didImportItemsAt: urls, errors: errors)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
self.delegate?.importControllerDidCancel(self)
|
self.delegate?.importControllerDidCancel(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.presentedViewController?.dismiss(animated: true)
|
||||||
|
|
||||||
self.presentingViewController?.importController = nil
|
self.presentingViewController?.importController = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -102,14 +130,33 @@ class ImportController: NSObject
|
|||||||
extension ImportController: UIDocumentMenuDelegate
|
extension ImportController: UIDocumentMenuDelegate
|
||||||
{
|
{
|
||||||
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController)
|
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController)
|
||||||
|
{
|
||||||
|
if #available(iOS 11.0, *)
|
||||||
|
{
|
||||||
|
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(ImportController.cancel))
|
||||||
|
|
||||||
|
let documentBrowserViewController = UIDocumentBrowserViewController(forOpeningFilesWithContentTypes: Array(self.documentTypes))
|
||||||
|
documentBrowserViewController.delegate = self
|
||||||
|
documentBrowserViewController.browserUserInterfaceStyle = .dark
|
||||||
|
documentBrowserViewController.allowsPickingMultipleItems = true
|
||||||
|
documentBrowserViewController.allowsDocumentCreation = false
|
||||||
|
documentBrowserViewController.additionalTrailingNavigationBarButtonItems = [cancelButton]
|
||||||
|
|
||||||
|
self.presentedViewController = documentBrowserViewController
|
||||||
|
self.presentingViewController?.present(documentBrowserViewController, animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
documentPicker.delegate = self
|
documentPicker.delegate = self
|
||||||
|
|
||||||
|
self.presentedViewController = documentPicker
|
||||||
self.presentingViewController?.present(documentPicker, animated: true, completion: nil)
|
self.presentingViewController?.present(documentPicker, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController)
|
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController)
|
||||||
{
|
{
|
||||||
self.finish(with: nil)
|
self.finish(with: nil, errors: [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,15 +164,66 @@ extension ImportController: UIDocumentPickerDelegate
|
|||||||
{
|
{
|
||||||
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL)
|
||||||
{
|
{
|
||||||
self.finish(with: [url])
|
self.finish(with: [url], errors: [])
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
|
||||||
|
{
|
||||||
|
self.finish(with: Set(urls), errors: [])
|
||||||
}
|
}
|
||||||
|
|
||||||
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController)
|
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController)
|
||||||
{
|
{
|
||||||
self.finish(with: nil)
|
self.finish(with: nil, errors: [])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@available(iOS 11.0, *)
|
||||||
|
extension ImportController: UIDocumentBrowserViewControllerDelegate
|
||||||
|
{
|
||||||
|
func documentBrowser(_ controller: UIDocumentBrowserViewController, didPickDocumentURLs documentURLs: [URL])
|
||||||
|
{
|
||||||
|
var coordinatedURLs = Set<URL>()
|
||||||
|
var errors = [Error]()
|
||||||
|
|
||||||
|
let dispatchGroup = DispatchGroup()
|
||||||
|
|
||||||
|
for url in documentURLs
|
||||||
|
{
|
||||||
|
dispatchGroup.enter()
|
||||||
|
|
||||||
|
let intent = NSFileAccessIntent.readingIntent(with: url)
|
||||||
|
self.fileCoordinator.coordinate(with: [intent], queue: self.importQueue) { (error) in
|
||||||
|
if let error = error
|
||||||
|
{
|
||||||
|
errors.append(error)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
let temporaryURL = FileManager.default.temporaryDirectory.appendingPathComponent(url.lastPathComponent)
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
// Always access intent.url, as the system may have updated it when requesting access.
|
||||||
|
try FileManager.default.copyItem(at: intent.url, to: temporaryURL)
|
||||||
|
|
||||||
|
coordinatedURLs.insert(temporaryURL)
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
errors.append(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatchGroup.leave()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatchGroup.notify(queue: self.importQueue.underlyingQueue!) {
|
||||||
|
self.finish(with: coordinatedURLs, errors: errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private var ImportControllerKey: UInt8 = 0
|
private var ImportControllerKey: UInt8 = 0
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user