1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-24 03:25:03 +03:00

GH-405 Store prefs directly after changing

This commit is contained in:
Tae Won Ha 2017-04-02 17:15:30 +02:00
parent 80d7c02571
commit 380c3ac61f
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
3 changed files with 44 additions and 12 deletions

View File

@ -22,6 +22,7 @@ class Context {
let openQuicklyReducer = OpenQuicklyReducer()
let previewReducer = PreviewReducer(baseServerUrl: baseServerUrl)
let prefService = PrefService()
let previewService = PreviewService()
// For clean quit
@ -45,7 +46,7 @@ class Context {
.reduce(by: UiRootReducer())
.reduce(by: openQuicklyReducer.forMainWindow)
.filter { $0.modified }
.apply(to: PrefService())
.apply(to: prefService.forMainWindow)
.map { $0.state },
actionSource
.mapOmittingNil { $0 as? FileMonitor.Action }
@ -130,6 +131,7 @@ class Context {
.map { $0.state }
)
.merge()
.apply(to: prefService.forPrefPanes)
.subscribe(onNext: self.emitAppState)
.addDisposableTo(self.disposeBag)
@ -213,4 +215,8 @@ extension Observable {
fileprivate func apply<S:Service>(to service: S) -> Observable<Element> where S.Pair == Element {
return self.do(onNext: service.apply)
}
fileprivate func apply<S:StateService>(to service: S) -> Observable<Element> where S.StateType == Element {
return self.do(onNext: service.apply)
}
}

View File

@ -9,19 +9,38 @@ import RxSwift
fileprivate let defaults = UserDefaults.standard
class PrefService: Service {
typealias Pair = StateActionPair<AppState, UuidAction<MainWindow.Action>>
class PrefService {
static let compatibleVersion = "168"
static let lastCompatibleVersion = "128"
func apply(_ pair: Pair) {
guard case .close = pair.action.payload else {
return
}
NSLog("Saving pref!")
defaults.setValue(pair.state.dict(), forKey: PrefService.compatibleVersion)
}
let forMainWindow = PrefMainWindowService()
let forPrefPanes = PrefPaneService()
}
extension PrefService {
class PrefMainWindowService: Service {
typealias Pair = StateActionPair<AppState, UuidAction<MainWindow.Action>>
func apply(_ pair: Pair) {
guard case .close = pair.action.payload else {
return
}
NSLog("Saving pref!")
defaults.setValue(pair.state.dict(), forKey: PrefService.compatibleVersion)
}
}
class PrefPaneService: StateService {
typealias StateType = AppState
func apply(_ state: StateType) {
NSLog("Saving pref!")
defaults.setValue(state.dict(), forKey: PrefService.compatibleVersion)
}
}
}

View File

@ -108,6 +108,13 @@ protocol Service {
func apply(_: Pair)
}
protocol StateService {
associatedtype StateType
func apply(_: StateType)
}
protocol UiComponent {
associatedtype StateType