Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix displayed to not assume UIViewController starts not displayed #192

Merged
merged 2 commits into from
Sep 6, 2023
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
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