Skip to content

Commit

Permalink
Automatically load the next page when the user reaches the end
Browse files Browse the repository at this point in the history
  • Loading branch information
avery-pierce committed Feb 20, 2021
1 parent 5039f3d commit 2bc34f6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
21 changes: 15 additions & 6 deletions Cavy/Views/Screens/Listing/EndlessPostListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct EndlessPostListView: View {
}

var body: some View {
ListingView(postLoader.posts)
ListingView(postLoader.posts, isNextPageLoading: postLoader.nextPageLoading, onLoadNextPage: { postLoader.loadNextPage() })
.onAppear(perform: {
postLoader.loadFirstPage()
})
Expand Down Expand Up @@ -90,25 +90,34 @@ class EndlessPostLoader: ObservableObject {
})

@Published var posts: [CavyPost] = []
@Published var nextPageLoading: Bool = false

private var cancelBag = Set<AnyCancellable>()

init(_ intent: ListingIntent) {
self.intent = intent

loadedPosts
.breakpoint()
.sink { (posts) in
self.posts = posts
}.store(in: &cancelBag)
.assign(to: \.posts, on: self)
.store(in: &cancelBag)

isNextPageLoading
.assign(to: \.nextPageLoading, on: self)
.store(in: &cancelBag)
}

func loadNextPage() {
let nextResource = intent.createResource(pageNumber: nextPageNumber)
let nextResource = intent.createResource(limit: 50, pageNumber: nextPageNumber)
pages.append(nextResource)
nextResource.load()
}

func loadNextPageIfNeeded() {
if !nextPageLoading {
loadNextPage()
}
}

func loadFirstPage() {
guard pages.isEmpty else { return }
loadNextPage()
Expand Down
22 changes: 19 additions & 3 deletions Cavy/Views/Screens/Listing/ListingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ import SwiftUI
struct ListingView: View {
@Environment(\.lemmyAPIClient) var client
let posts: [CavyPost]
init(_ posts: [CavyPost]) {
let isNextPageLoading: Bool
let onLoadNextPage: (() -> Void)?

init(_ posts: [CavyPost], isNextPageLoading: Bool = false, onLoadNextPage: (() -> Void)? = nil) {
self.posts = posts
self.isNextPageLoading = isNextPageLoading
self.onLoadNextPage = onLoadNextPage
}

init(_ postListing: CavyPostListing) {
self.posts = postListing.cavyPosts
init(_ postListing: CavyPostListing, isNextPageLoading: Bool = false, onLoadNextPage: (() -> Void)? = nil) {
self.init(postListing.cavyPosts,
isNextPageLoading: isNextPageLoading,
onLoadNextPage: onLoadNextPage)
}

var body: some View {
Expand All @@ -35,6 +42,15 @@ struct ListingView: View {
}
.listRowInsets(EdgeInsets(top: 4.0, leading: 8.0, bottom: 4.0, trailing: 8.0))
}

HStack(alignment: .center) {
VStack(alignment: .center) {
if isNextPageLoading { ProgressView() }
}
}
.frame(height: 80, alignment: .center)
.frame(maxWidth: .infinity)
.onAppear(perform: { onLoadNextPage?() })
}
.listStyle(PlainListStyle())
}
Expand Down

0 comments on commit 2bc34f6

Please sign in to comment.