From 25afda3b60228978fdc396864b20931bcbf3bdc0 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Thu, 28 Apr 2022 16:54:18 -0700 Subject: [PATCH] Replaces UIAlertController with UIMenu for importing games --- .../Game Selection/GamesViewController.swift | 16 ++++++- .../Import Options/iTunesImportOption.swift | 2 +- Delta/Importing/ImportController.swift | 46 +++++++++++++------ 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Delta/Game Selection/GamesViewController.swift b/Delta/Game Selection/GamesViewController.swift index 40dd76e..f28e975 100644 --- a/Delta/Game Selection/GamesViewController.swift +++ b/Delta/Game Selection/GamesViewController.swift @@ -135,7 +135,21 @@ extension GamesViewController } } - self.importController.barButtonItem = self.importButton + if #available(iOS 14, *) + { + self.importController.presentingViewController = self + + let importActions = self.importController.makeActions().menuActions + let importMenu = UIMenu(title: NSLocalizedString("Import From…", comment: ""), image: UIImage(systemName: "square.and.arrow.down"), children: importActions) + self.importButton.menu = importMenu + + self.importButton.action = nil + self.importButton.target = nil + } + else + { + self.importController.barButtonItem = self.importButton + } self.prepareSearchController() diff --git a/Delta/Importing/Import Options/iTunesImportOption.swift b/Delta/Importing/Import Options/iTunesImportOption.swift index 6b9390f..1272156 100644 --- a/Delta/Importing/Import Options/iTunesImportOption.swift +++ b/Delta/Importing/Import Options/iTunesImportOption.swift @@ -13,7 +13,7 @@ import DeltaCore struct iTunesImportOption: ImportOption { let title = NSLocalizedString("iTunes", comment: "") - let image: UIImage? = nil + let image: UIImage? = UIImage(symbolNameIfAvailable: "music.note") private let presentingViewController: UIViewController diff --git a/Delta/Importing/ImportController.swift b/Delta/Importing/ImportController.swift index 1f26d3b..7c40864 100644 --- a/Delta/Importing/ImportController.swift +++ b/Delta/Importing/ImportController.swift @@ -37,11 +37,11 @@ class ImportController: NSObject var delegate: ImportControllerDelegate? var importOptions: [ImportOption]? + weak var presentingViewController: UIViewController? + weak var barButtonItem: UIBarButtonItem? weak var sourceView: UIView? - private 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. private weak var presentedViewController: UIViewController? @@ -64,26 +64,44 @@ class ImportController: NSObject super.init() } + func makeActions() -> [Action] + { + assert(self.presentingViewController != nil, "presentingViewController must be set before calling makeActions()") + + var actions = (self.importOptions ?? []).map { (option) -> Action in + let action = Action(title: option.title, style: .default, image: option.image) { _ in + option.import { importedURLs in + self.finish(with: importedURLs, errors: []) + } + } + + return action + } + + let filesAction = Action(title: NSLocalizedString("Files", comment: ""), style: .default, image: UIImage(symbolNameIfAvailable: "doc")) { action in + self.presentDocumentBrowser() + } + actions.append(filesAction) + + return actions + } + fileprivate func presentImportController(from presentingViewController: UIViewController, animated: Bool, completionHandler: (() -> Void)?) { self.presentingViewController = presentingViewController - let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) - alertController.addAction(UIAlertAction.cancel) + let actions = self.makeActions() - if let importOptions = self.importOptions + if actions.count > 1 { - for importOption in importOptions - { - alertController.add(importOption) { [unowned self] (urls) in - self.finish(with: urls, errors: []) - } - } + let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet) + alertController.addAction(UIAlertAction.cancel) - let filesAction = UIAlertAction(title: NSLocalizedString("Files", comment: ""), style: .default) { (action) in - self.presentDocumentBrowser() + let alertActions = actions.map { UIAlertAction($0) } + for action in alertActions + { + alertController.addAction(action) } - alertController.addAction(filesAction) if let sourceView = self.sourceView {