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

VC's did/will (dis)appear #100

Merged
merged 3 commits into from
Apr 18, 2016
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
8 changes: 8 additions & 0 deletions Rex.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
C7932E831C4B3F3000086F3C /* UITextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7932E811C4B3EDB00086F3C /* UITextField.swift */; };
C7932E841C4B41E100086F3C /* UITextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7932E811C4B3EDB00086F3C /* UITextField.swift */; };
C7932E871C4B42F500086F3C /* UITextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7932E851C4B420A00086F3C /* UITextFieldTests.swift */; };
C7945F111CC192E800DC9E37 /* UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7945F101CC192E800DC9E37 /* UIViewController.swift */; };
C7945F141CC1DFBE00DC9E37 /* UIViewControllerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7945F121CC1DFB400DC9E37 /* UIViewControllerTests.swift */; };
C7DCE2B41CB3C89A001217D8 /* UITextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7DCE2B21CB3C872001217D8 /* UITextView.swift */; };
C7DCE2B71CB3C9D6001217D8 /* UITextViewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7DCE2B51CB3C9C1001217D8 /* UITextViewTests.swift */; };
D8003E941AFEC3D400D7D3C5 /* Rex.h in Headers */ = {isa = PBXBuildFile; fileRef = D8003E931AFEC3D400D7D3C5 /* Rex.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -195,6 +197,8 @@
C72CF3E41CBF188A00E19897 /* RACSignal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RACSignal.swift; sourceTree = "<group>"; };
C7932E811C4B3EDB00086F3C /* UITextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextField.swift; sourceTree = "<group>"; };
C7932E851C4B420A00086F3C /* UITextFieldTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextFieldTests.swift; sourceTree = "<group>"; };
C7945F101CC192E800DC9E37 /* UIViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewController.swift; sourceTree = "<group>"; };
C7945F121CC1DFB400DC9E37 /* UIViewControllerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIViewControllerTests.swift; sourceTree = "<group>"; };
C7DCE2B21CB3C872001217D8 /* UITextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextView.swift; sourceTree = "<group>"; };
C7DCE2B51CB3C9C1001217D8 /* UITextViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextViewTests.swift; sourceTree = "<group>"; };
D8003E921AFEC3D400D7D3C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -411,6 +415,7 @@
C7932E811C4B3EDB00086F3C /* UITextField.swift */,
C7DCE2B21CB3C872001217D8 /* UITextView.swift */,
8289A2E41BD7F6DD0097FB60 /* UIView.swift */,
C7945F101CC192E800DC9E37 /* UIViewController.swift */,
);
path = UIKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -449,6 +454,7 @@
C7932E851C4B420A00086F3C /* UITextFieldTests.swift */,
C7DCE2B51CB3C9C1001217D8 /* UITextViewTests.swift */,
8289A2E61BD7F7730097FB60 /* UIViewTests.swift */,
C7945F121CC1DFB400DC9E37 /* UIViewControllerTests.swift */,
);
path = UIKit;
sourceTree = "<group>";
Expand Down Expand Up @@ -794,6 +800,7 @@
D8A454071BD26A1A00C9E790 /* Property.swift in Sources */,
D8E4A6201B7BBB1600EAD8A8 /* UIBarButtonItem.swift in Sources */,
D8F097451B17F3C8002E15BA /* NSObject.swift in Sources */,
C7945F111CC192E800DC9E37 /* UIViewController.swift in Sources */,
D834572E1AFEE45B0070616A /* SignalProducer.swift in Sources */,
C7DCE2B41CB3C89A001217D8 /* UITextView.swift in Sources */,
8289A2E51BD7F6DD0097FB60 /* UIView.swift in Sources */,
Expand All @@ -815,6 +822,7 @@
C7932E871C4B42F500086F3C /* UITextFieldTests.swift in Sources */,
8295FD8A1B87352D007C9000 /* UIButtonTests.swift in Sources */,
D8A4540A1BD2772700C9E790 /* PropertyTests.swift in Sources */,
C7945F141CC1DFBE00DC9E37 /* UIViewControllerTests.swift in Sources */,
9DA915A61CA63046003723B9 /* UIDatePickerTests.swift in Sources */,
D83457301AFEE45E0070616A /* SignalProducerTests.swift in Sources */,
D83457411AFEE6050070616A /* SignalTests.swift in Sources */,
Expand Down
43 changes: 43 additions & 0 deletions Source/UIKit/UIViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UIViewController.swift
// Rex
//
// Created by Rui Peres on 14/04/2016.
// Copyright © 2016 Neil Pankey. All rights reserved.
//

import Result
import ReactiveCocoa
import UIKit

extension UIViewController {
/// Returns a `Signal`, that will be triggered
/// when `self`'s `viewDidDisappear` is called
public var rex_viewDidDisappearSignal: Signal<(), NoError> {
return triggerForSelector(#selector(UIViewController.viewDidDisappear(_:)))
}

/// Returns a `Signal`, that will be triggered
/// when `self`'s `viewWillDisappear` is called
public var rex_viewWillDisappearSignal: Signal<(), NoError> {
return triggerForSelector(#selector(UIViewController.viewWillDisappear(_:)))
}

/// Returns a `Signal`, that will be triggered
/// when `self`'s `viewDidAppear` is called
public var rex_viewDidAppearSignal: Signal<(), NoError> {
return triggerForSelector(#selector(UIViewController.viewDidAppear(_:)))
}

/// Returns a `Signal`, that will be triggered
/// when `self`'s `viewWillAppear` is called
public var rex_viewWillAppearSignal: Signal<(), NoError> {
return triggerForSelector(#selector(UIViewController.viewWillAppear(_:)))
}

private func triggerForSelector(selector: Selector) -> Signal<(), NoError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should triggerForSelector be inside extension of NSObject (or UIResponder) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@335g Looking at UIResponder documentation, I don't think so. I might be missing something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, It might be good in this 👍

return self
.rac_signalForSelector(selector)
.rex_toTriggerSignal()
}
}
82 changes: 82 additions & 0 deletions Tests/UIKit/UIViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// UIViewControllerTests.swift
// Rex
//
// Created by Rui Peres on 16/04/2016.
// Copyright © 2016 Neil Pankey. All rights reserved.
//

import ReactiveCocoa
import UIKit
import XCTest
import enum Result.NoError

class UIViewControllerTests: XCTestCase {

weak var _viewController: UIViewController?

override func tearDown() {
XCTAssert(_viewController == nil, "Retain cycle detected in UIViewController properties")
super.tearDown()
}

func testViewDidDisappear() {

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

let viewController = UIViewController()
_viewController = viewController

viewController.rex_viewDidDisappearSignal.observeNext {
expectation.fulfill()
}

viewController.viewDidDisappear(true)
}

func testViewWillDisappear() {

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

let viewController = UIViewController()
_viewController = viewController

viewController.rex_viewWillDisappearSignal.observeNext {
expectation.fulfill()
}

viewController.viewWillDisappear(true)
}

func testViewDidAppear() {

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

let viewController = UIViewController()
_viewController = viewController

viewController.rex_viewDidAppearSignal.observeNext {
expectation.fulfill()
}

viewController.viewDidAppear(true)
}

func testViewWillAppear() {

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

let viewController = UIViewController()
_viewController = viewController

viewController.rex_viewWillAppearSignal.observeNext {
expectation.fulfill()
}

viewController.viewWillAppear(true)
}
}