Add ability to control Send button enabled state from external code.
This commit is contained in:
parent
c15db95ffe
commit
f62c619b80
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue