aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-11-15 13:37:59 +0530
committerRoopesh Chander <roop@roopc.net>2018-11-15 13:39:56 +0530
commit6bb25782c178d382349e729615011b414e671354 (patch)
tree44464256288dddba6d80c86881d758bb9f00c755 /WireGuard/WireGuard
parentExporting: Refactor out zip exporting into a separate class (diff)
downloadwireguard-apple-6bb25782c178d382349e729615011b414e671354.tar.xz
wireguard-apple-6bb25782c178d382349e729615011b414e671354.zip
Exporting: Export to zip in a background thread
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard')
-rw-r--r--WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift20
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipExporter.swift35
2 files changed, 32 insertions, 23 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift
index dc2e27e..b003b08 100644
--- a/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift
+++ b/WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift
@@ -76,17 +76,17 @@ class SettingsTableViewController: UITableViewController {
let count = tunnelsManager.numberOfTunnels()
let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() }
- do {
- try ZipExporter.exportConfigFiles(tunnelConfigurations: tunnelConfigurations, to: destinationURL)
- } catch (let error) {
- ErrorPresenter.showErrorAlert(error: error, from: self)
+ ZipExporter.exportConfigFiles(tunnelConfigurations: tunnelConfigurations, to: destinationURL) { [weak self] (error) in
+ if let error = error {
+ ErrorPresenter.showErrorAlert(error: error, from: self)
+ return
+ }
+ let activityVC = UIActivityViewController(activityItems: [destinationURL], applicationActivities: nil)
+ // popoverPresentationController shall be non-nil on the iPad
+ activityVC.popoverPresentationController?.sourceView = sourceView
+ activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
+ self?.present(activityVC, animated: true)
}
-
- let activityVC = UIActivityViewController(activityItems: [destinationURL], applicationActivities: nil)
- // popoverPresentationController shall be non-nil on the iPad
- activityVC.popoverPresentationController?.sourceView = sourceView
- activityVC.popoverPresentationController?.sourceRect = sourceView.bounds
- present(activityVC, animated: true)
}
func showErrorAlert(title: String, message: String) {
diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
index d0cf9a7..bd6c3dd 100644
--- a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
+++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift
@@ -8,21 +8,30 @@ enum ZipExporterError: Error {
}
class ZipExporter {
- static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to destinationURL: URL) throws {
+ static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, completion: @escaping (Error?) -> Void) {
- guard (!tunnelConfigurations.isEmpty) else { throw ZipExporterError.noTunnelsToExport }
-
- var inputsToArchiver: [(fileName: String, contents: Data)] = []
-
- var lastTunnelName: String = ""
- for tunnelConfiguration in tunnelConfigurations {
- if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) {
- let name = tunnelConfiguration.interface.name
- if (name.isEmpty || name == lastTunnelName) { continue }
- inputsToArchiver.append((fileName: "\(name).conf", contents: contents))
- lastTunnelName = name
+ guard (!tunnelConfigurations.isEmpty) else {
+ completion(ZipExporterError.noTunnelsToExport)
+ return
+ }
+ DispatchQueue.global(qos: .userInitiated).async {
+ var inputsToArchiver: [(fileName: String, contents: Data)] = []
+ var lastTunnelName: String = ""
+ for tunnelConfiguration in tunnelConfigurations {
+ if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) {
+ let name = tunnelConfiguration.interface.name
+ if (name.isEmpty || name == lastTunnelName) { continue }
+ inputsToArchiver.append((fileName: "\(name).conf", contents: contents))
+ lastTunnelName = name
+ }
+ }
+ do {
+ try ZipArchive.archive(inputs: inputsToArchiver, to: url)
+ } catch (let e) {
+ DispatchQueue.main.async { completion(e) }
+ return
}
+ DispatchQueue.main.async { completion(nil) }
}
- try ZipArchive.archive(inputs: inputsToArchiver, to: destinationURL)
}
}