|
| 1 | +final class DataMigrator { |
| 2 | + |
| 3 | + private let coreDataStack: CoreDataStack |
| 4 | + private let backupLocation: URL? |
| 5 | + private let keychainUtils: KeychainUtils |
| 6 | + private let localDefaults: UserDefaults |
| 7 | + private let sharedDefaults: UserDefaults? |
| 8 | + |
| 9 | + init(coreDataStack: CoreDataStack = ContextManager.sharedInstance(), |
| 10 | + backupLocation: URL? = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.org.wordpress")?.appendingPathComponent("WordPress.sqlite"), |
| 11 | + keychainUtils: KeychainUtils = KeychainUtils(), |
| 12 | + localDefaults: UserDefaults = UserDefaults.standard, |
| 13 | + sharedDefaults: UserDefaults? = UserDefaults(suiteName: WPAppGroupName)) { |
| 14 | + self.coreDataStack = coreDataStack |
| 15 | + self.backupLocation = backupLocation |
| 16 | + self.keychainUtils = keychainUtils |
| 17 | + self.localDefaults = localDefaults |
| 18 | + self.sharedDefaults = sharedDefaults |
| 19 | + } |
| 20 | + |
| 21 | + enum DataMigratorError: Error { |
| 22 | + case localDraftsNotSynced |
| 23 | + case databaseCopyError |
| 24 | + case keychainError |
| 25 | + case sharedUserDefaultsNil |
| 26 | + } |
| 27 | + |
| 28 | + func exportData(completion: ((Result<Void, DataMigratorError>) -> Void)? = nil) { |
| 29 | + guard isLocalDraftsSynced() else { |
| 30 | + completion?(.failure(.localDraftsNotSynced)) |
| 31 | + return |
| 32 | + } |
| 33 | + guard let backupLocation, copyDatabase(to: backupLocation) else { |
| 34 | + completion?(.failure(.databaseCopyError)) |
| 35 | + return |
| 36 | + } |
| 37 | + guard copyKeychain(from: nil, to: WPAppKeychainAccessGroup) else { |
| 38 | + completion?(.failure(.keychainError)) |
| 39 | + return |
| 40 | + } |
| 41 | + guard copyUserDefaults(from: localDefaults, to: sharedDefaults) else { |
| 42 | + completion?(.failure(.sharedUserDefaultsNil)) |
| 43 | + return |
| 44 | + } |
| 45 | + BloggingRemindersScheduler.handleRemindersMigration() |
| 46 | + completion?(.success(())) |
| 47 | + } |
| 48 | + |
| 49 | + func importData(completion: ((Result<Void, DataMigratorError>) -> Void)? = nil) { |
| 50 | + guard let backupLocation, restoreDatabase(from: backupLocation) else { |
| 51 | + completion?(.failure(.databaseCopyError)) |
| 52 | + return |
| 53 | + } |
| 54 | + guard copyKeychain(from: WPAppKeychainAccessGroup, to: nil) else { |
| 55 | + completion?(.failure(.keychainError)) |
| 56 | + return |
| 57 | + } |
| 58 | + guard copyUserDefaults(from: sharedDefaults, to: localDefaults) else { |
| 59 | + completion?(.failure(.sharedUserDefaultsNil)) |
| 60 | + return |
| 61 | + } |
| 62 | + BloggingRemindersScheduler.handleRemindersMigration() |
| 63 | + completion?(.success(())) |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +// MARK: - Private Functions |
| 69 | + |
| 70 | +private extension DataMigrator { |
| 71 | + |
| 72 | + func isLocalDraftsSynced() -> Bool { |
| 73 | + let fetchRequest = NSFetchRequest<Post>(entityName: String(describing: Post.self)) |
| 74 | + fetchRequest.predicate = NSPredicate(format: "status = %@ && (remoteStatusNumber = %@ || remoteStatusNumber = %@ || remoteStatusNumber = %@ || remoteStatusNumber = %@)", |
| 75 | + BasePost.Status.draft.rawValue, |
| 76 | + NSNumber(value: AbstractPostRemoteStatus.pushing.rawValue), |
| 77 | + NSNumber(value: AbstractPostRemoteStatus.failed.rawValue), |
| 78 | + NSNumber(value: AbstractPostRemoteStatus.local.rawValue), |
| 79 | + NSNumber(value: AbstractPostRemoteStatus.pushingMedia.rawValue)) |
| 80 | + guard let count = try? coreDataStack.mainContext.count(for: fetchRequest) else { |
| 81 | + return false |
| 82 | + } |
| 83 | + |
| 84 | + return count == 0 |
| 85 | + } |
| 86 | + |
| 87 | + func copyDatabase(to destination: URL) -> Bool { |
| 88 | + do { |
| 89 | + try coreDataStack.createStoreCopy(to: destination) |
| 90 | + } catch { |
| 91 | + DDLogError("Error copying database: \(error)") |
| 92 | + return false |
| 93 | + } |
| 94 | + return true |
| 95 | + } |
| 96 | + |
| 97 | + func restoreDatabase(from source: URL) -> Bool { |
| 98 | + do { |
| 99 | + try coreDataStack.restoreStoreCopy(from: source) |
| 100 | + } catch { |
| 101 | + DDLogError("Error restoring database: \(error)") |
| 102 | + return false |
| 103 | + } |
| 104 | + return true |
| 105 | + } |
| 106 | + |
| 107 | + func copyKeychain(from sourceAccessGroup: String?, to destinationAccessGroup: String?) -> Bool { |
| 108 | + do { |
| 109 | + try keychainUtils.copyKeychain(from: sourceAccessGroup, to: destinationAccessGroup) |
| 110 | + } catch { |
| 111 | + DDLogError("Error copying keychain: \(error)") |
| 112 | + return false |
| 113 | + } |
| 114 | + |
| 115 | + return true |
| 116 | + } |
| 117 | + |
| 118 | + func copyUserDefaults(from source: UserDefaults?, to destination: UserDefaults?) -> Bool { |
| 119 | + guard let source, let destination else { |
| 120 | + return false |
| 121 | + } |
| 122 | + let data = source.dictionaryRepresentation() |
| 123 | + for (key, value) in data { |
| 124 | + destination.set(value, forKey: key) |
| 125 | + } |
| 126 | + |
| 127 | + return true |
| 128 | + } |
| 129 | +} |
0 commit comments