Skip to content

Commit

Permalink
[MUIC-304] Fix keyboard disappearing on operator's message received (#38
Browse files Browse the repository at this point in the history
)

* [MUIC-304] Fix keyboard disappearing on operator's message received
* Small refactor for ChatMessageEntryView
* Always check for choice card input mode on received message
* Fix tableview content offset being set wrong when keyboard appears.
Scroll to the bottom of table view instead.
  • Loading branch information
mc-vladyslav-kupriienko committed May 11, 2021
1 parent e95678d commit 4d37124
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
5 changes: 3 additions & 2 deletions GliaWidgets/View/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,17 @@ extension ChatView {
private func observeKeyboard() {
keyboardObserver.keyboardWillShow = { [unowned self] properties in
let bottomInset = safeAreaInsets.bottom
let offset = CGPoint(x: 0, y: self.tableView.contentSize.height)
let newEntryConstraint = -properties.finalFrame.height + bottomInset
UIView.animate(
withDuration: properties.duration,
delay: 0.0,
options: properties.animationOptions,
animations: { [weak self] in
self?.messageEntryViewBottomConstraint.constant = newEntryConstraint
self?.tableView.contentOffset = offset
self?.layoutIfNeeded()
},
completion: { [weak self] _ in
self?.tableView.scrollToBottom(animated: true)
}
)
}
Expand Down
39 changes: 28 additions & 11 deletions GliaWidgets/View/Chat/Entry/ChatMessageEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@ public class ChatMessageEntryView: UIView {
var isChoiceCardModeEnabled: Bool {
didSet {
isEnabled = !isChoiceCardModeEnabled
textView.resignFirstResponder()
if isChoiceCardModeEnabled {
textView.resignFirstResponder()
}
placeholderLabel.text = isChoiceCardModeEnabled
? style.choiceCardPlaceholder
: style.placeholder
}
}

var showsSendButton: Bool {
get { return !sendButton.isHidden }
set { sendButton.isHidden = !newValue }
}

var isEnabled: Bool {
get { return isUserInteractionEnabled }
set { isUserInteractionEnabled = newValue }
Expand Down Expand Up @@ -60,7 +64,7 @@ public class ChatMessageEntryView: UIView {
fatalError("init(coder:) has not been implemented")
}

public override func layoutSubviews() {
override public func layoutSubviews() {
super.layoutSubviews()
updateTextViewHeight()
}
Expand All @@ -72,8 +76,10 @@ public class ChatMessageEntryView: UIView {

messageContainerView.backgroundColor = style.backgroundColor

let tapRecognizer = UITapGestureRecognizer(target: self,
action: #selector(textViewContainerTap))
let tapRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(textViewContainerTap)
)
messageContainerView.addGestureRecognizer(tapRecognizer)

textView.delegate = self
Expand All @@ -99,8 +105,10 @@ public class ChatMessageEntryView: UIView {

private func layout() {
messageContainerView.addSubview(textView)
textViewHeightConstraint = textView.autoSetDimension(.height,
toSize: kMinTextViewHeight)
textViewHeightConstraint = textView.autoSetDimension(
.height,
toSize: kMinTextViewHeight
)

textView.autoPinEdge(toSuperviewEdge: .left, withInset: 16)
textView.autoPinEdge(toSuperviewEdge: .top, withInset: 13)
Expand Down Expand Up @@ -138,8 +146,10 @@ public class ChatMessageEntryView: UIView {
}

private func updateTextViewHeight() {
let size = CGSize(width: textView.frame.size.width,
height: CGFloat.greatestFiniteMagnitude)
let size = CGSize(
width: textView.frame.size.width,
height: CGFloat.greatestFiniteMagnitude
)
var newHeight = textView.sizeThatFits(size).height

textView.isScrollEnabled = newHeight > kMaxTextViewHeight
Expand All @@ -163,8 +173,15 @@ public class ChatMessageEntryView: UIView {
}

extension ChatMessageEntryView: UITextViewDelegate {
public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(in: range, with: text)
public func textView(
_ textView: UITextView,
shouldChangeTextIn range: NSRange,
replacementText text: String
) -> Bool {
let newText = (textView.text as NSString).replacingCharacters(
in: range,
with: text
)
return newText.count < maxCharacters
}

Expand All @@ -173,7 +190,7 @@ extension ChatMessageEntryView: UITextViewDelegate {
textChanged?(textView.text)
}

public func textViewDidBeginEditing(_ textView: UITextView) {
public func textViewDidBeginEditing(_: UITextView) {
placeholderLabel.isHidden = true
}

Expand Down

0 comments on commit 4d37124

Please sign in to comment.