Skip to content

Commit

Permalink
Support for infinite scrolling in user gallery, scraps and favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceylo committed Nov 3, 2024
1 parent 37f3dd4 commit 495b06e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
2 changes: 2 additions & 0 deletions FurAffinity/Helper Views/ProgressiveLoadItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ struct ProgressiveLoadItem<DataType: ProgressiveData>: View {
.onChange(of: currentData) { _, newData in
if needsMoreData && newData.canLoadMore {
Task {
guard needsMoreData else { return }
try await Task.sleep(for: crawlingDelay)
guard needsMoreData else { return }
loadMoreData(newData)
}
}
Expand Down
18 changes: 14 additions & 4 deletions FurAffinity/User/RemoteUserGalleryLikeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ struct RemoteUserGalleryLikeView: View {
var body: some View {
RemoteView(url: url, contentsLoader: {
await model.session?.galleryLike(for: url)
}) { gallery, _ in
}, contentsViewBuilder: { gallery, updateHandler in
UserGalleryLikeView(
galleryType: galleryType,
gallery: gallery
)
}
gallery: gallery) { latestGallery in
guard let nextUrl = latestGallery.nextPageUrl else {
logger.error("Next page requested but there is none!")
return
}

Task {
let nextGallery = await model.session?.galleryLike(for: nextUrl)
let updated = nextGallery.map { latestGallery.appending($0) }
updateHandler.update(with: updated)
}
}
})
}
}

Expand Down
46 changes: 31 additions & 15 deletions FurAffinity/User/UserGalleryLikeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ enum GalleryType {
}
}

extension FAUserGalleryLike: ProgressiveData {
var canLoadMore: Bool {
nextPageUrl != nil
}
}

struct UserGalleryLikeView: View {
var galleryType: GalleryType
var gallery: FAUserGalleryLike
var loadMore: (_ galleryLike: FAUserGalleryLike) -> Void

var body: some View {
Group {
Expand All @@ -37,23 +44,30 @@ struct UserGalleryLikeView: View {
}
} else {
GeometryReader { geometry in
List(gallery.previews) { preview in
NavigationLink(
value: FAURL.submission(url: preview.url, previewData: preview)
) {
if galleryType.shouldDisplayAuthor {
SubmissionFeedItemView<AuthoredHeaderView>(submission: preview)
} else {
SubmissionFeedItemView<TitledHeaderView>(submission: preview)
List {
ForEach(gallery.previews) { preview in
NavigationLink(
value: FAURL.submission(url: preview.url, previewData: preview)
) {
if galleryType.shouldDisplayAuthor {
SubmissionFeedItemView<AuthoredHeaderView>(submission: preview)
} else {
SubmissionFeedItemView<TitledHeaderView>(submission: preview)
}
}
.id(preview.sid)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
}
.id(preview.sid)
.listRowSeparator(.hidden)
.listRowInsets(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))

ProgressiveLoadItem(
label: "Loading more submissions…",
currentData: gallery,
loadMoreData: loadMore
)
}
.listStyle(.plain)
.onAppear {
// Note: onAppear won't be enough once infinite scroll is implemented
.onChange(of: gallery.previews, initial: true) {
let thumbnailsWidth = geometry.size.width - 28.333
let previews = gallery.previews
prefetchThumbnails(for: previews, availableWidth: thumbnailsWidth)
Expand All @@ -72,7 +86,8 @@ struct UserGalleryLikeView: View {
NavigationStack {
UserGalleryLikeView(
galleryType: .favorites,
gallery: .init(displayAuthor: "Some User", previews: OfflineFASession.default.submissionPreviews)
gallery: .init(displayAuthor: "Some User", previews: OfflineFASession.default.submissionPreviews, nextPageUrl: nil),
loadMore: { _ in }
)
}
.environmentObject(Model.demo)
Expand All @@ -82,7 +97,8 @@ struct UserGalleryLikeView: View {
NavigationStack {
UserGalleryLikeView(
galleryType: .favorites,
gallery: .init(displayAuthor: "Some User", previews: [])
gallery: .init(displayAuthor: "Some User", previews: [], nextPageUrl: nil),
loadMore: { _ in }
)
}
.environmentObject(Model.empty)
Expand Down

0 comments on commit 495b06e

Please sign in to comment.