Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe inputBar frame change to update collectionView bottom inset #1726

Merged
merged 5 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -35,6 +35,7 @@ See [MIGRATION_GUIDE.md](https://github.com/MessageKit/MessageKit/blob/main/Docu
MessageSizeCalculator.messageContainerSize(for message: MessageType, at indexPath: IndexPath) -> CGSize
```
- Updated InputBarAccessoryView to v6.1.0 [#1716](https://github.com/MessageKit/MessageKit/pull/1716) by [@martinpucik](https://github.com/martinpucik)
- Observe inputBar frame change to update collectionView bottom inset instead of keyboard show/hide notifications [#1726](https://github.com/MessageKit/MessageKit/pull/1726) by [@martinpucik](https://github.com/martinpucik)

### Fixed

Expand Down
21 changes: 6 additions & 15 deletions Sources/Controllers/MessagesViewController+Keyboard.swift
Expand Up @@ -35,10 +35,12 @@ internal extension MessagesViewController {
keyboardManager.bind(inputAccessoryView: inputContainerView)
keyboardManager.bind(to: messagesCollectionView)

/// Observe didBeginEditing to scroll down the content
/// Observe didBeginEditing to scroll content to last item if necessary
NotificationCenter.default
.publisher(for: UITextView.textDidBeginEditingNotification)
.subscribe(on: DispatchQueue.global())
/// Wait for inputBar frame change animation to end
.delay(for: .milliseconds(200), scheduler: DispatchQueue.main)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
self?.handleTextViewDidBeginEditing(notification)
Expand All @@ -65,19 +67,10 @@ internal extension MessagesViewController {
}
.store(in: &disposeBag)

Publishers.MergeMany(
NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification),
NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)
)
.subscribe(on: DispatchQueue.global())
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] _ in
self?.updateMessageCollectionViewBottomInset()
})
.store(in: &disposeBag)

/// Observe frame change of the input bar container to not cover collectioView with inputBar
/// Observe frame change of the input bar container to update collectioView bottom inset
inputContainerView.publisher(for: \.center)
.receive(on: DispatchQueue.main)
.removeDuplicates()
.sink(receiveValue: { [weak self] _ in
self?.updateMessageCollectionViewBottomInset()
})
Expand All @@ -88,8 +81,6 @@ internal extension MessagesViewController {

/// Updates bottom messagesCollectionView inset based on the position of inputContainerView
func updateMessageCollectionViewBottomInset() {
/// This is important to skip notifications from child modal controllers in iOS >= 13.0
guard self.presentedViewController == nil else { return }
let collectionViewHeight = messagesCollectionView.frame.height
let newBottomInset = collectionViewHeight - (inputContainerView.frame.minY - additionalBottomInset) - automaticallyAddedBottomInset
let normalizedNewBottomInset = max(0, newBottomInset)
Expand Down