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

Fix bottomConstraint calculation bug when rendering custom keyboards (such as Microsoft Swiftkey) for the first time #710

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions Chatto/Source/ChatController/Collaborators/KeyboardTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class KeyboardTracker {
var isTracking = false
var inputBarContainer: UIView
private var notificationCenter: NotificationCenter
private var bottomConstraintSizeAtKeyboardShow: CGFloat?

private var heightBlock: KeyboardHeightBlock

Expand Down Expand Up @@ -121,19 +122,30 @@ class KeyboardTracker {
guard !self.isPerformingForcedLayout else { return }

let bottomConstraint = self.bottomConstraintFromNotification(notification)

guard bottomConstraint > 0 else { return } // Some keyboards may report initial willShow/DidShow notifications with invalid positions
self.keyboardStatus = .shown
self.layoutInputContainer(withBottomConstraint: bottomConstraint)
self.adjustTrackingViewSizeIfNeeded()

// Some keyboards report sizes during keyboardDidShow that change
// only after they have fully rendered, such as Microsoft Swiftkey
self.bottomConstraintSizeAtKeyboardShow = bottomConstraint

self.showKeyboard(withBottomConstraint: bottomConstraint)
}

@objc
private func keyboardWillChangeFrame(_ notification: Notification) {
guard self.isTracking else { return }
let bottomConstraint = self.bottomConstraintFromNotification(notification)

// In the event that our keyboard has changed its frame from the first
// time it was displayed, we will want to trigger another layout
let didBottomConstraintChange = self.bottomConstraintSizeAtKeyboardShow != nil && self.bottomConstraintSizeAtKeyboardShow != bottomConstraint

if bottomConstraint == 0 {
self.keyboardStatus = .hiding
self.layoutInputAtBottom()
} else if didBottomConstraintChange {
self.showKeyboard(withBottomConstraint: bottomConstraint)
}
}

Expand Down Expand Up @@ -203,6 +215,12 @@ class KeyboardTracker {
self.heightBlock(constraint, self.keyboardStatus)
self.isPerformingForcedLayout = false
}

private func showKeyboard(withBottomConstraint bottomConstraint: CGFloat) {
self.keyboardStatus = .shown
self.layoutInputContainer(withBottomConstraint: bottomConstraint)
self.adjustTrackingViewSizeIfNeeded()
}
}

private class KeyboardTrackingView: UIView {
Expand Down