]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
Exporting: Export to zip in a background thread
authorRoopesh Chander <roop@roopc.net>
Thu, 15 Nov 2018 08:07:59 +0000 (13:37 +0530)
committerRoopesh Chander <roop@roopc.net>
Thu, 15 Nov 2018 08:09:56 +0000 (13:39 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
WireGuard/WireGuard/UI/iOS/SettingsTableViewController.swift
WireGuard/WireGuard/ZipArchive/ZipExporter.swift

index dc2e27ef2164f537a52395dd169be7e50bafe60e..b003b08154164e586c836c13b7522bdc7f37d909 100644 (file)
@@ -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) {
index d0cf9a7ec65a4caf306c4aa54fb37a189931e1fd..bd6c3dd908c3d3f4efb72ebaabd2b749f58793ba 100644 (file)
@@ -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)
     }
 }