Add ability to disallow the beginning of text editing for ChatInputBar.

This commit is contained in:
Igor Kashkuta 2016-04-14 17:32:41 +01:00
parent 9c04a00048
commit 4d4fbed64d
2 changed files with 44 additions and 0 deletions

View File

@ -25,6 +25,7 @@
import UIKit
public protocol ChatInputBarDelegate: class {
func inputBarShouldBeginTextEditing(inputBar: ChatInputBar) -> Bool
func inputBarDidBeginEditing(inputBar: ChatInputBar)
func inputBarDidEndEditing(inputBar: ChatInputBar)
func inputBarDidChangeText(inputBar: ChatInputBar)
@ -210,6 +211,11 @@ extension ChatInputBar { // Tabar
// MARK: UITextViewDelegate
extension ChatInputBar: UITextViewDelegate {
public func textViewShouldBeginEditing(textView: UITextView) -> Bool {
guard let sDelegate = self.delegate else { return true }
return sDelegate.inputBarShouldBeginTextEditing(self)
}
public func textViewDidEndEditing(textView: UITextView) {
self.presenter?.onDidEndEditing()
self.delegate?.inputBarDidEndEditing(self)

View File

@ -49,6 +49,15 @@ class ChatInputBarTests: XCTestCase {
return itemView
}
private func simulateTapOnTextViewForDelegate(textViewDelegate: UITextViewDelegate) {
let dummyTextView = UITextView()
if let shouldBeginEditing = textViewDelegate.textViewShouldBeginEditing
where !shouldBeginEditing(dummyTextView) {
return
}
textViewDelegate.textViewDidBeginEditing?(dummyTextView)
}
func testThat_WhenInputTextChanged_BarEnablesSendButton() {
self.bar.sendButton.enabled = false
self.bar.inputText = "!"
@ -153,6 +162,28 @@ class ChatInputBarTests: XCTestCase {
XCTAssertTrue(closureCalled)
XCTAssertFalse(self.bar.sendButton.enabled)
}
func testThat_WhenTextViewGoingToBecomeEditable_ItBecomesEditableByDefault() {
self.setupPresenter()
self.simulateTapOnTextViewForDelegate(self.bar)
XCTAssertTrue(self.presenter.onDidBeginEditingCalled)
}
func testThat_WhenTextViewGoingToBecomeEditableAndDelegateAllowsIt_ItWillBeEditable() {
self.setupDelegate()
self.delegate.inputBarShouldBeginTextEditingResult = true
self.simulateTapOnTextViewForDelegate(self.bar)
XCTAssertTrue(self.delegate.inputBarShouldBeginTextEditingCalled)
XCTAssertTrue(self.delegate.inputBarDidBeginEditingCalled)
}
func testThat_WhenTextViewGoingToBecomeEditableAndDelegateDisallowsIt_ItWontBeEditable() {
self.setupDelegate()
self.delegate.inputBarShouldBeginTextEditingResult = false
self.simulateTapOnTextViewForDelegate(self.bar)
XCTAssertTrue(self.delegate.inputBarShouldBeginTextEditingCalled)
XCTAssertFalse(self.delegate.inputBarDidBeginEditingCalled)
}
}
class FakeChatInputBarPresenter: ChatInputBarPresenter {
@ -186,6 +217,13 @@ class FakeChatInputBarPresenter: ChatInputBarPresenter {
}
class FakeChatInputBarDelegate: ChatInputBarDelegate {
var inputBarShouldBeginTextEditingCalled = false
var inputBarShouldBeginTextEditingResult = true
func inputBarShouldBeginTextEditing(inputBar: ChatInputBar) -> Bool {
self.inputBarShouldBeginTextEditingCalled = true
return self.inputBarShouldBeginTextEditingResult
}
var inputBarDidBeginEditingCalled = false
func inputBarDidBeginEditing(inputBar: ChatInputBar) {
self.inputBarDidBeginEditingCalled = true