Skip to content

Commit 696eb34

Browse files
authored
Deprecated scrollToBottom() in favor of scrollToLastItem() (MessageKit#1505)
* refactor: deprecated scrollToBottom() in favor of scrollToLastItem * refactor: deprecated scrollToBottom() in favor of scrollToLastItem Changelog
1 parent b1e523a commit 696eb34

8 files changed

Lines changed: 73 additions & 33 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa
1111
### Changed
1212

1313
- Changed `resources` to `resource_bundle` in MessageKit.podspec [#1460](https://github.com/MessageKit/MessageKit/pull/1460) by [@martinpucik](https://github.com/martinpucik)
14+
- Changed dependency manager for Example project to SPM [#1504](https://github.com/MessageKit/MessageKit/pull/1504) by [@martinpucik](https://github.com/martinpucik)
15+
- Deprecated `messagesCollectionView.scrollToBottom` and `scrollsToBottomOnKeyboardBeginsEditing` in favor of `messagesCollectionView.scrollToLastItem` and `scrollsToLastItemOnKeyboardBeginsEditing`. This will be removed in a future release [#1505](https://github.com/MessageKit/MessageKit/pull/1505) by [@martinpucik](https://github.com/martinpucik)
1416

1517
### Removed
1618

Example/Sources/View Controllers/AdvancedExampleViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33

4-
Copyright (c) 2017-2019 MessageKit
4+
Copyright (c) 2017-2020 MessageKit
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -60,7 +60,7 @@ final class AdvancedExampleViewController: ChatViewController {
6060
DispatchQueue.main.async {
6161
self.messageList = messages
6262
self.messagesCollectionView.reloadData()
63-
self.messagesCollectionView.scrollToBottom()
63+
self.messagesCollectionView.scrollToLastItem()
6464
}
6565
}
6666
}
@@ -202,7 +202,7 @@ final class AdvancedExampleViewController: ChatViewController {
202202
updateTitleView(title: "MessageKit", subtitle: isHidden ? "2 Online" : "Typing...")
203203
setTypingIndicatorViewHidden(isHidden, animated: true, whilePerforming: updates) { [weak self] success in
204204
if success, self?.isLastSectionVisible() == true {
205-
self?.messagesCollectionView.scrollToBottom(animated: true)
205+
self?.messagesCollectionView.scrollToLastItem(animated: true)
206206
}
207207
}
208208
}

Example/Sources/View Controllers/AutocompleteExampleViewController.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
//
2-
// AutocompleteExampleViewController.swift
3-
// ChatExample
4-
//
5-
// Created by Nathan Tannar on 2019-04-05.
6-
// Copyright © 2019 MessageKit. All rights reserved.
7-
//
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017-2020 MessageKit
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
824

925
import UIKit
1026
import MessageKit
@@ -141,7 +157,7 @@ final class AutocompleteExampleViewController: ChatViewController {
141157
func setTypingIndicatorViewHidden(_ isHidden: Bool, performUpdates updates: (() -> Void)? = nil) {
142158
setTypingIndicatorViewHidden(isHidden, animated: true, whilePerforming: updates) { [weak self] success in
143159
if success, self?.isLastSectionVisible() == true {
144-
self?.messagesCollectionView.scrollToBottom(animated: true)
160+
self?.messagesCollectionView.scrollToLastItem(animated: true)
145161
}
146162
}
147163
}

Example/Sources/View Controllers/ChatViewController.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ChatViewController: MessagesViewController, MessagesDataSource {
8787
DispatchQueue.main.async {
8888
self.messageList = messages
8989
self.messagesCollectionView.reloadData()
90-
self.messagesCollectionView.scrollToBottom()
90+
self.messagesCollectionView.scrollToLastItem()
9191
}
9292
}
9393
}
@@ -110,7 +110,7 @@ class ChatViewController: MessagesViewController, MessagesDataSource {
110110
messagesCollectionView.messagesDataSource = self
111111
messagesCollectionView.messageCellDelegate = self
112112

113-
scrollsToBottomOnKeyboardBeginsEditing = true // default false
113+
scrollsToLastItemOnKeyboardBeginsEditing = true // default false
114114
maintainPositionOnKeyboardFrameChanged = true // default false
115115

116116
showMessageTimestampOnSwipeLeft = true // default false
@@ -140,7 +140,7 @@ class ChatViewController: MessagesViewController, MessagesDataSource {
140140
}
141141
}, completion: { [weak self] _ in
142142
if self?.isLastSectionVisible() == true {
143-
self?.messagesCollectionView.scrollToBottom(animated: true)
143+
self?.messagesCollectionView.scrollToLastItem(animated: true)
144144
}
145145
})
146146
}
@@ -335,7 +335,7 @@ extension ChatViewController: InputBarAccessoryViewDelegate {
335335
inputBar.sendButton.stopAnimating()
336336
inputBar.inputTextView.placeholder = "Aa"
337337
self?.insertMessages(components)
338-
self?.messagesCollectionView.scrollToBottom(animated: true)
338+
self?.messagesCollectionView.scrollToLastItem(animated: true)
339339
}
340340
}
341341
}

