Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion Source/UIKit/UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,39 @@ extension UIViewController {
.rac_signalForSelector(selector)
.rex_toTriggerSignal()
}
}

public typealias DismissingCompletion = (Void -> Void)?
public typealias DismissingInformation = (animated: Bool, completion: DismissingCompletion)?

/// Wraps a viewController's `dismissViewControllerAnimated` function in a bindable property.
/// It mimics the same input as `dismissViewControllerAnimated`: a `Bool` flag for the animation
/// and a `(Void -> Void)?` closure for `completion`.
/// E.g:
/// ```
/// //Dismissed with animation (`true`) and `nil` completion
/// viewController.rex_dismissAnimated <~ aProducer.map { _ in (true, nil) }
/// ```
/// The dismissal observation can be made either with binding (example above)
/// or `viewController.dismissViewControllerAnimated(true, completion: nil)`
public var rex_dismissAnimated: MutableProperty<DismissingInformation> {

let initial: UIViewController -> DismissingInformation = { _ in nil }
let setter: (UIViewController, DismissingInformation) -> Void = { host, dismissingInfo in

guard let unwrapped = dismissingInfo else { return }
host.dismissViewControllerAnimated(unwrapped.animated, completion: unwrapped.completion)
}

let property = associatedProperty(self, key: &dismissModally, initial: initial, setter: setter)

property <~ rac_signalForSelector(#selector(UIViewController.dismissViewControllerAnimated(_:completion:)))
.takeUntilBlock { _ in property.value != nil }
.rex_toTriggerSignal()
.map { _ in return nil }


return property
}
}

private var dismissModally: UInt8 = 0
30 changes: 30 additions & 0 deletions Tests/UIKit/UIViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,34 @@ class UIViewControllerTests: XCTestCase {

viewController.viewWillAppear(true)
}

func testDismissViewController_via_property() {

let expectation = self.expectationWithDescription("Expected rex_dismissModally to be triggered")
defer { self.waitForExpectationsWithTimeout(2, handler: nil) }

let viewController = UIViewController()
_viewController = viewController

viewController.rex_dismissAnimated.signal.observeNext { _ in
expectation.fulfill()
}

viewController.rex_dismissAnimated <~ SignalProducer(value: (animated: true, completion: nil))
}

func testDismissViewController_via_cocoaDismiss() {

let expectation = self.expectationWithDescription("Expected rex_dismissModally to be triggered")
defer { self.waitForExpectationsWithTimeout(2, handler: nil) }

let viewController = UIViewController()
_viewController = viewController

viewController.rex_dismissAnimated.signal.observeNext { _ in
expectation.fulfill()
}

viewController.dismissViewControllerAnimated(true, completion: nil)
}
}