Krisiacik / ImageViewer

An image viewer à la Twitter
MIT License
2.53k stars 385 forks source link

controller reload datatsource #205

Open Chepond opened 5 years ago

Chepond commented 5 years ago

how to reload the datasource of controller? for example if i added more galleryItems to the array upon paging, how do i reload the whole source? since if i add the source to the start of array, it is working(direction: left), but if i add to the last of the array, it is not able to swipe to next photo(direction: right)

Chepond commented 5 years ago

I have found the root cause.:) When I added source to the start of array, after I called page(to: index), it can go to the last galleryItem as the index of the last galleryItem after paging has changed(increased). But if I add source to the end of array, the index of the last galleryItem will remain unchanged, then the page function will not perform any action as it is guarded....

Can anyone please give me some hope, any WORKAROUND around?:( Or can you add the reload data source function plssssss

open func page(toIndex index: Int) {

    guard currentIndex != index && index >= 0 && index < self.itemsDataSource.itemCount() else { return }

    let imageViewController = self.pagingDataSource.createItemController(index)
    let direction: UIPageViewController.NavigationDirection = index > currentIndex ? .forward : .reverse

    // workaround to make UIPageViewController happy
    if direction == .forward {
        let previousVC = self.pagingDataSource.createItemController(index - 1)
        setViewControllers([previousVC], direction: direction, animated: true, completion: { finished in
            DispatchQueue.main.async(execute: { [weak self] in
                self?.setViewControllers([imageViewController], direction: direction, animated: false, completion: nil)
                })
        })
    } else {
        let nextVC = self.pagingDataSource.createItemController(index + 1)
        setViewControllers([nextVC], direction: direction, animated: true, completion: { finished in
            DispatchQueue.main.async(execute: { [weak self] in
                self?.setViewControllers([imageViewController], direction: direction, animated: false, completion: nil)
                })
        })
    }
}