From d8975a4bd3355f6227258ac9440f7dd834e17c9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Julian-Steffen=20M=C3=BCller?= Date: Thu, 20 May 2021 14:36:03 +0200 Subject: [PATCH] Library Backup is now done through icloud --- Zockerhoehle/AppDelegate.swift | 3 +++ Zockerhoehle/ICloudManager.swift | 29 ++++++++++++++++++++++++-- Zockerhoehle/Utils/LibraryExport.swift | 12 ++++++----- Zockerhoehle/Utils/LibraryImport.swift | 10 ++++----- 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Zockerhoehle/AppDelegate.swift b/Zockerhoehle/AppDelegate.swift index 15e0851..7877aa6 100644 --- a/Zockerhoehle/AppDelegate.swift +++ b/Zockerhoehle/AppDelegate.swift @@ -23,6 +23,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { #endif print("disFinishLaunchung") + + ICloudManager.shared.preload_all_data() + return true } } diff --git a/Zockerhoehle/ICloudManager.swift b/Zockerhoehle/ICloudManager.swift index 8cf5db5..605fee2 100644 --- a/Zockerhoehle/ICloudManager.swift +++ b/Zockerhoehle/ICloudManager.swift @@ -14,6 +14,18 @@ class ICloudManager { static let relative_seperator : [String] = ["iCloud~Zockerhoehle", "Documents"] + static var backup_folder : URL? { + get { + return documents_folder?.appendingPathComponent("backup") + } + } + + static var documents_folder : URL? { + get { + return FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents") + } + } + static public func relativePathFrom(url : URL) -> String? { return url.relative_path_after(pathComponent: relative_seperator) ?? .none } @@ -21,7 +33,7 @@ class ICloudManager { static public func fileExists(at path : String?) -> Bool { guard let path = path else { return false } - if let url = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent(path) { + if let url = documents_folder?.appendingPathComponent(path) { return FileManager.default.fileExists(atPath: url.path) } @@ -32,7 +44,7 @@ class ICloudManager { guard let path = path else { return .none } do { - let url = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents").appendingPathComponent(path) + let url = documents_folder?.appendingPathComponent(path) let imageData = try Data(contentsOf: url!) return UIImage(data: imageData)! @@ -53,6 +65,19 @@ class ICloudManager { return false } + public func preload_all_data() -> Bool { + guard let docs_url = ICloudManager.documents_folder else { return false } + + do { + try FileManager.default.startDownloadingUbiquitousItem(at: docs_url) + + return true + }catch{ + return false + } + + } + private init() { } diff --git a/Zockerhoehle/Utils/LibraryExport.swift b/Zockerhoehle/Utils/LibraryExport.swift index e1c8625..ac322e4 100644 --- a/Zockerhoehle/Utils/LibraryExport.swift +++ b/Zockerhoehle/Utils/LibraryExport.swift @@ -60,12 +60,14 @@ struct LibraryExporter : Encodable { print("Exported Library to '\(exportFileName)' (Size: \(fileSizeAsString))") do { - let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) - let encodedFileURL = documentDirectory.appendingPathComponent(exportFileName) - - try data.write(to: encodedFileURL) - }catch { + if let documentDirectory = ICloudManager.backup_folder { + let encodedFileURL = documentDirectory.appendingPathComponent(exportFileName) + print("WRITE TO \(encodedFileURL)") + try data.write(to: encodedFileURL) + } + }catch let error { + print("Export failed. Error \(error)") } } } diff --git a/Zockerhoehle/Utils/LibraryImport.swift b/Zockerhoehle/Utils/LibraryImport.swift index b68ed03..a3ca050 100644 --- a/Zockerhoehle/Utils/LibraryImport.swift +++ b/Zockerhoehle/Utils/LibraryImport.swift @@ -25,7 +25,7 @@ class LibraryImport { } func backupFiles() -> [String] { do { - let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) + guard let documentDirectory = try ICloudManager.backup_folder else { return [] } let filesInDocumentsDir = try FileManager.default.contentsOfDirectory(at: documentDirectory, includingPropertiesForKeys: .none, options: []) @@ -135,13 +135,13 @@ class LibraryImport { func importLIB(from filename: String) { do { - let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:false) - - let file = documentDirectory.appendingPathComponent(filename) + guard let file = ICloudManager.backup_folder?.appendingPathComponent(filename) else { + print("LibraryImport:importLIB Could not get icloud backup folder") + return + } if file.isFileURL { let library = try JSONDecoder().decode(BHLibrary.self, from: Data(contentsOf: file)) - resetDatabase() var gameDict = [UUID:Game]()