Add ability to control Send button enabled state from external code.

This commit is contained in:
Igor Kashkuta 2016-04-13 14:49:54 +01:00
parent c15db95ffe
commit f62c619b80
2 changed files with 22 additions and 2 deletions

View File

@ -38,6 +38,10 @@ public class ChatInputBar: ReusableXibView {
public weak var delegate: ChatInputBarDelegate?
weak var presenter: ChatInputBarPresenter?
public var updateSendButtonEnabledState = { (inputBar: ChatInputBar) -> Bool in
return !inputBar.textView.text.isEmpty
}
@IBOutlet weak var scrollView: HorizontalStackScrollView!
@IBOutlet weak var textView: ExpandableTextView!
@IBOutlet weak var sendButton: UIButton!
@ -145,10 +149,14 @@ public class ChatInputBar: ReusableXibView {
}
set {
self.textView.text = newValue
self.sendButton.enabled = !self.textView.text.isEmpty
self.updateSendButton()
}
}
private func updateSendButton() {
self.sendButton.enabled = self.updateSendButtonEnabledState(self)
}
@IBAction func buttonTapped(sender: AnyObject) {
self.presenter?.onSendButtonPressed()
self.delegate?.inputBarSendButtonPressed(self)
@ -213,7 +221,7 @@ extension ChatInputBar: UITextViewDelegate {
}
public func textViewDidChange(textView: UITextView) {
self.sendButton.enabled = !textView.text.isEmpty
self.updateSendButton()
self.delegate?.inputBarDidChangeText(self)
}
}

View File

@ -141,6 +141,18 @@ class ChatInputBarTests: XCTestCase {
self.bar.buttonTapped(self.bar)
XCTAssertTrue(self.delegate.inputBarSendButtonPressedCalled)
}
func testThat_WhenInputTextChangedAndCustomStateUpdateClosureProvided_BarUpdatesSendButtonStateAccordingly() {
var closureCalled = false
self.bar.updateSendButtonEnabledState = { (_) in
closureCalled = true
return false
}
self.bar.inputText = " "
self.bar.textViewDidChange(self.bar.textView)
XCTAssertTrue(closureCalled)
XCTAssertFalse(self.bar.sendButton.enabled)
}
}
class FakeChatInputBarPresenter: ChatInputBarPresenter {