Share import logic between browser and imported

This commit is contained in:
Davide De Rosa 2020-07-17 14:20:41 +02:00
parent 2485c5b588
commit 0acef41a7f
2 changed files with 22 additions and 62 deletions

View File

@ -30,12 +30,14 @@ import PassepartoutCore
private let log = SwiftyBeaver.self
protocol ImportedHostsViewControllerDelegate: class {
func importedHostsController(_: ImportedHostsViewController, didImport url: URL)
}
class ImportedHostsViewController: UITableViewController {
private lazy var pendingConfigurationURLs = TransientStore.shared.service.pendingConfigurationURLs().sortedCaseInsensitive()
private var importer: HostImporter?
private var parsingResult: OpenVPN.ConfigurationParser.Result?
weak var delegate: ImportedHostsViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
@ -43,65 +45,8 @@ class ImportedHostsViewController: UITableViewController {
title = L10n.App.ImportedHosts.title
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
parsingResult = nil
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// guard !pendingConfigurationURLs.isEmpty else {
// let alert = UIAlertController.asAlert(
// title,
// L10n.Core.Organizer.Alerts.AddHost.message
// )
// alert.addCancelAction(L10n.Core.Global.ok) {
// self.close()
// }
// present(alert, animated: true, completion: nil)
// return
// }
if let selectedIP = tableView.indexPathForSelectedRow {
tableView.deselectRow(at: selectedIP, animated: true)
}
}
// MARK: Actions
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
// segue parses configuration file if not yet
if parsingResult == nil {
guard let cell = sender as? UITableViewCell, let indexPath = tableView.indexPath(for: cell) else {
return false
}
let url = pendingConfigurationURLs[indexPath.row]
return tryParseURL(url, cell: cell)
}
return true
}
private func tryParseURL(_ url: URL, cell: UITableViewCell?) -> Bool {
deselectSelectedRow()
importer = HostImporter(withConfigurationURL: url, parentViewController: self)
importer?.importHost(withPassphrase: nil, removeOnError: false, removeOnCancel: false) {
self.parsingResult = $0
self.perform(segue: StoryboardSegue.Organizer.importHostSegueIdentifier)
}
return true
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let wizard = segue.destination as? WizardHostViewController else {
return
}
wizard.parsingResult = parsingResult
// retain back button
wizard.navigationItem.leftBarButtonItem = nil
private func selectHost(withUrl url: URL) {
delegate?.importedHostsController(self, didImport: url)
}
@IBAction private func close() {
@ -126,6 +71,11 @@ extension ImportedHostsViewController {
cell.leftText = url.normalizedFilename
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let url = pendingConfigurationURLs[indexPath.row]
selectHost(withUrl: url)
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true

View File

@ -175,6 +175,8 @@ class OrganizerViewController: UITableViewController, StrongTableHost {
}
vc.setProfile(selectedProfile)
} else if let vc = destination as? ImportedHostsViewController {
vc.delegate = self
} else if let vc = destination as? WizardHostViewController {
vc.parsingResult = hostParsingResult
}
@ -788,6 +790,14 @@ extension OrganizerViewController: UIDocumentPickerDelegate {
}
}
extension OrganizerViewController: ImportedHostsViewControllerDelegate {
func importedHostsController(_: ImportedHostsViewController, didImport url: URL) {
dismiss(animated: true) {
self.tryParseHostURL(url)
}
}
}
extension OrganizerViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
dismiss(animated: true, completion: nil)