diff --git a/Documentation/Usage.md b/Documentation/Usage.md index ce60918..925d4b1 100644 --- a/Documentation/Usage.md +++ b/Documentation/Usage.md @@ -282,25 +282,25 @@ At the same time, items can be added at the start. Notice you'll need to update ```swift -@State var count: Int = -1 @StateObject var page: Page = .first() // @ObservedObject var page: Page = .first() @State var data = Array(0..<5) -Pager(page: self.page, - data: self.data, - id: \.self) { - self.pageView($0) -} -.onPageChanged({ pageIndex in - guard pageIndex == 1 else { return } - let newData = (1...5).map { $0 * self.count } - withAnimation { - self.data.insert(contentsOf: newData, at: 0) - self.page.index += 5 - self.count -= 1 +var body: some View { + Pager(page: self.page, + data: self.data, + id: \.self) { + self.pageView($0) } -}) + .onPageChanged({ pageIndex in + guard pageIndex == 1 else { return } + let newData = (1...5).map { data1.first! - $0 }.reversed() + withAnimation { + page.index += newData.count + data.insert(contentsOf: newData, at: 0) + } + }) +} ``` ## Content Loading Policy diff --git a/Example/SwiftUIPagerExample/Examples/InfiniteExampleView.swift b/Example/SwiftUIPagerExample/Examples/InfiniteExampleView.swift index fc8390a..a194e4e 100644 --- a/Example/SwiftUIPagerExample/Examples/InfiniteExampleView.swift +++ b/Example/SwiftUIPagerExample/Examples/InfiniteExampleView.swift @@ -12,7 +12,6 @@ struct InfiniteExampleView: View { @StateObject var page1 = Page.withIndex(2) @StateObject var page2 = Page.first() - @State var count: Int = 1 @State var data1 = Array(0..<7) @State var isPresented: Bool = false var data2 = Array(0..<20) @@ -36,11 +35,10 @@ struct InfiniteExampleView: View { .onPageChanged({ page in print("Page changed to: \(page)") if page == 1 { - let newData = (1...5).map { data1.first! - $0 * count }.reversed() + let newData = (1...5).map { data1.first! - $0 }.reversed() withAnimation { page1.index += newData.count data1.insert(contentsOf: newData, at: 0) - count += 1 isPresented.toggle() } } else if page == self.data1.count - 2 {