Example/Sources/Views/SwiftUI/MessagesView.swift

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
//
2-
// MessagesView.swift
3-
// ChatExample
4-
//
5-
// Created by Kino Roy on 2020-07-18.
6-
// Copyright © 2020 MessageKit. All rights reserved.
7-
//
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017-2020 MessageKit
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
824

925
import SwiftUI
1026
import MessageKit
@@ -15,7 +31,7 @@ final class MessageSwiftUIVC: MessagesViewController {
1531
super.viewDidAppear(animated)
1632
// Because SwiftUI wont automatically make our controller the first responder, we need to do it on viewDidAppear
1733
becomeFirstResponder()
18-
messagesCollectionView.scrollToBottom(animated: true)
34+
messagesCollectionView.scrollToLastItem(animated: true)
1935
}
2036
}
2137

@@ -32,7 +48,7 @@ struct MessagesView: UIViewControllerRepresentable {
3248
messagesVC.messagesCollectionView.messagesLayoutDelegate = context.coordinator
3349
messagesVC.messagesCollectionView.messagesDataSource = context.coordinator
3450
messagesVC.messageInputBar.delegate = context.coordinator
35-
messagesVC.scrollsToBottomOnKeyboardBeginsEditing = true // default false
51+
messagesVC.scrollsToLastItemOnKeyboardBeginsEditing = true // default false
3652
messagesVC.maintainPositionOnKeyboardFrameChanged = true // default false
3753
messagesVC.showMessageTimestampOnSwipeLeft = true // default false
3854

@@ -47,7 +63,7 @@ struct MessagesView: UIViewControllerRepresentable {
4763
private func scrollToBottom(_ uiViewController: MessagesViewController) {
4864
DispatchQueue.main.async {
4965
// The initialized state variable allows us to start at the bottom with the initial messages without seeing the inital scroll flash by
50-
uiViewController.messagesCollectionView.scrollToBottom(animated: self.initialized)
66+
uiViewController.messagesCollectionView.scrollToLastItem(animated: self.initialized)
5167
self.initialized = true
5268
}
5369
}

Sources/Controllers/MessagesViewController+Keyboard.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33

4-
Copyright (c) 2017-2019 MessageKit
4+
Copyright (c) 2017-2020 MessageKit
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -46,13 +46,16 @@ internal extension MessagesViewController {
4646
@objc
4747
private func handleTextViewDidBeginEditing(_ notification: Notification) {
4848
if scrollsToLastItemOnKeyboardBeginsEditing || scrollsToBottomOnKeyboardBeginsEditing {
49-
guard let inputTextView = notification.object as? InputTextView,
50-
inputTextView === messageInputBar.inputTextView else { return }
51-
49+
guard
50+
let inputTextView = notification.object as? InputTextView,
51+
inputTextView === messageInputBar.inputTextView
52+
else {
53+
return
54+
}
5255
if scrollsToLastItemOnKeyboardBeginsEditing {
5356
messagesCollectionView.scrollToLastItem()
5457
} else {
55-
messagesCollectionView.scrollToBottom(animated: true)
58+
messagesCollectionView.scrollToLastItem(animated: true)
5659
}
5760
}
5861
}

Sources/Controllers/MessagesViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33

4-
Copyright (c) 2017-2019 MessageKit
4+
Copyright (c) 2017-2020 MessageKit
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -22,6 +22,7 @@
2222
SOFTWARE.
2323
*/
2424

25+
import Foundation
2526
import UIKit
2627
import InputBarAccessoryView
2728

@@ -48,6 +49,7 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIGestureRecogni
4849
///
4950
/// The default value of this property is `false`.
5051
/// NOTE: This is related to `scrollToBottom` whereas the above flag is related to `scrollToLastItem` - check each function for differences
52+
@available(*, deprecated, message: "Control scrolling to bottom on keyboardBeginEditing by using scrollsToLastItemOnKeyboardBeginsEditing instead", renamed: "scrollsToLastItemOnKeyboardBeginsEditing")
5153
open var scrollsToBottomOnKeyboardBeginsEditing: Bool = false
5254

5355
/// A Boolean value that determines whether the `MessagesCollectionView`

Sources/Views/MessagesCollectionView.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33

4-
Copyright (c) 2017-2019 MessageKit
4+
Copyright (c) 2017-2020 MessageKit
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -121,6 +121,7 @@ open class MessagesCollectionView: UICollectionView {
121121

122122
// NOTE: This method seems to cause crash in certain cases - https://github.com/MessageKit/MessageKit/issues/725
123123
// Could try using `scrollToLastItem` above
124+
@available(*, deprecated, message: "Scroll to bottom by using scrollToLastItem(:) instead", renamed: "scrollToLastItem")
124125
public func scrollToBottom(animated: Bool = false) {
125126
performBatchUpdates(nil) { [weak self] _ in
126127
guard let self = self else { return }
@@ -166,7 +167,7 @@ open class MessagesCollectionView: UICollectionView {
166167
return messagesCollectionViewFlowLayout.isSectionReservedForTypingIndicator(section)
167168
}
168169

169-
// MARK: View Register/Dequeue
170+
// MARK: - View Register/Dequeue
170171

171172
/// Registers a particular cell using its reuse-identifier
172173
public func register<T: UICollectionViewCell>(_ cellClass: T.Type) {

0 commit comments

Comments
 (0)