1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-28 08:13:17 +03:00
vimr/VimR/PrefStore.swift

121 lines
3.8 KiB
Swift
Raw Normal View History

2016-07-26 22:42:30 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
2016-08-03 22:41:43 +03:00
import Cocoa
2016-07-26 22:42:30 +03:00
import RxSwift
2016-11-19 15:29:18 +03:00
protocol StandardPrefData {
init?(dict: [String: Any])
func dict() -> [String: Any]
}
struct PrefData: StandardPrefData {
fileprivate static let general = "general"
fileprivate static let appearance = "appearance"
fileprivate static let advanced = "advanced"
fileprivate static let mainWindow = "mainWindow"
static let `default` = PrefData(general: .default, appearance: .default, advanced: .default, mainWindow: .default)
var general: GeneralPrefData
var appearance: AppearancePrefData
var advanced: AdvancedPrefData
2016-11-19 15:29:18 +03:00
var mainWindow: MainWindowPrefData
2016-11-19 15:29:18 +03:00
init(general: GeneralPrefData,
appearance: AppearancePrefData,
advanced: AdvancedPrefData,
mainWindow: MainWindowPrefData)
{
self.general = general
self.appearance = appearance
self.advanced = advanced
self.mainWindow = mainWindow
}
2016-08-14 16:38:41 +03:00
2016-11-19 15:29:18 +03:00
init?(dict: [String: Any]) {
guard let generalDict: [String: Any] = PrefUtils.value(from: dict, for: PrefData.general),
let appearanceDict: [String: Any] = PrefUtils.value(from: dict, for: PrefData.appearance),
let advancedDict: [String: Any] = PrefUtils.value(from: dict, for: PrefData.advanced),
let mainWindowDict: [String: Any] = PrefUtils.value(from: dict, for: PrefData.mainWindow)
else {
return nil
}
2016-09-24 17:31:14 +03:00
2016-11-19 15:29:18 +03:00
guard let general = GeneralPrefData(dict: generalDict),
let appearance = AppearancePrefData(dict: appearanceDict),
let advanced = AdvancedPrefData(dict: advancedDict),
let mainWindow = MainWindowPrefData(dict: mainWindowDict)
else {
return nil
}
2016-11-19 15:29:18 +03:00
self.init(general: general, appearance: appearance, advanced: advanced, mainWindow: mainWindow)
}
2016-11-19 15:29:18 +03:00
func dict() -> [String: Any] {
return [
PrefData.general: self.general.dict(),
PrefData.appearance: self.appearance.dict(),
PrefData.advanced: self.advanced.dict(),
PrefData.mainWindow: self.mainWindow.dict(),
]
}
2016-07-26 22:42:30 +03:00
}
2016-10-29 19:45:14 +03:00
/**
2016-11-19 15:29:18 +03:00
To reset prefs for 38
2016-10-29 19:45:14 +03:00
$ defaults write com.qvacua.vimr 38 -dict editor-font-name InputMonoCompressed-Regular editor-font-size 13 editor-uses-ligatures 0 open-new-window-on-reactivation 1 open-new-window-when-launching 1
$ defaults read ~/Library/Preferences/com.qvacua.VimR
*/
2016-09-27 19:02:05 +03:00
class PrefStore: StandardFlow {
2016-07-26 22:42:30 +03:00
2016-11-19 15:29:18 +03:00
fileprivate static let compatibleVersion = "128"
fileprivate static let lastCompatibleVersion = "38"
2016-09-25 18:50:33 +03:00
fileprivate let userDefaults = UserDefaults.standard
2016-11-19 15:29:18 +03:00
var data = PrefData.default
2016-07-26 22:42:30 +03:00
2016-09-27 19:02:05 +03:00
override init(source: Observable<Any>) {
super.init(source: source)
2016-07-26 22:42:30 +03:00
2016-09-25 18:50:33 +03:00
if let prefs = self.userDefaults.dictionary(forKey: PrefStore.compatibleVersion) {
2016-11-19 15:29:18 +03:00
// 128
self.data = PrefData(dict: prefs) ?? .default
} else if let dictLastVersion = self.userDefaults.dictionary(forKey: PrefStore.lastCompatibleVersion) {
// 38
self.data = Pref38To128Converter().prefData128(from: dictLastVersion)
2016-07-26 22:42:30 +03:00
}
2016-08-14 16:38:41 +03:00
2016-11-19 15:29:18 +03:00
// We write self.data here for when PrefData(dict: prefs) was nil or the prefs last compatible version was
// converted.
self.userDefaults.setValue(self.data.dict(), forKey: PrefStore.compatibleVersion)
2016-07-26 22:42:30 +03:00
}
2016-09-27 19:02:05 +03:00
override func subscription(source: Observable<Any>) -> Disposable {
return source
.filter { $0 is PrefData || $0 is MainWindowPrefData }
.subscribe(onNext: { [unowned self] data in
switch data {
case let prefData as PrefData:
self.data = prefData
case let mainWindowPrefData as MainWindowPrefData:
self.data.mainWindow = mainWindowPrefData
default:
return
}
2016-11-19 15:29:18 +03:00
self.userDefaults.setValue(self.data.dict(), forKey: PrefStore.compatibleVersion)
self.publish(event: self.data)
})
2016-07-26 22:42:30 +03:00
}
}