fermoya / SwiftUIPager

Native Pager in SwiftUI
MIT License
1.28k stars 171 forks source link

[BUG] Issues on delete of the Last element. #228

Closed Clem23 closed 3 years ago

Clem23 commented 3 years ago

Describe the bug When SwiftUIPager is configure as LoadingPolicy = lazy and removing the last item. It seems to works fine the first time. But on second removing of the last element, Pager become empty.

When SwiftUIPager is configure as LoadingPolicy = eager then removing the last element make the Pager to scroll on the first element.

More-over, Page.Index and Page.TotalPages do not reflect the deletion.

Here is my Fix On file PagerContent+Helper.swift

/// Current page index
    var page: Int {
        // Fix Begin
        if (pagerModel.index >= numberOfPages) {
            pagerModel.totalPages = numberOfPages
            pagerModel.index = numberOfPages - 1
        }
        // Fix End
        return pagerModel.index
    }

You will perhaps find a better fix but this one do the job. (Perhaps it could be better to observe changes on data)

Clem23 commented 3 years ago

Could be a regression as on January 15th your code change from that:

 /// Work around to avoid @State keeps wrong value
    var page: Int {
        return min(pagerModel.page, numberOfPages - 1)
    }

to that:

/// Current page index
    var page: Int {
        pagerModel.index
    }

Nevertheless the TotalPages was not updated...

fermoya commented 3 years ago

Hi @Clem23 , I made some changes in January to use an ObservableObject rather than individual State variables.

I'm confused by your second, does that mean this is still reproducible but the suggested fix doesn't work any more?