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

[BUG] Vertical pager breaks when adding content on the start of the list, on demand #298

Open
Samigos opened this issue Sep 19, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@Samigos
Copy link

Samigos commented Sep 19, 2022

Xcode version: 14.0
SwiftUIPager version: 2.5.0

I have an app, that shows posts on a vertical pager. Each page fills the whole screen.

So, when the app launches, I fetch the 10 most recent posts and display them.
As soon as those posts are fetched, I start listening for new posts. (see code below) Whenever that callback gets triggered, a new post is created. I take it and place it on the top of my list.

The thing is when I scroll to find the new post, its views get mixed up with the views of the next post.
Here's what I mean:

Before the new post, I have the one below
https://imgur.com/a/ZmMzfvb

And then, a new post is added to the top
https://imgur.com/a/PJ0trSF

As you'll notice the image seems to be the same, but it shouldn’t! If I scroll for a while and then go back up, the new post will be fixed and display the proper image. (I'm using SDWebImageSwiftUI for async images, but I don't think it matters... I also used Apple's AsyncImage, with the same results)

Here's my feed view model:

@Published var feedPage: Page = .first()
@Published var feedItems = Array(0..<2)

var posts = [Post]()

...

private func subscribeToNewPosts() {
    postsService.subscribeToNewPosts() { [weak self] post in
        self?.posts.insert(post, at: 0)
                
        DispatchQueue.main.async {
             self?.feedItems = Array(0..<(self?.posts.count ?? 1))
        }
    }
}

And here's my feed view:

private struct FeedPageView: View {
    @EnvironmentObject private var viewModel: FeedView.ViewModel
    
    var body: some View {
        ZStack {
            VStack {
                Pager(page: viewModel.feedPage,
                      data: viewModel.feedItems,
                      id: \.self,
                      content: { index in
                    if index == 0 {
                        HomeCameraView()
                            .background(.black)
                    } else {
                            PostView(post: viewModel.posts[index - 1])
                    }
                })
                .vertical()
                .sensitivity(.custom(0.1))
                .onPageWillChange { index in
                    viewModel.willChangeVerticalPage(index: index)
                }
            }
        }
    }
}

Any idea what I'm doing wrong?

@Samigos Samigos added the bug Something isn't working label Sep 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant