Exporting: Export to zip in a background thread

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-11-15 13:37:59 +05:30
parent 1dac181803
commit 6bb25782c1
2 changed files with 33 additions and 24 deletions

View File

@ -76,17 +76,17 @@ class SettingsTableViewController: UITableViewController {
let count = tunnelsManager.numberOfTunnels() let count = tunnelsManager.numberOfTunnels()
let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() } let tunnelConfigurations = (0 ..< count).compactMap { tunnelsManager.tunnel(at: $0).tunnelConfiguration() }
do { ZipExporter.exportConfigFiles(tunnelConfigurations: tunnelConfigurations, to: destinationURL) { [weak self] (error) in
try ZipExporter.exportConfigFiles(tunnelConfigurations: tunnelConfigurations, to: destinationURL) if let error = error {
} catch (let error) { ErrorPresenter.showErrorAlert(error: error, from: self)
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) { func showErrorAlert(title: String, message: String) {

View File

@ -8,21 +8,30 @@ enum ZipExporterError: Error {
} }
class ZipExporter { 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 } guard (!tunnelConfigurations.isEmpty) else {
completion(ZipExporterError.noTunnelsToExport)
var inputsToArchiver: [(fileName: String, contents: Data)] = [] return
}
var lastTunnelName: String = "" DispatchQueue.global(qos: .userInitiated).async {
for tunnelConfiguration in tunnelConfigurations { var inputsToArchiver: [(fileName: String, contents: Data)] = []
if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) { var lastTunnelName: String = ""
let name = tunnelConfiguration.interface.name for tunnelConfiguration in tunnelConfigurations {
if (name.isEmpty || name == lastTunnelName) { continue } if let contents = WgQuickConfigFileWriter.writeConfigFile(from: tunnelConfiguration) {
inputsToArchiver.append((fileName: "\(name).conf", contents: contents)) let name = tunnelConfiguration.interface.name
lastTunnelName = 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)
} }
} }