2017-01-22 16:22:05 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
2017-01-17 21:47:59 +03:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RxSwift
|
|
|
|
|
2017-03-29 21:10:11 +03:00
|
|
|
class AppDelegateReducer: Reducer {
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-01-22 16:22:05 +03:00
|
|
|
typealias Pair = StateActionPair<AppState, AppDelegate.Action>
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-02-06 20:57:50 +03:00
|
|
|
init(baseServerUrl: URL) {
|
|
|
|
self.baseServerUrl = baseServerUrl
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:17:45 +03:00
|
|
|
func reduce(_ source: Observable<Pair>) -> Observable<Pair> {
|
2017-01-17 21:47:59 +03:00
|
|
|
return source.map { pair in
|
2017-02-04 17:34:13 +03:00
|
|
|
var state = pair.state
|
|
|
|
|
2017-01-17 21:47:59 +03:00
|
|
|
switch pair.action {
|
|
|
|
|
|
|
|
case let .newMainWindow(urls, cwd):
|
2017-02-27 22:45:07 +03:00
|
|
|
let mainWindow = self.newMainWindow(with: state, urls: urls, cwd: cwd)
|
2017-01-22 16:22:05 +03:00
|
|
|
state.mainWindows[mainWindow.uuid] = mainWindow
|
2017-01-17 21:47:59 +03:00
|
|
|
|
2017-02-24 00:15:35 +03:00
|
|
|
case let .openInKeyWindow(urls, cwd):
|
2017-02-27 22:45:07 +03:00
|
|
|
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-24 00:15:35 +03:00
|
|
|
|
2017-02-28 00:45:26 +03:00
|
|
|
case .preferences:
|
|
|
|
state.preferencesOpen = Marked(true)
|
|
|
|
|
2017-02-12 12:53:24 +03:00
|
|
|
case .quitWithoutSaving, .quit:
|
2017-02-04 17:34:13 +03:00
|
|
|
state.mainWindows.removeAll()
|
2017-02-12 12:53:24 +03:00
|
|
|
state.quitWhenNoMainWindow = true
|
2017-02-04 17:34:13 +03:00
|
|
|
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
2017-02-12 12:53:24 +03:00
|
|
|
|
|
|
|
return StateActionPair(state: state, action: pair.action)
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 20:57:50 +03:00
|
|
|
|
|
|
|
fileprivate let baseServerUrl: URL
|
2017-02-27 22:45:07 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-03-29 21:10:11 +03:00
|
|
|
mainWindow.preview.server = self.baseServerUrl.appendingPathComponent(PreviewReducer.nonePath)
|
2017-02-27 22:45:07 +03:00
|
|
|
|
|
|
|
return mainWindow
|
|
|
|
}
|
2017-01-17 21:47:59 +03:00
|
|
|
}
|