diff --git a/Session/Conversations/ConversationVC.swift b/Session/Conversations/ConversationVC.swift index 439394502..19a5fd4ed 100644 --- a/Session/Conversations/ConversationVC.swift +++ b/Session/Conversations/ConversationVC.swift @@ -55,6 +55,11 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat return messagesTableView.contentSize.height - tableViewUnobscuredHeight } + var isCloseToBottom: Bool { + let margin = (self.lastPageTop - self.messagesTableView.contentOffset.y) + return margin <= ConversationVC.scrollToBottomMargin + } + lazy var mnemonic: String = { let identityManager = OWSIdentityManager.shared() let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection @@ -314,6 +319,13 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat baselineKeyboardHeight = newHeight self.messagesTableView.keyboardHeight = newHeight } + let margin = (self.lastPageTop - self.messagesTableView.contentOffset.y) + // HACK: If the keyboard is coming up and we're very close to the bottom, scroll to the + // bottom. This "fixes" an issue where the conversation would randomly scroll up sometimes + // when bringing up the keyboard. + if newHeight > 200 && margin <= 2 { + scrollToBottom(isAnimated: false) + } scrollButtonConstraint?.constant = -(newHeight + 16) let newContentOffsetY = max(self.messagesTableView.contentOffset.y + min(lastPageTop, 0) + newHeight - self.messagesTableView.keyboardHeight, 0.0) self.messagesTableView.contentOffset.y = newContentOffsetY @@ -353,13 +365,11 @@ final class ConversationVC : BaseVC, ConversationViewModelDelegate, OWSConversat if update.viewItem?.interaction is TSOutgoingMessage { shouldScrollToBottom = true } else { - let margin = (self.lastPageTop - self.messagesTableView.contentOffset.y) - shouldScrollToBottom = margin <= ConversationVC.scrollToBottomMargin + shouldScrollToBottom = self.isCloseToBottom } case .update: self.messagesTableView.reloadRows(at: [ IndexPath(row: Int(update.oldIndex), section: 0) ], with: .fade) - let margin = (self.lastPageTop - self.messagesTableView.contentOffset.y) - shouldScrollToBottom = margin <= ConversationVC.scrollToBottomMargin + shouldScrollToBottom = self.isCloseToBottom default: preconditionFailure() } }