Skip to content

Commit 1c135ec

Browse files
author
Christian Elies
committed
docs(readme): updated the pagination section
1 parent 880055e commit 1c135ec

File tree

1 file changed

+27
-40
lines changed

1 file changed

+27
-40
lines changed

README.md

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,52 +37,39 @@ AdvancedList(yourData, content: { item in
3737

3838
### 📄 Pagination
3939

40-
The `Pagination` is implemented as a class (conforming to `ObservableObject`) so the `AdvancedList` can observe it.
41-
It has three different states: `error`, `idle` and `loading`. If the `state` of the `Pagination` changes the `AdvancedList` updates itself to show or hide the state related view (`ErrorView` for state `.error(Error)` or `LoadingView` for state `.loading`, `.idle` will display nothing). Update the `state` if you start loading (`.loading`), stop loading ( `.idle`) or if an error occurred (`.error(Error)`) so the `AdvancedList` can render the appropriate view.
40+
The `Pagination` functionality is now (>= `5.0.0`) implemented as a `modifier`.
41+
It has three different states: `error`, `idle` and `loading`. If the `state` of the `Pagination` changes the `AdvancedList` displays the view created by the view builder of the specified pagination object (`AdvancedListPagination`). Keep track of the current pagination state by creating a local state variable (`@State`) of type `AdvancedListPaginationState`. Use this state variable in the `content` `ViewBuilder` of your pagination configuration object to determine which view should be displayed in the list (see the example below).
4242

43-
If you want to use pagination you can choose between the `lastItemPagination` and the `thresholdItemPagination`. Both concepts are described [here](https://github.com/crelies/ListPagination). Just pass `.lastItemPagination` or `.thresholdItemPagination` including the required parameters to the `AdvancedList` initializer.
43+
If you want to use pagination you can choose between the `lastItemPagination` and the `thresholdItemPagination`. Both concepts are described [here](https://github.com/crelies/ListPagination). Just specify the type of the pagination when adding the `.pagination` modifier to your `AdvancedList`.
4444

45-
Both pagination types require
46-
47-
- an **ErrorView** and a **LoadingView** (**ViewBuilder**)
48-
- a block (**shouldLoadNextPage**) which is called if the `last or threshold item appeared` and
49-
- the initial state (**AdvancedListPaginationState**) of the pagination which determines the visibility of the pagination state related view.
50-
51-
The `thresholdItemPagination` expects an offset parameter (number of items before the last item) to determine the threshold item.
52-
53-
**The ErrorView or LoadingView will only be visible below the List if the last item of the List appeared! That way the user is only interrupted if needed.**
54-
55-
**Skip pagination setup by using `.noPagination`.**
45+
**The view created by the `content` `ViewBuilder` of your pagination configuration object will only be visible below the List if the last item of the List appeared! That way the user is only interrupted if needed.**
5646

5747
**Example:**
5848

5949
```swift
60-
private(set) lazy var pagination: AdvancedListPagination<AnyView, AnyView> = {
61-
.thresholdItemPagination(errorView: { error in
62-
AnyView(
63-
VStack {
64-
Text(error.localizedDescription)
65-
.lineLimit(nil)
66-
.multilineTextAlignment(.center)
67-
68-
Button(action: {
69-
// load current page again
70-
}) {
71-
Text("Retry")
72-
}.padding()
73-
}
74-
)
75-
}, loadingView: {
76-
AnyView(
77-
VStack {
78-
Divider()
79-
Text("Loading...")
80-
}
81-
)
82-
}, offset: 25, shouldLoadNextPage: {
83-
// load next page
84-
}, state: .idle)
85-
}()
50+
@State private var paginationState: AdvancedListPaginationState = .idle
51+
52+
AdvancedList(...)
53+
.pagination(.init(type: .lastItem, shouldLoadNextPage: {
54+
paginationState = .loading
55+
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
56+
items.append(contentsOf: moreItems)
57+
paginationState = .idle
58+
}
59+
}) {
60+
switch paginationState {
61+
case .idle:
62+
EmptyView()
63+
case .loading:
64+
if #available(iOS 14.0, *) {
65+
ProgressView()
66+
} else {
67+
Text("Loading ...")
68+
}
69+
case let .error(error):
70+
Text(error.localizedDescription)
71+
}
72+
})
8673
```
8774

8875
### 📁 Move and 🗑️ delete items

0 commit comments

Comments
 (0)