-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP - Scroll performance fixes for when there are many read posts already #66
Open
mikedg1
wants to merge
2
commits into
ejbills:main
Choose a base branch
from
mikedg1:featuree/241118-Scrol-performance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ struct SubredditFeedView: View { | |
let appTheme: AppThemeSettings | ||
let textSizePreference: TextSizePreference | ||
|
||
@State var readPostIDs: Set<String> = [] // Post IDs that are maked read, this is kept to improve scrolling performance rather than checking the CoreData for each new item | ||
|
||
@State private var forceCompactMode: Bool = false | ||
|
||
@State private var posts: [Post] = [] | ||
|
@@ -61,9 +63,7 @@ struct SubredditFeedView: View { | |
ThemedList(appTheme: appTheme, textSizePreference: textSizePreference, stripStyling: true) { | ||
if !posts.isEmpty && searchTerm.isEmpty { | ||
ForEach(posts, id: \.id) { post in | ||
var isRead: Bool { | ||
readPosts.contains(where: { $0.readPostId == post.id }) | ||
} | ||
let isRead = readPostIDs.contains(post.id) | ||
let justRead = readThisSession.contains(post.id) | ||
|
||
var isSaved: Bool { | ||
|
@@ -72,10 +72,11 @@ struct SubredditFeedView: View { | |
|
||
if !hideReadPosts || (!isRead || isSaved || (isRead && justRead)) { | ||
PostFeedItemView(post: post, isRead: isRead, forceCompactMode: forceCompactMode, isSaved: isSaved, appTheme: appTheme, textSizePreference: textSizePreference, useLargeThumbnail: useLargeThumbnailForMediaPreview) { | ||
handlePostTap(post, isRead: isRead) | ||
handlePostTap(post) | ||
} | ||
.if(markReadOnScroll, transform: { postFeedItem in | ||
postFeedItem.onScrolledOffTopOfScreen { | ||
readPostIDs.insert(post.id) | ||
PostUtils.shared.markRead(context: managedObjectContext, postId: post.id) | ||
readThisSession.insert(post.id) | ||
} | ||
|
@@ -141,18 +142,13 @@ struct SubredditFeedView: View { | |
} | ||
|
||
// MARK: - Private Methods | ||
|
||
private func handlePostAppearance(_ postId: String) { | ||
if !posts.isEmpty && posts.count > Int(Double(posts.count) * 0.85) { | ||
if postId == posts[Int(Double(posts.count) * 0.85)].id { | ||
scrapeSubreddit(lastPostAfter: lastPostAfter, sort: sortOption) | ||
} | ||
} | ||
} | ||
|
||
private func handlePostTap(_ post: Post, isRead: Bool) { | ||
private func handlePostTap(_ post: Post) { | ||
let isRead = readPostIDs.contains(post.id) | ||
|
||
coordinator.path.append(PostResponse(post: post)) | ||
if !isRead { | ||
readPostIDs.insert(post.id) | ||
|
||
PostUtils.shared.markRead(context: managedObjectContext, postId: post.id) | ||
if (!hideReadPostsImmediately) { | ||
readThisSession.insert(post.id) | ||
|
@@ -183,45 +179,61 @@ struct SubredditFeedView: View { | |
} | ||
} | ||
|
||
private func deferredTurnOffLoading() { | ||
defer { | ||
isLoading = false | ||
} | ||
} | ||
|
||
private func scrapeSubreddit(lastPostAfter: String? = nil, sort: SortOption? = nil, searchTerm: String = "", preventListIdRefresh: Bool = false) { | ||
self.isLoading = true | ||
if !preventListIdRefresh { self.listIdentifier = MiscUtils.randomString(length: 4) } | ||
|
||
if searchTerm.isEmpty { | ||
RedditScraper.scrapeSubreddit(subreddit: subredditName, lastPostAfter: lastPostAfter, sort: sort, | ||
trackingParamRemover: trackingParamRemover, over18: over18) { result in | ||
defer { | ||
isLoading = false | ||
} | ||
|
||
switch result { | ||
case .success(let newPosts): | ||
if newPosts.isEmpty && self.retryCount < 3 { // if a load fails, auto retry up to 3 times | ||
self.retryCount += 1 | ||
self.scrapeSubreddit(lastPostAfter: lastPostAfter, sort: sort, searchTerm: searchTerm, preventListIdRefresh: preventListIdRefresh) | ||
|
||
deferredTurnOffLoading() | ||
} else { | ||
self.retryCount = 0 | ||
for post in newPosts { | ||
if !postIDs.contains(post.id) { | ||
posts.append(post) | ||
postIDs.insert(post.id) | ||
DispatchQueue.global(qos: .background).async { | ||
self.retryCount = 0 | ||
|
||
var newReadIds: Set<String> = [] | ||
newPosts.forEach { post in | ||
let isRead = readPosts.contains(where: { $0.readPostId == post.id }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do the "slow" check on load, off the main thread. |
||
if isRead { | ||
newReadIds.insert(post.id) | ||
} | ||
} | ||
|
||
if let newLastPost = newPosts.last { | ||
self.lastPostAfter = newLastPost.id | ||
} | ||
|
||
DispatchQueue.main.async { | ||
posts += newPosts | ||
readPostIDs.formUnion(newReadIds) | ||
|
||
deferredTurnOffLoading() | ||
} | ||
} | ||
|
||
if let newLastPost = newPosts.last { | ||
self.lastPostAfter = newLastPost.id | ||
} | ||
} | ||
case .failure(let error): | ||
deferredTurnOffLoading() | ||
|
||
print("Error: \(error.localizedDescription)") | ||
} | ||
} | ||
} else { | ||
RedditScraper.search(query: searchTerm, searchType: "", sortBy: selectedSearchSortOption, topSortBy: selectedSearchTopOption, | ||
trackingParamRemover: trackingParamRemover, over18: over18) { result in | ||
defer { | ||
isLoading = false | ||
} | ||
deferredTurnOffLoading() | ||
|
||
switch result { | ||
case .success(let newMedia): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seemed to be the bottleneck.