Skip to content

Commit

Permalink
Merge pull request weiran#222 from JeffreyCA/master
Browse files Browse the repository at this point in the history
Fix duplicates in feed and next page fetching for "New" and "Jobs" posts
  • Loading branch information
weiran authored Dec 5, 2021
2 parents 9654c16 + 5089154 commit 2247ff7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
17 changes: 14 additions & 3 deletions App/Feed/FeedViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import PromiseKit

class FeedViewModel {
var posts: [Post] = []
var postIds: Set<Int> = Set()
var postType: PostType = .news
var pageIndex = 1
var lastPostId = 0
var isFetching = false

func fetchFeed(fetchNextPage: Bool = false) -> Promise<Void> {
Expand All @@ -21,22 +23,31 @@ class FeedViewModel {
}

if fetchNextPage {
pageIndex += 1
if postType == .newest || postType == .jobs {
lastPostId = posts.last?.id ?? lastPostId
} else {
pageIndex += 1
}
}

isFetching = true

return firstly {
HackersKit.shared.getPosts(type: postType, page: pageIndex)
HackersKit.shared.getPosts(type: postType, page: pageIndex, nextId: lastPostId)
}.done { posts in
self.posts.append(contentsOf: posts)
let newPosts = posts.filter { !self.postIds.contains($0.id) }
let newPostIds = newPosts.map { $0.id }
self.posts.append(contentsOf: newPosts)
self.postIds.formUnion(newPostIds)
self.isFetching = false
}
}

func reset() {
posts = []
postIds = Set()
pageIndex = 1
lastPostId = 0
isFetching = false
}

Expand Down
13 changes: 9 additions & 4 deletions Shared Frameworks/HackersKit/HackersKit+PostsList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ import PromiseKit
import SwiftSoup

extension HackersKit {
func getPosts(type: PostType, page: Int = 1) -> Promise<[Post]> {
func getPosts(type: PostType, page: Int = 1, nextId: Int = 0) -> Promise<[Post]> {
firstly {
fetchPostsHtml(type: type, page: page)
fetchPostsHtml(type: type, page: page, nextId: nextId)
}.map { html in
try HtmlParser.postsTableElement(from: html)
}.compactMap { tableElement in
try HtmlParser.posts(from: tableElement, type: type)
}
}

private func fetchPostsHtml(type: PostType, page: Int) -> Promise<String> {
let url = URL(string: "https://news.ycombinator.com/\(type.rawValue)?p=\(page)")!
private func fetchPostsHtml(type: PostType, page: Int, nextId: Int) -> Promise<String> {
var url: URL
if type == .newest || type == .jobs {
url = URL(string: "https://news.ycombinator.com/\(type.rawValue)?next=\(nextId)")!
} else {
url = URL(string: "https://news.ycombinator.com/\(type.rawValue)?p=\(page)")!
}
return fetchHtml(url: url)
}
}

0 comments on commit 2247ff7

Please sign in to comment.