diff --git a/ChattoAdditions/Source/Input/ChatInputBar.swift b/ChattoAdditions/Source/Input/ChatInputBar.swift index 42722e6..7b42c1c 100644 --- a/ChattoAdditions/Source/Input/ChatInputBar.swift +++ b/ChattoAdditions/Source/Input/ChatInputBar.swift @@ -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) } } diff --git a/ChattoAdditions/Tests/Input/ChatInputBarTests.swift b/ChattoAdditions/Tests/Input/ChatInputBarTests.swift index 87a0c4a..b739fb3 100644 --- a/ChattoAdditions/Tests/Input/ChatInputBarTests.swift +++ b/ChattoAdditions/Tests/Input/ChatInputBarTests.swift @@ -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 {