Skip to content

Commit b480e90

Browse files
author
Chris
authored
Merge pull request #2 from crelies/dev
Version 3.0.0
2 parents 3a1b0eb + c571184 commit b480e90

File tree

5 files changed

+173
-212
lines changed

5 files changed

+173
-212
lines changed

README.md

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,24 @@ Add this Swift package in Xcode using its Github repository url. (File > Swift P
1212

1313
## 🚀 How to use
1414

15-
You control the view through an instance of `ListService`. The service manages the current state and the items of the list.
16-
Use it to append, update or remove items and to modify the state of the list. The `AdvancedList` view listens to the service and updates itself if needed.
15+
The `AdvancedList` view is similar to the `List` and `ForEach` views. You have to pass data (`RandomAccessCollection`) and a view provider (`(Data.Element) -> some View`) to the initializer. In addition to the `List` view the `AdvancedList` expects a list state and corresponding views.
16+
Modify your data anytime or hide an item through the content block if you like. The view is updated automatically 🎉.
1717

1818
```swift
1919
import AdvancedList
2020

21-
let listService = ListService()
21+
@State private var listState: ListState = .items
2222

23-
AdvancedList(listService: listService, emptyStateView: {
23+
AdvancedList(yourData, content: { item in
24+
Text("Item")
25+
}, listState: $listState, emptyStateView: {
2426
Text("No data")
2527
}, errorStateView: { error in
2628
Text(error.localizedDescription)
2729
.lineLimit(nil)
2830
}, loadingStateView: {
2931
Text("Loading ...")
3032
}, pagination: .noPagination)
31-
32-
listService.listState = .loading
33-
// TODO: fetch your items
34-
listService.appendItems(yourItems)
35-
listService.listState = .items
3633
```
3734

3835
### 📄 Pagination
@@ -87,38 +84,85 @@ private(set) lazy var pagination: AdvancedListPagination<AnyView, AnyView> = {
8784

8885
### 📁 Move and 🗑️ delete items
8986

90-
You can define which actions your list should support through the `supportedListActions` property of your `ListService` instance.
91-
Choose between `delete`, `move`, `moveAndDelete` and `none`. **The default is `none`.**
87+
You can define which actions your list should support through the `onMoveAction` and `onDeleteAction` initializer parameters.
88+
**Per default the move and delete functions are disabled if you skip the parameters.**
9289

9390
```swift
94-
let listService = ListService()
95-
listService.supportedListActions = .moveAndDelete(onMove: { indexSet, index in
96-
// move me
97-
}, onDelete: { indexSet in
98-
// please delete me
99-
})
91+
import AdvancedList
92+
93+
@State private var listState: ListState = .items
94+
95+
AdvancedList(yourData, content: { item in
96+
Text("Item")
97+
}, listState: $listState, onMoveAction: { (indexSet, index) in
98+
// do something
99+
}, onDeleteAction: { indexSet in
100+
// do something
101+
}, emptyStateView: {
102+
Text("No data")
103+
}, errorStateView: { error in
104+
Text(error.localizedDescription)
105+
.lineLimit(nil)
106+
}, loadingStateView: {
107+
Text("Loading ...")
108+
}, pagination: .noPagination)
100109
```
101110

102111
### 🎛️ Filtering
103112

104-
The `AdvancedList` supports filtering (**disabled by default**). You only have to set the closure `excludeItem: (AnyListItem) -> Bool)` on your `ListService` instance.
105-
`AnyListItem` gives you access to the item (`Any`). **Keep in mind that you have to cast this item to your custom type!**
113+
**You can hide items in your list through the content block.** Only return a view in the content block if a specific condition is met.
114+
115+
## 🎁 Example
116+
117+
The following code shows how easy-to-use the view is:
106118

107119
```swift
108-
let listService = ListService()
109-
listService.excludeItem = { ($0.item as? YourItem).type == .xyz }
120+
import AdvancedList
121+
122+
@State private var listState: ListState = .items
123+
124+
AdvancedList(yourData, content: { item in
125+
Text("Item")
126+
}, listState: $listState, emptyStateView: {
127+
Text("No data")
128+
}, errorStateView: { error in
129+
VStack {
130+
Text(error.localizedDescription)
131+
.lineLimit(nil)
132+
133+
Button(action: {
134+
// do something
135+
}) {
136+
Text("Retry")
137+
}
138+
}
139+
}, loadingStateView: {
140+
Text("Loading ...")
141+
}, pagination: .noPagination)
110142
```
111143

112-
## 🎁 Example
144+
For more examples take a look at [AdvancedList-SwiftUI](https://github.com/crelies/AdvancedList-SwiftUI).
113145

114-
The following code shows how easy-to-use the view is:
146+
## Migration 2.x -> 3.0
147+
148+
The `AdvancedList` was dramatically simplified and is now more like the `List` and `ForEach` SwiftUI views.
149+
150+
1. Delete your list service instances and directly **pass your data to the list initializer**
151+
2. Create your views through a content block (**initializer parameter**) instead of conforming your items to `View` directly (removed type erased wrapper `AnyListItem`)
152+
3. Pass a list state binding to the initializer (**before:** the `ListService` managed the list state)
153+
4. **Move and delete:** Instead of setting `AdvancedListActions` on your list service just pass a `onMoveAction` and/or `onDeleteAction` block to the initializer
115154

155+
**Before:**
116156
```swift
117157
import AdvancedList
118158

119159
let listService = ListService()
120-
listService.appendItems(yourItems)
121-
listService.listState = .items
160+
listService.supportedListActions = .moveAndDelete(onMove: { (indexSet, index) in
161+
// please move me
162+
}, onDelete: { indexSet in
163+
// please delete me
164+
})
165+
listService.listState = .loading
122166

123167
AdvancedList(listService: listService, emptyStateView: {
124168
Text("No data")
@@ -136,6 +180,39 @@ AdvancedList(listService: listService, emptyStateView: {
136180
}, loadingStateView: {
137181
Text("Loading ...")
138182
}, pagination: .noPagination)
183+
184+
listService.listState = .loading
185+
// fetch your items ...
186+
listService.appendItems(yourItems)
187+
listService.listState = .items
139188
```
140189

141-
For more examples take a look at [AdvancedList-SwiftUI](https://github.com/crelies/AdvancedList-SwiftUI).
190+
**After:**
191+
```swift
192+
import AdvancedList
193+
194+
@State private var listState: ListState = .items
195+
196+
AdvancedList(yourData, content: { item in
197+
Text("Item")
198+
}, listState: $listState, onMoveAction: { (indexSet, index) in
199+
// move me
200+
}, onDeleteAction: { indexSet in
201+
// delete me
202+
}, emptyStateView: {
203+
Text("No data")
204+
}, errorStateView: { error in
205+
VStack {
206+
Text(error.localizedDescription)
207+
.lineLimit(nil)
208+
209+
Button(action: {
210+
// do something
211+
}) {
212+
Text("Retry")
213+
}
214+
}
215+
}, loadingStateView: {
216+
Text("Loading ...")
217+
}, pagination: .noPagination)
218+
```

Sources/AdvancedList/public/Models/AdvancedListActions.swift

Lines changed: 0 additions & 15 deletions
This file was deleted.

Sources/AdvancedList/public/Models/AnyListItem.swift

Lines changed: 0 additions & 22 deletions
This file was deleted.

Sources/AdvancedList/public/Services/ListService.swift

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)