fermoya / SwiftUIPager

Native Pager in SwiftUI
MIT License
1.3k stars 172 forks source link

[BUG] Event driven navigation in looping Pager #234

Closed louis1001 closed 3 years ago

louis1001 commented 3 years ago

Describe the bug When using a looping Pager, updating the Page element with .update(.next) works as intended until the last page, and then stops.

To Reproduce

import SwiftUI
import SwiftUIPager

struct SampleView: View {
    @ObservedObject var page1 = Page.first()
    var data = Array(0..<5)
    var body: some View {
        Pager(page: page1, data: data, id: \.self) { element in
            Text("\(element)")
                .frame(width: 300, height: 300)
                .background(Color.gray)
        }
        .onAppear {
            Timer.scheduledTimer(withTimeInterval: 2, repeats: true) {_ in
                withAnimation {
                    page1.update(.next)
                }
            }
        }
    }
}

Expected behavior When it gets to the last page, I expect it to animate going the next page (the first one), like it would with user input.

Environment:

fermoya commented 3 years ago

@louis1001 thanks for reporting. This should be easy to fix, I'll see if I can get some time for that.

The issue is here in case you wanted to raise a PR yourself. Page needs to know Pager is looping so that it returns the right index.

It might be more complicated than that but that's a start

louis1001 commented 3 years ago

Thanks! I'll take a look at it.

My first impulse is to give Page a Pager? that it could check.