Skip to content

Commit

Permalink
Merge pull request #3 from BrianSemiglia/driver-improvements
Browse files Browse the repository at this point in the history
Driver improvements
  • Loading branch information
BrianSemiglia authored Mar 7, 2017
2 parents 2b0e093 + b60a4ba commit 28d1783
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 83 deletions.
28 changes: 11 additions & 17 deletions Cycle/Classes/Cycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ open class CycledApplicationDelegate<T: SinkSourceConverting>: UIResponder, UIAp
_ application: UIApplication,
willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil
) -> Bool {
window = UIWindow(frame: UIScreen.main.fixedCoordinateSpace.bounds, root: cycle.root)
window = UIWindow(frame: UIScreen.main.bounds, root: cycle.root)
window?.makeKeyAndVisible()
return cycle.delegate.application!(
application,
Expand All @@ -48,22 +48,25 @@ extension UIWindow {
}

public final class Cycle<E: SinkSourceConverting> {
fileprivate var events: Observable<E.Source>?
fileprivate var eventsProxy: ReplaySubject<E.Source>?
fileprivate let cleanup = DisposeBag()
private var events: Observable<E.Source>?
private var eventsProxy: ReplaySubject<E.Source>?
private let cleanup = DisposeBag()
fileprivate let delegate: UIApplicationDelegate
fileprivate let root: UIViewController
public required init(transformer: E) {
eventsProxy = ReplaySubject.create(
bufferSize: 1
)
let drivers = E.Drivers()
let drivers = transformer.driversFrom(initial: E.Source())
root = drivers.screen.root
delegate = drivers.application
events = transformer.effectsFrom(
events: eventsProxy!,
drivers: drivers
)
// `.startWith` is redundant, but necessary to kickoff cycle
// Possibly removed if `events` was BehaviorSubject?
// Not sure how to `merge` observables to single BehaviorSubject though.
events?
.startWith(E.Source())
.subscribe { [weak self] in
Expand All @@ -74,12 +77,11 @@ public final class Cycle<E: SinkSourceConverting> {

public protocol SinkSourceConverting {
associatedtype Source: Initializable
associatedtype Drivers: CycleDrivable
associatedtype Drivers: UIApplicationDelegateProviding, ScreenDrivable
func driversFrom(initial: Source) -> Drivers
func effectsFrom(events: Observable<Source>, drivers: Drivers) -> Observable<Source>
}

public protocol CycleDrivable: Initializable, UIApplicationProviding, ScreenDrivable {}

public protocol Initializable {
init()
}
Expand All @@ -93,15 +95,7 @@ public protocol UIViewControllerProviding {
var root: UIViewController { get }
}

public protocol UIApplicationProviding {
public protocol UIApplicationDelegateProviding {
associatedtype Delegate: UIApplicationDelegate
var application: Delegate { get }
}

extension UIViewController {
public static var empty: UIViewController {
let x = UIViewController()
x.view.backgroundColor = .white
return x
}
}
12 changes: 9 additions & 3 deletions Example/Cycle/Integer Mutation/IntegerMutatingApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ struct IntegerMutatingApp: SinkSourceConverting {
var screen = ValueToggler.Model.empty
var application = RxUIApplication.Model.empty
}
struct Drivers: CycleDrivable {
let screen = ValueToggler()
let application = RxUIApplication(initial: .empty)
struct Drivers: UIApplicationDelegateProviding, ScreenDrivable {
let screen: ValueToggler
let application: RxUIApplication
}
func driversFrom(initial: IntegerMutatingApp.Model) -> IntegerMutatingApp.Drivers { return
Drivers(
screen: ValueToggler(),
application: RxUIApplication(initial: initial.application)
)
}
func effectsFrom(events: Observable<Model>, drivers: Drivers) -> Observable<Model> {
let value = drivers.screen
Expand Down
13 changes: 9 additions & 4 deletions Example/Cycle/Integer Mutation/ValueToggler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import CoreLocation //
import Cycle

class ValueToggler: UIViewControllerProviding {

static var shared = ValueToggler()


struct Model {
struct Button {
enum State {
Expand Down Expand Up @@ -57,7 +55,6 @@ class ValueToggler: UIViewControllerProviding {
)

var cleanup = DisposeBag()
var input: Observable<Model>?
let root = UIViewController.empty

init() {
Expand Down Expand Up @@ -120,3 +117,11 @@ extension ValueToggler.Model {
)
}
}

extension UIViewController {
public static var empty: UIViewController {
let x = UIViewController()
x.view.backgroundColor = .white
return x
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ struct PushNotificationRegistration: SinkSourceConverting {
struct Model: Initializable {
var application = RxUIApplication.Model.empty
}
struct Drivers: CycleDrivable {
let screen = ScreenDriver()
let application = RxUIApplication(initial: .empty)
struct Drivers: UIApplicationDelegateProviding, ScreenDrivable {
let screen: ScreenDriver
let application: RxUIApplication
}
func driversFrom(initial: PushNotificationRegistration.Model) -> PushNotificationRegistration.Drivers { return
Drivers(
screen: ScreenDriver(),
application: RxUIApplication(initial: initial.application)
)
}
func effectsFrom(events: Observable<Model>, drivers: Drivers) -> Observable<Model> { return
drivers.application
Expand Down
15 changes: 11 additions & 4 deletions Example/Cycle/Shortcut Items/ShortcutItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ struct ShortcutActionsExample: SinkSourceConverting {
var application = RxUIApplication.Model.empty
var async = Timer.Model.empty
}
struct Drivers: CycleDrivable {
let screen = ScreenDriver()
let timer = Timer(.empty)
let application = RxUIApplication(initial: .empty)
struct Drivers: UIApplicationDelegateProviding, ScreenDrivable {
let screen: ScreenDriver
let timer: Timer
let application: RxUIApplication
}
func driversFrom(initial: ShortcutActionsExample.Model) -> ShortcutActionsExample.Drivers {
return Drivers(
screen: ScreenDriver(),
timer: Timer(initial.async),
application: RxUIApplication(initial: initial.application)
)
}
func effectsFrom(events: Observable<Model>, drivers: Drivers) -> Observable<Model> {

Expand Down
12 changes: 9 additions & 3 deletions Example/Cycle/URLActionOutgoing/URLActionOutgoing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ struct URLActionOutgoing: SinkSourceConverting {
struct Model: Initializable {
var application = RxUIApplication.Model.empty
}
struct Drivers: CycleDrivable {
let screen = ScreenDriver()
let application = RxUIApplication(initial: .empty)
struct Drivers: UIApplicationDelegateProviding, ScreenDrivable {
let screen: ScreenDriver
let application: RxUIApplication
}
func driversFrom(initial: URLActionOutgoing.Model) -> URLActionOutgoing.Drivers {
return Drivers(
screen: ScreenDriver(),
application: RxUIApplication(initial: initial.application)
)
}
func effectsFrom(events: Observable<Model>, drivers: Drivers) -> Observable<Model> { return
drivers.application
Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Changeset (2.1)
- Cycle (0.0.2):
- Cycle (0.0.4):
- Changeset (= 2.1)
- RxSwift (= 3.2.0)
- RxCocoa (3.2.0):
Expand All @@ -17,7 +17,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Changeset: 52ae0451caae6d236d8ecee150ab62050c3dc826
Cycle: 8bb6fe112af27e9ed78eecabf0c739bb0ea8d6eb
Cycle: 10dab4653adb1968be054c46f660521b0ca56f94
RxCocoa: ccdf43101a70407097a29082f648ba1676075b30
RxSwift: 46574f70d416b7923c237195939cc488a7fbf3a0

Expand Down
14 changes: 10 additions & 4 deletions Example/Tests/RxUIApplicationTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1503,17 +1503,23 @@ class RxUIApplicationTestCase: XCTestCase {
}

class RxUIApplicationCycle: SinkSourceConverting {
struct Drivers: CycleDrivable {
let screen = ScreenDriverStub()
let application = RxUIApplication(initial: .empty)
}
struct DriverModels: Initializable {
var application = RxUIApplication.Model.empty
}
struct Drivers: UIApplicationDelegateProviding, ScreenDrivable {
let screen: ScreenDriverStub
let application: RxUIApplication
}
let filter: (Observable<DriverModels>, RxUIApplication) -> Observable<DriverModels>
init(filter: @escaping (Observable<DriverModels>, RxUIApplication) -> Observable<DriverModels>) {
self.filter = filter
}
func driversFrom(initial: RxUIApplicationTestCase.RxUIApplicationCycle.DriverModels) -> RxUIApplicationTestCase.RxUIApplicationCycle.Drivers {
return Drivers(
screen: ScreenDriverStub(),
application: RxUIApplication(initial: initial.application)
)
}
func effectsFrom(events: Observable<DriverModels>, drivers: Drivers) -> Observable<DriverModels> { return
filter(events, drivers.application)
}
Expand Down
Loading

0 comments on commit 28d1783

Please sign in to comment.