0

I have a pagination for grid where selected image not correctly showing

this is servicecall:

@Published var isLoadingPage = false

func getPhotosAlbumDetails(ID: String, resetPage: Bool, showHud: Bool = true, _ completion: @escaping StatusCompletion) {
    
    guard APIManager.shared.isConnectedToInternet else {
        showInfoToast(message: K.Msgs.noInternet)
        completion(false)
        return
    }
    
    if isLoadingPage { return }
    isLoadingPage = true
    
    if resetPage {
        page = 1
        photos.removeAll()
    }
    
    let params: [String: Any] = [
        "ID": ID,
        "pg": page
    ]
    
    APIManager.shared.callDecodableApiRequest(with: APIEndPoint.galleryPhotoAlbumDetails, method: .get, params: params, showHud: showHud) { (statusCode, error, model: GalleryPhotoAlbumDetailsModel?) in
        
        self.isLoadingPage = false
        
        if statusCode.isSuccess {
            
            if self.page == 1 {
                self.photos = model?.photos ?? []
            } else {
                let new = model?.photos ?? []
                self.photos.append(contentsOf: new)
            }
            
            self.photoDetails = model
            if let html = self.photoDetails?.description {
                self.plainDescription = html.htmlToPlainText()
            }
            
            self.page += 1
            completion(true)
            
        } else {
            showErrorToast(message: K.Msgs.error)
            completion(false)
        }
    }
}

navigation from list cell:

 struct GalleryDetailsView: View {

                       LazyVGrid(columns: Array(repeating: GridItem(.flexible(), spacing: 4), count: 4), spacing: 4) {
                        ForEach(0..<viewModel.photos.count, id: \.self) { ind in
                            Button {
                                selectedImageIndex = ind
                                
                                NavigationManager.shared.navigateToRoute(
                                    .galleryPreview(
                                        selectedImageIndex: selectedImageIndex ?? 0,
                                        selectedID: self.selectedDataID
                                    )
                                )
                            } label: {
                                let width = ((screenWidth - 24) / 4) - 2
                                URLImageView(url: viewModel.photos[ind].photoPath ?? "N/A" , placeholder: "PhotogalleryPlaceholder", width: width, height: width)
                                    .scaledToFill()
                                    .clipped()
                            }
                            .buttonStyle(.plain)
                            .onAppear {
                                if ind == viewModel.photos.count - 2,
                                   viewModel.photos.count < (viewModel.photoDetails?.totalPhotos ?? 0) {
                                    viewModel.getPhotosAlbumDetails(ID: selectedDataID, resetPage: false) { _ in }
                                }
                            }
                        }
                    }

       }

this is preview screen: per page 50 images loading... but here in onapper selectedImageIndex showing 53 but app crashes... but for all first page images showing correctly.. how to show 2nd page image.. please guide

struct GalleryPrevIewView: View {

@State var selectedImageIndex: Int
var photosArray: [Photo] = []
@StateObject private var viewModel = GalleryViewModel()
@State var selectedDataID: String = ""


var body: some View {
    ZStack {
        Color.black
            TabView(selection: $selectedImageIndex) {
                ForEach(0..<viewModel.photos.count, id: \.self) { index in
                    ZStack(alignment: .topLeading) {
                        
                        URLImageView(url: viewModel.photos[index].photoPath ?? "N/A" , placeholder: "PhotogalleryPlaceholder")
                            .clipped()
                            .scaledToFit()
                            .tag(index)
                            .frame(width: screenWidth)
                    }
                }
            }
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            
          
        }
    }
    .onAppear {
        viewModel.getPhotosAlbumDetails(ID: selectedDataID, resetPage: false) { status in
      Print("selectedImageIndex... (selectedImageIndex)")
            if status {
                if selectedImageIndex < viewModel.photos.count {
                    DispatchQueue.main.async {
                        withAnimation {
                            selectedImageIndex = selectedImageIndex
                        }
                    }
                }
                else if let matchIndex = viewModel.photos.firstIndex(where: { $0.phID == photosArray[selectedImageIndex].phID }) {
                    selectedImageIndex = matchIndex
                }
            }
        }
    }
}
}

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.