Enhancement/load more messages and maintain scroll position#284
Conversation
| scrollToItem(at: indexPath, at: .bottom, animated: animated) | ||
| } | ||
|
|
||
| override open func reloadData() { |
There was a problem hiding this comment.
@azurechen Thanks for submitting this PR! Unfortunately, I don't think overriding reloadData is the right approach to achieving this functionality.
I notice this is the second PR you've submitted recently. You should join the MessageKit Slack channel so we can discuss design decisions.
I think some MessageKit users have already implemented this functionality on their own, it would be good to discuss/compare it to what they've done.
There was a problem hiding this comment.
Hi I have joined slack few days ago. Thanks so much.
I have explained my idea on the following comment. I don't think I really override the reloadData method I just extend it.
nathantannar4
left a comment
There was a problem hiding this comment.
Hey @azurechen I like this feature! I agree with @SD10 that we shouldn't override reloadData to get this functionality. Perhaps making a separate function that inserts new messages
|
I have consider using another method to insert old messages. But let me explain why I think override First, base on the pattern of original Second, in a sense I didn't really override the if !messageLoadMoreControl.isRefreshing {
super.reloadData()
return
}Extend Register the selector messageLoadMoreControl.addTarget(self, action: #selector(loadMoreMessages), for: .valueChanged)And insert older data to the self.messageList.insert(contentsOf: messages, at: 0)I have been thinking about how to use a beautiful way to implement it for a few days. I really want to contribute because I use And please try the example, I believe the effect is satisfying. |
|
@azurechen I thought about this a bit more and I mentioned in slack, but I think a second method - |
|
@cwalo I think it is a perfect compromise! |
cwalo
left a comment
There was a problem hiding this comment.
Can't approve until we get some more discussion on this.
|
|
||
| // reset the contentOffset after data is updated | ||
| self.setContentOffset(CGPoint( | ||
| x: self.contentOffset.x + (afterContentSize.width - beforeContentSize.width), |
There was a problem hiding this comment.
For readability, consider creating a let variable for the new offset and then use that in setContentOffset.
|
|
||
| open weak var messageCellDelegate: MessageCellDelegate? | ||
|
|
||
| open var messageLoadMoreControl = UIRefreshControl() |
There was a problem hiding this comment.
I'm not so sure this should live in the collectionview. Perhaps move to MessagesViewController and add a method for adding/removing it. Similar to JSQ in that regard.
There was a problem hiding this comment.
I agree moving it to MessagesViewController make sense. but because of !messageLoadMoreControl.isRefreshing. it have to be put in MessageCollectionView
There was a problem hiding this comment.
I agree. It could also just be called refreshControl and lie in MessagesViewController
There was a problem hiding this comment.
Possibly, we don't even need to provide this control? The method is the most important thing. Then the user can hook it up to whatever they want? Need to think about this a little bit more
There was a problem hiding this comment.
There is a possible issue that different control component has different sliding animation duration. if the animation not stop, it is hard to calculate the right offset.
Do you think we can temporarily keep this control, and provide another interface to user in later version to customize their own refreshControl?
There was a problem hiding this comment.
I though an idea. Create a new class name MessageMoreControl but not extends UIRefreshControl.
And user have to setup the component by
messageMoreControl = MessageMoreControl(component: UIRefreshControl())
messageMoreControl.delegate = self
user can choose use UIRefreshControl or customized view
messageMoreControl = MessageMoreControl(component: CustomRefreshView())
messageMoreControl.delegate = self
and provide a MessageMoreControlDelegate for user load more data
protocol MessageMoreControlDelegate {
func didPullDown(of messageMoreControl: MessageMoreControl)
}
| } | ||
|
|
||
| public func reloadAndMaintainOffset() { | ||
| if !messageLoadMoreControl.isRefreshing { |
There was a problem hiding this comment.
I'm waffling between whether or not this method should do any checking of the refresh control or if the user should do it. Otherwise, it should only calculate the new offset and refresh. If this were the case, the user could do any checking they need to, then just call super. @SD10 thoughts?
There was a problem hiding this comment.
I agree, the user should probably do this outside of the call to reloadAndMaintainOffset() to determine if the method is called at all. Also, may need to improve method naming on this one 🤔
There was a problem hiding this comment.
@SD10 could you suggest a name? I think the method name should include reloadData to imply that is a extended method of reloadData
cwalo
left a comment
There was a problem hiding this comment.
One question about automaticallyAdjustsScrollViewInsets = true. Otherwise, looks good. 👍
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
|
|
||
| automaticallyAdjustsScrollViewInsets = true |
There was a problem hiding this comment.
for the UIRefreshControl of this example. If not set automaticallyAdjustsScrollViewInsets, the refreshControl will be covered by navigation bar.
|
Will this be part of |
|
@alijaza I'm reviewing this again now I'm wondering if this functionality can be moved into the See this comment cc @cwalo What do you think? |
…intain-scroll-position
|
Thank you for contributing to MessageKit! I've invited you to join the MessageKit GitHub organization - no pressure to accept! If you'd like more information on what that means, check out our contributing guidelines and join the MessageKit Slack channel. Feel free to reach out if you have any questions! 😃 |
What does this implement/fix? Explain your changes.
Pull down to load more messages and keep the position.
The example was updated. Reviewers can try the effect at
ConversationViewControllerDoes this close any currently open issues?
#186 Maintain scroll position after loading new messages
Where has this been tested?
Devices/Simulators: iPhone 6, iPhone 7s Simulators
iOS Version: 10
Swift Version: 3.2
MessageKit Version: 0.9.0