Keep track of migrations that are performed

This commit is contained in:
Gabriel Wong 2017-12-08 15:10:54 +07:00
parent 8ba393e034
commit 3b2e1153ca
3 changed files with 22 additions and 0 deletions

View file

@ -19,6 +19,7 @@ public enum Key {
static let engineVersion = "KeymanEngineVersion"
static let keyboardPickerDisplayed = "KeyboardPickerDisplayed"
static let synchronizeSWKeyboard = "KeymanSynchronizeSWKeyboard"
static let migrationLevel = "KeymanEngineMigrationLevel"
// JSON keys for language REST calls
static let options = "options"

View file

@ -84,4 +84,14 @@ public extension UserDefaults {
public func userKeyboard(withID keyboardID: String, languageID: String) -> InstallableKeyboard? {
return userKeyboards?.first { $0.id == keyboardID && $0.languageID == languageID }
}
var migrationLevel: Int {
get {
return integer(forKey: Key.migrationLevel)
}
set(level) {
set(level, forKey: Key.migrationLevel)
}
}
}

View file

@ -8,8 +8,17 @@
import Foundation
fileprivate enum MigrationLevel {
static let initial = 0
static let migratedForKMP = 10
}
enum Migrations {
static func migrateForKMP(storage: Storage) {
guard storage.userDefaults.migrationLevel < MigrationLevel.migratedForKMP else {
return
}
let languageDir = storage.baseDir.appendingPathComponent("languages")
let fontDir = storage.baseDir.appendingPathComponent("fonts")
@ -80,6 +89,8 @@ enum Migrations {
// Remove keyboards that were not copied successfully
let filteredUserKeyboards = userKeyboards.filter { successfulKeyboards.contains($0.id) }
storage.userDefaults.userKeyboards = filteredUserKeyboards
storage.userDefaults.migrationLevel = MigrationLevel.migratedForKMP
storage.userDefaults.synchronize()
// TODO: Remove old directory
}