From b529c3405e1215a2aafd9217ded2dcf148f4f6af Mon Sep 17 00:00:00 2001 From: Rui Peres Date: Tue, 5 Apr 2016 11:28:14 +0100 Subject: [PATCH 1/2] Added UITextView text's producer --- Rex.xcodeproj/project.pbxproj | 8 ++++++++ Source/UIKit/UITextView.swift | 24 ++++++++++++++++++++++++ Tests/UIKit/UITextViewTests.swift | 29 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 Source/UIKit/UITextView.swift create mode 100644 Tests/UIKit/UITextViewTests.swift diff --git a/Rex.xcodeproj/project.pbxproj b/Rex.xcodeproj/project.pbxproj index 203c3be..adc65d3 100644 --- a/Rex.xcodeproj/project.pbxproj +++ b/Rex.xcodeproj/project.pbxproj @@ -20,6 +20,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 */; }; + 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, ); }; }; D8003EB41AFEC6B000D7D3C5 /* ReactiveCocoa.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = D8003EAD1AFEC68A00D7D3C5 /* ReactiveCocoa.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; D8003EB51AFEC6B000D7D3C5 /* Result.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = D8003EAE1AFEC68A00D7D3C5 /* Result.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -184,6 +186,8 @@ 9DA915A51CA63046003723B9 /* UIDatePickerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIDatePickerTests.swift; sourceTree = ""; }; C7932E811C4B3EDB00086F3C /* UITextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextField.swift; sourceTree = ""; }; C7932E851C4B420A00086F3C /* UITextFieldTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextFieldTests.swift; sourceTree = ""; }; + C7DCE2B21CB3C872001217D8 /* UITextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextView.swift; sourceTree = ""; }; + C7DCE2B51CB3C9C1001217D8 /* UITextViewTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UITextViewTests.swift; sourceTree = ""; }; D8003E921AFEC3D400D7D3C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; D8003E931AFEC3D400D7D3C5 /* Rex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Rex.h; sourceTree = ""; }; D8003E9F1AFEC3D400D7D3C5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -394,6 +398,7 @@ 8289A2E21BD7EF740097FB60 /* UIImageView.swift */, D86FFBD71B34B242001A89B3 /* UILabel.swift */, C7932E811C4B3EDB00086F3C /* UITextField.swift */, + C7DCE2B21CB3C872001217D8 /* UITextView.swift */, 8289A2E41BD7F6DD0097FB60 /* UIView.swift */, ); path = UIKit; @@ -430,6 +435,7 @@ 8289A2E01BD7EF1F0097FB60 /* UIImageViewTests.swift */, D8F073141B861B3A0047D546 /* UILabelTests.swift */, C7932E851C4B420A00086F3C /* UITextFieldTests.swift */, + C7DCE2B51CB3C9C1001217D8 /* UITextViewTests.swift */, 8289A2E61BD7F7730097FB60 /* UIViewTests.swift */, ); path = UIKit; @@ -774,6 +780,7 @@ D8E4A6201B7BBB1600EAD8A8 /* UIBarButtonItem.swift in Sources */, D8F097451B17F3C8002E15BA /* NSObject.swift in Sources */, D834572E1AFEE45B0070616A /* SignalProducer.swift in Sources */, + C7DCE2B41CB3C89A001217D8 /* UITextView.swift in Sources */, 8289A2E51BD7F6DD0097FB60 /* UIView.swift in Sources */, D86FFBD61B34B116001A89B3 /* UIControl.swift in Sources */, D8F0973F1B17F31E002E15BA /* NSData.swift in Sources */, @@ -797,6 +804,7 @@ D83457411AFEE6050070616A /* SignalTests.swift in Sources */, 8295FD8D1B87374A007C9000 /* UIBarButtonItemTests.swift in Sources */, 8295FD871B87309F007C9000 /* UIControlTests.swift in Sources */, + C7DCE2B71CB3C9D6001217D8 /* UITextViewTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Source/UIKit/UITextView.swift b/Source/UIKit/UITextView.swift new file mode 100644 index 0000000..716f767 --- /dev/null +++ b/Source/UIKit/UITextView.swift @@ -0,0 +1,24 @@ +// +// UITextView.swift +// Rex +// +// Created by Rui Peres on 05/04/2016. +// Copyright © 2016 Neil Pankey. All rights reserved. +// + +import ReactiveCocoa +import UIKit +import enum Result.NoError + +extension UITextView { + + /// Sends the textView's string value whenever it changes. + public var rex_textSignal: SignalProducer { + return NSNotificationCenter.defaultCenter() + .rac_notifications(UITextViewTextDidChangeNotification, object: self) + .filterMap { notification in + guard let textView = notification.object as? UITextView else { return nil} + return textView.text + } + } +} diff --git a/Tests/UIKit/UITextViewTests.swift b/Tests/UIKit/UITextViewTests.swift new file mode 100644 index 0000000..9786cb4 --- /dev/null +++ b/Tests/UIKit/UITextViewTests.swift @@ -0,0 +1,29 @@ +// +// UITextViewTests.swift +// Rex +// +// Created by Rui Peres on 05/04/2016. +// Copyright © 2016 Neil Pankey. All rights reserved. +// + +import ReactiveCocoa +import UIKit +import XCTest + +class UITextViewTests: XCTestCase { + + func testTextProperty() { + let expectation = self.expectationWithDescription("Expected textSignal's value to equal to the textViews's text") + defer { self.waitForExpectationsWithTimeout(2, handler: nil) } + + let textView = UITextView(frame: CGRectZero) + textView.text = "Test" + + textView.rex_textSignal.startWithNext { text in + XCTAssertEqual(text, textView.text) + expectation.fulfill() + } + + NSNotificationCenter.defaultCenter().postNotificationName(UITextViewTextDidChangeNotification, object: textView) + } +} From 5bc779329682857cc49d68b2b4e5faf80ef456d3 Mon Sep 17 00:00:00 2001 From: Rui Peres Date: Fri, 8 Apr 2016 00:47:22 +0100 Subject: [PATCH 2/2] Simplified logic --- Source/UIKit/UITextField.swift | 5 +---- Source/UIKit/UITextView.swift | 5 +---- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/Source/UIKit/UITextField.swift b/Source/UIKit/UITextField.swift index 9833399..407aede 100644 --- a/Source/UIKit/UITextField.swift +++ b/Source/UIKit/UITextField.swift @@ -17,9 +17,6 @@ extension UITextField { public var rex_textSignal: SignalProducer { return NSNotificationCenter.defaultCenter() .rac_notifications(UITextFieldTextDidChangeNotification, object: self) - .filterMap { notification in - guard let textField = notification.object as? UITextField else { return nil} - return textField.text - } + .filterMap { ($0.object as? UITextField)?.text } } } diff --git a/Source/UIKit/UITextView.swift b/Source/UIKit/UITextView.swift index 716f767..3315749 100644 --- a/Source/UIKit/UITextView.swift +++ b/Source/UIKit/UITextView.swift @@ -16,9 +16,6 @@ extension UITextView { public var rex_textSignal: SignalProducer { return NSNotificationCenter.defaultCenter() .rac_notifications(UITextViewTextDidChangeNotification, object: self) - .filterMap { notification in - guard let textView = notification.object as? UITextView else { return nil} - return textView.text - } + .filterMap { ($0.object as? UITextView)?.text } } }