diff --git a/VimR/Context.swift b/VimR/Context.swift index 3a2d7d5a..cc4cd60a 100644 --- a/VimR/Context.swift +++ b/VimR/Context.swift @@ -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(to service: S) -> Observable where S.Pair == Element { return self.do(onNext: service.apply) } + + fileprivate func apply(to service: S) -> Observable where S.StateType == Element { + return self.do(onNext: service.apply) + } } diff --git a/VimR/PrefService.swift b/VimR/PrefService.swift index b92c7bf2..53254b3d 100644 --- a/VimR/PrefService.swift +++ b/VimR/PrefService.swift @@ -9,19 +9,38 @@ import RxSwift fileprivate let defaults = UserDefaults.standard -class PrefService: Service { - - typealias Pair = StateActionPair> +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> + + 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) + } + } +} \ No newline at end of file diff --git a/VimR/Types.swift b/VimR/Types.swift index ebbd5d8a..e7a37605 100644 --- a/VimR/Types.swift +++ b/VimR/Types.swift @@ -108,6 +108,13 @@ protocol Service { func apply(_: Pair) } +protocol StateService { + + associatedtype StateType + + func apply(_: StateType) +} + protocol UiComponent { associatedtype StateType