1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-11 07:22:23 +03:00
vimr/VimR/AppDelegateReducer.swift

67 lines
1.8 KiB
Swift
Raw Normal View History

2017-01-22 16:22:05 +03:00
/**
* Tae Won Ha - http://taewon.de - @hataewon
* See LICENSE
*/
import Foundation
import RxSwift
2017-03-11 19:08:50 +03:00
class AppDelegateTransformer: Reducer {
2017-01-22 16:22:05 +03:00
typealias Pair = StateActionPair<AppState, AppDelegate.Action>
2017-02-06 20:57:50 +03:00
init(baseServerUrl: URL) {
self.baseServerUrl = baseServerUrl
}
func transform(_ source: Observable<Pair>) -> Observable<Pair> {
return source.map { pair in
var state = pair.state
switch pair.action {
case let .newMainWindow(urls, cwd):
let mainWindow = self.newMainWindow(with: state, urls: urls, cwd: cwd)
2017-01-22 16:22:05 +03:00
state.mainWindows[mainWindow.uuid] = mainWindow
case let .openInKeyWindow(urls, cwd):
guard let uuid = state.currentMainWindowUuid, state.mainWindows[uuid] != nil else {
let mainWindow = self.newMainWindow(with: state, urls: urls, cwd: cwd)
state.mainWindows[mainWindow.uuid] = mainWindow
break
}
state.mainWindows[uuid]?.urlsToOpen.append(Marked(urls.toDict { url in MainWindow.OpenMode.default }))
state.mainWindows[uuid]?.cwd = cwd
2017-02-28 00:45:26 +03:00
case .preferences:
state.preferencesOpen = Marked(true)
case .quitWithoutSaving, .quit:
state.mainWindows.removeAll()
state.quitWhenNoMainWindow = true
}
return StateActionPair(state: state, action: pair.action)
}
}
2017-02-06 20:57:50 +03:00
fileprivate let baseServerUrl: URL
fileprivate func newMainWindow(with state: AppState, urls: [URL], cwd: URL) -> MainWindow.State {
var mainWindow = state.mainWindowTemplate
mainWindow.uuid = UUID().uuidString
mainWindow.root = state.root
let markedUrls = Marked(urls.toDict { url in MainWindow.OpenMode.default })
mainWindow.urlsToOpen.append(markedUrls)
mainWindow.cwd = cwd
mainWindow.preview.server = self.baseServerUrl.appendingPathComponent(PreviewTransformer.nonePath)
return mainWindow
}
}