tutorials/tutorial4/TicTacToe/LoggedIn/LoggedInRouter.swift (49 lines of code) (raw):
//
// Copyright (c) 2017. Uber Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import RIBs
protocol LoggedInInteractable: Interactable, OffGameListener, GameListener {
var router: LoggedInRouting? { get set }
var listener: LoggedInListener? { get set }
}
protocol LoggedInViewControllable: ViewControllable {
func replaceModal(viewController: ViewControllable?)
}
final class LoggedInRouter: Router<LoggedInInteractable>, LoggedInRouting {
init(interactor: LoggedInInteractable,
viewController: LoggedInViewControllable,
offGameBuilder: OffGameBuildable) {
self.viewController = viewController
self.offGameBuilder = offGameBuilder
super.init(interactor: interactor)
interactor.router = self
}
// MARK: - LoggedInRouting
func cleanupViews() {
if currentChild != nil {
viewController.replaceModal(viewController: nil)
}
}
func routeToOffGame(with games: [Game]) {
detachCurrentChild()
attachOffGame(with: games)
}
func routeToGame(with gameBuilder: GameBuildable) {
detachCurrentChild()
let game = gameBuilder.build(withListener: interactor)
self.currentChild = game
attachChild(game)
viewController.replaceModal(viewController: game.viewControllable)
}
// MARK: - Private
private let viewController: LoggedInViewControllable
private let offGameBuilder: OffGameBuildable
private var currentChild: ViewableRouting?
private func attachOffGame(with games: [Game]) {
let offGame = offGameBuilder.build(withListener: interactor, games: games)
self.currentChild = offGame
attachChild(offGame)
viewController.replaceModal(viewController: offGame.viewControllable)
}
private func detachCurrentChild() {
if let currentChild = currentChild {
detachChild(currentChild)
viewController.replaceModal(viewController: nil)
}
}
}