'Last item in for each list does not allow scrolling on full screen sheet SwiftUI Firestore

I have a for each list that displays items that I call snippets ("Kinda like a tweet"). Within these snippets is a button that allows the user to open up the comments section via a full-screen sheet. The problem is that when I scroll on the comment sheet, it automatically closes. I think the reason is due to my pagination functionality.

Basically I have a updateData() function that will be called to fetch the next batch of firestore documents. It is activated when the last item on the list is reached. It appears that when I scroll on the comment section sheet, it also activates the updateData() function in the view behind it. This resets everything and closes the comment section sheet.

This only happens for the last item in the list and not for any item before it.

Here is the main view

ForEach(self.data){i in
    ZStack{
        if i.name == ""{
            ProgressView()
        }
        else {
            //Track end of list. This is where I get the last item in the list and update data if scrolling continues. 
            if self.data.last?.id == i.id{
                VStack{
                    GeometryReader{g in
                        
                        
                        
                        VStack{
                            
                            
                            ZStack{
                                FeedSnippetsRow(refSnippet: i, refreshData: self.refresh)
                                    .environmentObject(viewedModel)
                            }
                            .padding(EdgeInsets.init(top: 0, leading: 15, bottom: 0, trailing: 15))
                            
                            Divider().foregroundColor(doppleGray)
                            
                            
                        }
                        
                        .onAppear {
                            self.time = Timer.publish(every: 0.1, on: .main, in: .tracking).autoconnect()
                        }
                        .onReceive(self.time) { (_) in
                            
                            if (g.frame(in: .global).minY) < UIScreen.main.bounds.height - 80 {

                                
                                self.updateData()
                                print("updated")
                                
                                
                                self.time.upstream.connect().cancel()
                            }
                            
                        }
                    }
                }
                
                .frame(height: 1000)
            } else {
                //Original data
                
               
                VStack{
                    ZStack{
                        FeedSnippetsRow(refSnippet: i, refreshData: self.refresh)
                            .environmentObject(viewedModel)
                    }
                    .padding(EdgeInsets.init(top: 0, leading: 15, bottom: 0, trailing: 15))
                    
                    Divider().foregroundColor(doppleGray)
                }
                
                
                
            }
            
            
           
            
        }
    }
    
    
}

This is the FeedSnippetsRow: Here let's focus on the comment button that opens the comments sheet via a full-screen sheet.

HStack{
    
    Button(action:{
        self.showCommentsView.toggle()  
    }){
        Image(systemName: "message")
            .resizable()
            .aspectRatio(contentMode: .fill)
            .frame(width: 18, height: 18)
            .foregroundColor(.primary)
    }
    .fullScreenCover(isPresented: $showCommentsView){
        Comments(snippetId: refSnippet.snippetId, didComment: $didComment, commentCount: $commentCount)
   
    }
    
 

What should I do so that the last item in the list, does not cause the comment sheet to close when scrolling in the comment section?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source