From 2292d04f918403fa2d7296157caa044b3fbce258 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Thu, 2 Jul 2020 17:36:50 -0500 Subject: [PATCH] Revert to using List for timeline and use infinite scrolling technique to speed up timeline loads --- .../Shared/Timeline/TimelineItemView.swift | 1 - .../Shared/Timeline/TimelineModel.swift | 21 ++++++++++++++++--- .../Shared/Timeline/TimelineView.swift | 9 ++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/Multiplatform/Shared/Timeline/TimelineItemView.swift b/Multiplatform/Shared/Timeline/TimelineItemView.swift index 62c7dfaad..d4f4f172a 100644 --- a/Multiplatform/Shared/Timeline/TimelineItemView.swift +++ b/Multiplatform/Shared/Timeline/TimelineItemView.swift @@ -45,7 +45,6 @@ struct TimelineItemView: View { } } } - Divider() } .onAppear { articleIconImageLoader.loadImage(for: timelineItem.article) diff --git a/Multiplatform/Shared/Timeline/TimelineModel.swift b/Multiplatform/Shared/Timeline/TimelineModel.swift index d89da96a1..307bf0f03 100644 --- a/Multiplatform/Shared/Timeline/TimelineModel.swift +++ b/Multiplatform/Shared/Timeline/TimelineModel.swift @@ -55,6 +55,14 @@ class TimelineModel: ObservableObject { fetchAndReplaceArticlesAsync() } + // TODO: Replace this with ScrollViewReader if we have to keep it + func loadMoreTimelineItemsIfNecessary(_ timelineItem: TimelineItem) { + let thresholdIndex = timelineItems.index(timelineItems.endIndex, offsetBy: -10) + if timelineItems.firstIndex(where: { $0.id == timelineItem.id }) == thresholdIndex { + nextBatch() + } + } + } // MARK: Private @@ -112,12 +120,19 @@ private extension TimelineModel { func replaceArticles(with unsortedArticles: Set
) { articles = Array(unsortedArticles).sortedByDate(sortDirection ? .orderedDescending : .orderedAscending, groupByFeed: groupByFeed) - timelineItems = articles.map { TimelineItem(article: $0) } - + timelineItems = [TimelineItem]() + nextBatch() // TODO: Update unread counts and other item done in didSet on AppKit } - + func nextBatch() { + let rangeEndIndex = timelineItems.endIndex + 50 > articles.endIndex ? articles.endIndex : timelineItems.endIndex + 50 + let range = timelineItems.endIndex..