We’re only showing 20 Pods at a time. Let add infinite scrolling.
Goals
- Add infinite scrolling
- Ensure pods are unique
Steps
- Append to pods instead of replacing it
- Animate the change
- getPods() when last row appears
- Ensure pods are unique
1, 2 Append and Animate
private func getPods() async {
if let response = await Network().getPods() {
withAnimation {
pods += response
}
}
}
3 getPods() when last row appears
NavigationLink(destination: PodDetailView(pod: pod)) {
Text(pod.title)
}
.task {
if pod == pods.last {
await getPods()
}
}
4 Ensure pods are unique
Adapted from SwiftLee.
// from https://www.avanderlee.com/swift/unique-values-removing-duplicates-array/
public extension Array where Element: Hashable {
func uniqued() -> [Element] {
var seen = Set<Element>()
return filter{ seen.insert($0).inserted }
}
}