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

Scroll to specific Message Item #437

Closed
seidju opened this issue Feb 6, 2018 · 6 comments
Closed

Scroll to specific Message Item #437

seidju opened this issue Feb 6, 2018 · 6 comments

Comments

@seidju
Copy link

seidju commented Feb 6, 2018

Hi @AntonPalich , I want to implement Reply functionality, so user, after he taps on "replied" message scrolls to original message.
The main problem is, that sliding data source doesn't give me all items at once, so before scrolling, I need to add messages to dataSource, something like this:

func loadPrevious(completion: (() -> ())?) {
    slidingWindow.loadPrevious()
    slidingWindow.adjustWindow(focusPosition: 0, maxWindowSize: preferredMaxWindowSize)
    delegate?.chatDataSourceDidUpdate(self, completion: completion) //add completion block for dataSource update
  }

Now, I call something like recursively append images to SlidingDataSource, until desired messageId would contains in chatItems :

fileprivate func scrollToItem(index: Int, messageId: String) {
    if dataSource.chatItems.contains(where: { $0.uid == messageId }) {
      let ip = IndexPath(item: index, section: 0)
      //scroll ???
    } else {
      dataSource.loadPrevious { [weak self] in
        self?.scrollToItem(index: index, messageId: messageId)
      }
    }
  }

My question - how to scroll to this message, even if it's already in chatItems. I tried to just use

      collectionView.scrollToItem(at: ip, at: .centeredVertically, animated: true)

but I got an error ,that cell at ip is doesn't exist.
Thanks for any help or advice!

@Xopoko
Copy link

Xopoko commented Oct 13, 2018

Same question. Has anyone already been able to fix it? The issue has been open since February 6.

@spacedema
Copy link

Have the same issue. Any workaround?

@Daxito
Copy link

Daxito commented Nov 27, 2018

+1

@seidju
Copy link
Author

seidju commented Jul 9, 2019

Hi everyone!
Any updates on this issue?
I tried on latest release, using scrollToItem(withId: uid), but still no effect. In this function - property
let isScrollingUp = rect.minY < collectionView.contentOffset.y always return false
Here's code snippet:

 if let index = self?.chatItemCompanionCollection.indexOf(messageModel.uid),
        let uid = self?.chatItemCompanionCollection[index].uid {
        self?.scrollToItem(withId: uid, position: .centeredVertically, animated: true)
      }

@AntonPalich
Copy link
Contributor

AntonPalich commented Jul 9, 2019

@seidju I just checked the demo app. I modified handleTapOnScrollToBottomButton function to scroll to the first item when you tap "Scroll to bottom" button on "Scroll To Bottom Button Example" screen. It worked for me.

    @objc
    private func handleTapOnScrollToBottomButton() {
        guard self.chatItemCompanionCollection.count > 0 else { return }

        let startIndex = self.chatItemCompanionCollection.startIndex
        self.scrollToItem(withId: self.chatItemCompanionCollection[startIndex].uid, position: .top, animated: true)
    }

Having isScrollingUp == false doesn't mean that it doesn't work. Your item can be visible on a screen, or you have not enough items to make a scroll action. Could you provide a list of items when you try to scroll, a screen from your app and id of a messageModel.uid?

@seidju
Copy link
Author

seidju commented Jul 15, 2019

@AntonPalich Yep, I just checked it again and it was my stupid mistake (used wrong uid). Everything works great, as expected. Sorry for that )
Closing this issue!

@seidju seidju closed this as completed Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants