Skip to content

Commit

Permalink
flow: allow the UIWindow as root view
Browse files Browse the repository at this point in the history
  • Loading branch information
twittemb committed Feb 9, 2018
1 parent ddf0afc commit d048a85
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 13 deletions.
4 changes: 2 additions & 2 deletions RxFlow/Flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public protocol Flow: Presentable {
/// - Returns: the NextFlowItems matching the Step. These NextFlowItems determines the next navigation steps (Presentables to display / Steppers to listen)
func navigate (to step: Step) -> NextFlowItems

/// the UIViewController on which rely the navigation inside this Flow. This method must always give the same instance
var root: UIViewController { get }
/// the Presentable on which rely the navigation inside this Flow. This method must always give the same instance
var root: Presentable { get }
}

extension Flow {
Expand Down
4 changes: 4 additions & 0 deletions RxFlowDemo/RxFlowDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1A25DE81202D3CFE00F1C4B8 /* AppFlow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1A25DE80202D3CFE00F1C4B8 /* AppFlow.swift */; };
1A8FBE101FF845EE00389464 /* Reusable.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A8FBE0D1FF845EE00389464 /* Reusable.framework */; };
1A8FBE111FF845EE00389464 /* RxSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A8FBE0E1FF845EE00389464 /* RxSwift.framework */; };
1A8FBE121FF845EE00389464 /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1A8FBE0F1FF845EE00389464 /* RxCocoa.framework */; };
Expand Down Expand Up @@ -67,6 +68,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
1A25DE80202D3CFE00F1C4B8 /* AppFlow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppFlow.swift; sourceTree = "<group>"; };
1A8FBE0D1FF845EE00389464 /* Reusable.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Reusable.framework; path = Carthage/Build/iOS/Reusable.framework; sourceTree = "<group>"; };
1A8FBE0E1FF845EE00389464 /* RxSwift.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxSwift.framework; path = Carthage/Build/iOS/RxSwift.framework; sourceTree = "<group>"; };
1A8FBE0F1FF845EE00389464 /* RxCocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = RxCocoa.framework; path = Carthage/Build/iOS/RxCocoa.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -142,6 +144,7 @@
isa = PBXGroup;
children = (
1A8FBE221FF8480C00389464 /* DemoStep.swift */,
1A25DE80202D3CFE00F1C4B8 /* AppFlow.swift */,
1A8FBE241FF8480C00389464 /* MainFlow.swift */,
1A8FBE231FF8480C00389464 /* SettingsFlow.swift */,
1A8FBE251FF8480C00389464 /* WatchedFlow.swift */,
Expand Down Expand Up @@ -421,6 +424,7 @@
1A8FBE4A1FF84BB200389464 /* SettingsAboutViewController.swift in Sources */,
1A8FBE701FF84C2C00389464 /* WatchedViewModel.swift in Sources */,
1A8FBE341FF84B5300389464 /* CastsHelper.swift in Sources */,
1A25DE81202D3CFE00F1C4B8 /* AppFlow.swift in Sources */,
1A8FBE351FF84B5300389464 /* MoviesHelper.swift in Sources */,
1A8FBE281FF8480C00389464 /* SettingsFlow.swift in Sources */,
1A8FBE641FF84BFE00389464 /* CastViewModel.swift in Sources */,
Expand Down
10 changes: 3 additions & 7 deletions RxFlowDemo/RxFlowDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var coordinator = Coordinator()
let movieService = MoviesService()
lazy var mainFlow = {
return MainFlow(with: self.movieService)
}()
var appFlow: AppFlow!

func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Expand All @@ -31,11 +29,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
print ("did navigate to flow=\(flow) and step=\(step)")
}).disposed(by: self.disposeBag)

Flows.whenReady(flow1: mainFlow, block: { [unowned window] (root) in
window.rootViewController = root
})
self.appFlow = AppFlow(withWindow: window, andService: self.movieService)

coordinator.coordinate(flow: mainFlow, withStepper: OneStepper(withSingleStep: DemoStep.apiKey))
coordinator.coordinate(flow: self.appFlow, withStepper: OneStepper(withSingleStep: DemoStep.apiKey))

return true
}
Expand Down
68 changes: 68 additions & 0 deletions RxFlowDemo/RxFlowDemo/Flows/AppFlow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// AppFlow.swift
// RxFlowDemo
//
// Created by Thibault Wittemberg on 18-02-08.
// Copyright © 2018 RxSwiftCommunity. All rights reserved.
//

import Foundation
import RxFlow

class AppFlow: Flow {

var root: Presentable {
return self.rootWindow
}

private let rootWindow: UIWindow
private let service: MoviesService

init(withWindow window: UIWindow, andService service: MoviesService) {
self.rootWindow = window
self.service = service
}

func navigate(to step: Step) -> NextFlowItems {
guard let step = step as? DemoStep else { return NextFlowItems.stepNotHandled }

switch step {
case .apiKey:
return navigationToApiScreen()
case .apiKeyIsComplete:
return navigationToDashboardScreen()
default:
return NextFlowItems.stepNotHandled
}

}

private func navigationToApiScreen () -> NextFlowItems {
let settingsViewController = SettingsViewController.instantiate()
self.rootWindow.rootViewController = settingsViewController
return NextFlowItems.one(flowItem: NextFlowItem(nextPresentable: settingsViewController, nextStepper: settingsViewController))
}

private func navigationToDashboardScreen () -> NextFlowItems {
let tabbarController = UITabBarController()
let wishlistStepper = WishlistStepper()
let wishListFlow = WishlistFlow(withService: self.service, andStepper: wishlistStepper)
let watchedFlow = WatchedFlow(withService: self.service)

Flows.whenReady(flow1: wishListFlow, flow2: watchedFlow, block: { [unowned self] (root1: UINavigationController, root2: UINavigationController) in
let tabBarItem1 = UITabBarItem(title: "Wishlist", image: UIImage(named: "wishlist"), selectedImage: nil)
let tabBarItem2 = UITabBarItem(title: "Watched", image: UIImage(named: "watched"), selectedImage: nil)
root1.tabBarItem = tabBarItem1
root1.title = "Wishlist"
root2.tabBarItem = tabBarItem2
root2.title = "Watched"

tabbarController.setViewControllers([root1, root2], animated: false)
self.rootWindow.rootViewController = tabbarController
})

return NextFlowItems.multiple(flowItems: [NextFlowItem(nextPresentable: wishListFlow, nextStepper: wishlistStepper),
NextFlowItem(nextPresentable: watchedFlow, nextStepper: OneStepper(withSingleStep: DemoStep.movieList))])
}

}
2 changes: 1 addition & 1 deletion RxFlowDemo/RxFlowDemo/Flows/MainFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RxSwift

class MainFlow: Flow {

var root: UIViewController {
var root: Presentable {
return self.rootViewController
}

Expand Down
2 changes: 1 addition & 1 deletion RxFlowDemo/RxFlowDemo/Flows/SettingsFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import RxSwift

class SettingsFlow: Flow {

var root: UIViewController {
var root: Presentable {
return self.rootViewController
}

Expand Down
2 changes: 1 addition & 1 deletion RxFlowDemo/RxFlowDemo/Flows/WatchedFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import UIKit

class WatchedFlow: Flow {

var root: UIViewController {
var root: Presentable {
return self.rootViewController
}

Expand Down
2 changes: 1 addition & 1 deletion RxFlowDemo/RxFlowDemo/Flows/WishlistFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import UIKit

class WishlistFlow: Flow {

var root: UIViewController {
var root: Presentable {
return self.rootViewController
}

Expand Down

0 comments on commit d048a85

Please sign in to comment.