Skip to content

Commit

Permalink
Fix displayed to not assume UIViewController starts not displayed (#…
Browse files Browse the repository at this point in the history
…192)

* Fix `displayed` to not assume UIViewController starts not displayed

* Update changelog
  • Loading branch information
mgray88 authored Sep 6, 2023
1 parent 7fcee7f commit 2cba1e0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
** *Unreleased* **:
- fix: `displayed` and `rxVisible` now do not assume UIViewController starts not visible

** Version 2.13.0 **:

- fix: use xcframeworks for RxFlow/RxFlowDemo deps to please Carthage
Expand Down
5 changes: 3 additions & 2 deletions RxFlow/Extensions/Reactive+UIViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public extension Reactive where Base: UIViewController {
var displayed: Observable<Bool> {
let viewDidAppearObservable = self.sentMessage(#selector(Base.viewDidAppear)).map { _ in true }
let viewDidDisappearObservable = self.sentMessage(#selector(Base.viewDidDisappear)).map { _ in false }
// a UIViewController is at first not displayed
let initialState = Observable.just(false)
let initialState = Observable.deferred {
.just(base.viewIfLoaded?.window != nil)
}
// future calls to viewDidAppear and viewDidDisappear will change the displayable state
return initialState.concat(Observable<Bool>.merge(viewDidAppearObservable, viewDidDisappearObservable))
}
Expand Down
24 changes: 24 additions & 0 deletions RxFlowTests/UIViewController+PresentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ final class UIViewController_PresentableTests: XCTestCase {
}
}

func testUIViewControllerVisibleStartsVisible() {
// Given: a UIViewController that starts "displayed"
let window = UIWindow()
let viewController = TestUIViewController()
_ = viewController.view
window.rootViewController = viewController
window.makeKeyAndVisible()
let testScheduler = TestScheduler(initialClock: 0)
let observer = testScheduler.createObserver(Bool.self)
testScheduler.start()

// When: subscribing to rxVisible
_ = viewController.rxVisible.asObservable().take(until: self.rx.deallocating).bind(to: observer)

// Then: rxVisible emits the first value as true
let referenceVisible = [true]
XCTAssertEqual(observer.events.count, 1)
var index = 0
referenceVisible.forEach {
XCTAssertEqual(observer.events[index].value.element, $0)
index += 1
}
}

func testUIViewControllerDismissed() {
// Given: a UIViewController
let viewController = TestUIViewController()
Expand Down

0 comments on commit 2cba1e0

Please sign in to comment.