Skip to content

Commit

Permalink
Accurate page counts in filtered threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
kahays committed Jan 15, 2022
1 parent 3335f85 commit 9884545
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions Awful.apk/src/main/java/com/ferg/awfulapp/thread/ForumParsing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -393,28 +393,41 @@ class ThreadPageParseTask(
firstPostOnPageIndex
)
val postsOnPreviousPages = (pageNumber - 1) * postsPerPage
// calculate the read total by counting posts on this + preceding pages - only update the read count if it has grown (e.g. going back to an old page will give a lower count)
val postsRead = (postsOnPreviousPages + postsOnThisPage).coerceAtLeast(readCount)
val minimumPostsRead = postsOnPreviousPages + postsOnThisPage
// only update the read count if it has grown (e.g. going back to an old page will give a lower count)
val totalPostsRead = minimumPostsRead.coerceAtLeast(readCount)

// post count is used for pagination (downstream.)
//
// post count should be an estimate of the total number of posts in our "virtual thread," i.e. even
// if we are only viewing posts by a specific user. this function is not aware if we are viewing a
// filtered version of the thread, so we detect that and return proper values for pagination.
//
// because the last page number accounts for a filtered thread, we can calculate minimum and maximum
// post ranges to constrain the post count properly.
val minPosts = (lastPageNumber - 1) * postsPerPage + 1 // one post on the last page, any preceding pages are full
val maxPosts = lastPageNumber * postsPerPage // all pages full

postCount = if (pageNumber == lastPageNumber) {
// this is the last page, so we've read all the posts in the thread
postsRead
// if total posts are outside of our min/max, we're reading a filtered thread and should constrain the
// count accordingly.
if (totalPostsRead in minPosts..maxPosts) totalPostsRead else minimumPostsRead
} else {
// not the last page, so we can't tell how many posts the thread has, we have to estimate it
// we can calculate a minimum and maximum posts range by looking at the last page number
val minPosts =
(lastPageNumber - 1) * postsPerPage + 1 // one post on the last page, any preceding pages are full
val maxPosts = lastPageNumber * postsPerPage // all pages full
// if the old post count is within this range, let's just assume it's more accurate than taking the minimum
// if it's outside of that range it's obviously a stale value, use the min as our best guess
// if we're not on the last page, doesn't matter whether the thread is filtered or unfiltered, we can't tell
// exactly how many posts the thread has, we have to estimate it.
//
// if the old post count is within min/max range, let's just assume it's more accurate than taking the minimum.
// if it's outside of that range it's a stale value, use the min as our best guess.
if (postCount in minPosts..maxPosts) postCount else minPosts
}

// TODO: 16/06/2017 would it be better to store postCount and postsRead in the DB, and calculate the unread count from that?
unreadCount = postCount - postsRead
//
unreadCount = postCount - totalPostsRead

Timber.d(
"getThreadPosts: Thread ID %d, page %d of %d, %d posts on page%n%d posts total: %d read/%d unread",
id, pageNumber, lastPageNumber, postsOnThisPage, postCount, postsRead, unreadCount
id, pageNumber, lastPageNumber, postsOnThisPage, postCount, totalPostsRead, unreadCount
)

}
Expand Down

0 comments on commit 9884545

Please sign in to comment.