Skip to content

Commit

Permalink
Observe inputBar frame change to update collectionView bottom inset (#…
Browse files Browse the repository at this point in the history
…1726)

* feat: Update bottom inset on InputBar frame  change

* feat: Update bottom inset on InputBar frame  change

Delay textview did begin editing notification for scroll to bottom to work

* feat: Update bottom inset on InputBar frame  change

Remove unnecessary presentedController check

* feat: Update bottom inset on InputBar frame  change

Docu

* feat: Update bottom inset on InputBar frame  change

Changelog
  • Loading branch information
martinpucik committed Jul 20, 2022
1 parent 1ac0bf9 commit 6244308
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
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

0 comments on commit 6244308

Please sign in to comment.