Handle potentially unsupported as a warning alert

Configuration is imported anyway, so alert must be asynchronous.
This commit is contained in:
Davide De Rosa 2018-10-27 14:14:46 +02:00
parent 0b7ab7bca6
commit 8d2ce2e7ae
4 changed files with 65 additions and 16 deletions

View File

@ -95,12 +95,17 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
try? fm.removeItem(at: url) try? fm.removeItem(at: url)
return true return true
} }
handleParsedFile(parsedFile, in: root)
return true
}
private func handleParsedFile(_ parsedFile: ParsedFile, in root: UIViewController) {
// already presented: update parsed configuration // already presented: update parsed configuration
if let nav = root.presentedViewController as? UINavigationController, let wizard = nav.topViewController as? WizardHostViewController { if let nav = root.presentedViewController as? UINavigationController, let wizard = nav.topViewController as? WizardHostViewController {
wizard.parsedFile = parsedFile wizard.parsedFile = parsedFile
wizard.removesConfigurationOnCancel = true wizard.removesConfigurationOnCancel = true
return true return
} }
// present now // present now
@ -121,7 +126,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDele
} }
nav.modalPresentationStyle = .formSheet nav.modalPresentationStyle = .formSheet
root.present(nav, animated: true, completion: nil) root.present(nav, animated: true, completion: nil)
return true
} }
} }

View File

@ -32,26 +32,19 @@ private let log = SwiftyBeaver.self
extension ParsedFile { extension ParsedFile {
static func from(_ url: URL, withErrorAlertIn viewController: UIViewController) -> ParsedFile? { static func from(_ url: URL, withErrorAlertIn viewController: UIViewController) -> ParsedFile? {
let file: ParsedFile
log.debug("Parsing configuration URL: \(url)") log.debug("Parsing configuration URL: \(url)")
do { do {
return try TunnelKitProvider.Configuration.parsed(from: url) file = try TunnelKitProvider.Configuration.parsed(from: url)
} catch ApplicationError.missingConfiguration(let option) {
log.error("Could not parse configuration URL: missing configuration, \(option)")
let message = L10n.ParsedFile.Alerts.Missing.message(option)
alertConfigurationImportError(url: url, in: viewController, withMessage: message)
} catch ApplicationError.unsupportedConfiguration(let option) {
log.error("Could not parse configuration URL: unsupported configuration, \(option)")
let message = L10n.ParsedFile.Alerts.Unsupported.message(option)
alertConfigurationImportError(url: url, in: viewController, withMessage: message)
} catch let e { } catch let e {
log.error("Could not parse configuration URL: \(e)") let message = localizedMessage(forError: e)
let message = L10n.ParsedFile.Alerts.Parsing.message(e.localizedDescription) alertImportError(url: url, in: viewController, withMessage: message)
alertConfigurationImportError(url: url, in: viewController, withMessage: message) return nil
} }
return nil return file
} }
private static func alertConfigurationImportError(url: URL, in vc: UIViewController, withMessage message: String) { private static func alertImportError(url: URL, in vc: UIViewController, withMessage message: String) {
let alert = Macros.alert(url.normalizedFilename, message) let alert = Macros.alert(url.normalizedFilename, message)
// alert.addDefaultAction(L10n.ParsedFile.Alerts.Buttons.report) { // alert.addDefaultAction(L10n.ParsedFile.Alerts.Buttons.report) {
// var attach = IssueReporter.Attachments(debugLog: false, configurationURL: url) // var attach = IssueReporter.Attachments(debugLog: false, configurationURL: url)
@ -61,4 +54,48 @@ extension ParsedFile {
alert.addCancelAction(L10n.Global.ok) alert.addCancelAction(L10n.Global.ok)
vc.present(alert, animated: true, completion: nil) vc.present(alert, animated: true, completion: nil)
} }
static func alertImportWarning(url: URL, in vc: UIViewController, withWarning warning: ApplicationError, completionHandler: @escaping (Bool) -> Void) {
let message = details(forWarning: warning)
let alert = Macros.alert(url.normalizedFilename, L10n.ParsedFile.Alerts.PotentiallyUnsupported.message(message))
alert.addDefaultAction(L10n.Global.ok) {
completionHandler(true)
}
alert.addCancelAction(L10n.Global.cancel) {
completionHandler(false)
}
vc.present(alert, animated: true, completion: nil)
}
private static func localizedMessage(forError error: Error) -> String {
if let appError = error as? ApplicationError {
switch appError {
case .missingConfiguration(let option):
log.error("Could not parse configuration URL: missing configuration, \(option)")
return L10n.ParsedFile.Alerts.Missing.message(option)
case .unsupportedConfiguration(let option):
log.error("Could not parse configuration URL: unsupported configuration, \(option)")
return L10n.ParsedFile.Alerts.Unsupported.message(option)
default:
break
}
}
log.error("Could not parse configuration URL: \(error)")
return L10n.ParsedFile.Alerts.Parsing.message(error.localizedDescription)
}
private static func details(forWarning warning: ApplicationError) -> String {
switch warning {
case .missingConfiguration(let option):
return option
case .unsupportedConfiguration(let option):
return option
default:
fatalError("Only use .missingConfiguration or .unsupportedConfiguration for warnings")
}
}
} }

View File

@ -55,6 +55,7 @@
"parsed_file.alerts.missing.message" = "The configuration file lacks a required option (%@)."; "parsed_file.alerts.missing.message" = "The configuration file lacks a required option (%@).";
"parsed_file.alerts.unsupported.message" = "The configuration file contains an unsupported option (%@)."; "parsed_file.alerts.unsupported.message" = "The configuration file contains an unsupported option (%@).";
"parsed_file.alerts.potentially_unsupported.message" = "The configuration file is correct but contains a potentially unsupported option (%@).\n\nConnectivity may break depending on server settings.";
"parsed_file.alerts.parsing.message" = "Unable to parse the provided configuration file (%@)."; "parsed_file.alerts.parsing.message" = "Unable to parse the provided configuration file (%@).";
"parsed_file.alerts.buttons.report" = "Report an issue"; "parsed_file.alerts.buttons.report" = "Report an issue";

View File

@ -432,6 +432,13 @@ internal enum L10n {
} }
} }
internal enum PotentiallyUnsupported {
/// The configuration file is correct but contains a potentially unsupported option (%@).\n\nConnectivity may break depending on server settings.
internal static func message(_ p1: String) -> String {
return L10n.tr("Localizable", "parsed_file.alerts.potentially_unsupported.message", p1)
}
}
internal enum Unsupported { internal enum Unsupported {
/// The configuration file contains an unsupported option (%@). /// The configuration file contains an unsupported option (%@).
internal static func message(_ p1: String) -> String { internal static func message(_ p1: String) -> String {