Add folder read fetch query.

This commit is contained in:
Maurice Parker 2019-11-22 10:21:30 -06:00
parent 387b867d71
commit 43744ec128
4 changed files with 79 additions and 12 deletions

View File

@ -47,7 +47,7 @@ public enum FetchType {
case starred case starred
case unread case unread
case today case today
case unreadForFolder(Folder) case folder(Folder, Bool)
case webFeed(WebFeed) case webFeed(WebFeed)
case articleIDs(Set<String>) case articleIDs(Set<String>)
case search(String) case search(String)
@ -589,8 +589,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
return fetchUnreadArticles() return fetchUnreadArticles()
case .today: case .today:
return fetchTodayArticles() return fetchTodayArticles()
case .unreadForFolder(let folder): case .folder(let folder, let readFilter):
return fetchArticles(folder: folder) if readFilter {
return fetchUnreadArticles(folder: folder)
} else {
return fetchArticles(folder: folder)
}
case .webFeed(let webFeed): case .webFeed(let webFeed):
return fetchArticles(webFeed: webFeed) return fetchArticles(webFeed: webFeed)
case .articleIDs(let articleIDs): case .articleIDs(let articleIDs):
@ -610,8 +614,12 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
fetchUnreadArticlesAsync(callback) fetchUnreadArticlesAsync(callback)
case .today: case .today:
fetchTodayArticlesAsync(callback) fetchTodayArticlesAsync(callback)
case .unreadForFolder(let folder): case .folder(let folder, let readFilter):
fetchArticlesAsync(folder: folder, callback) if readFilter {
return fetchUnreadArticlesAsync(folder: folder, callback)
} else {
return fetchArticlesAsync(folder: folder, callback)
}
case .webFeed(let webFeed): case .webFeed(let webFeed):
fetchArticlesAsync(webFeed: webFeed, callback) fetchArticlesAsync(webFeed: webFeed, callback)
case .articleIDs(let articleIDs): case .articleIDs(let articleIDs):
@ -887,10 +895,18 @@ private extension Account {
} }
func fetchArticles(folder: Folder) -> Set<Article> { func fetchArticles(folder: Folder) -> Set<Article> {
return fetchUnreadArticles(forContainer: folder) return fetchArticles(forContainer: folder)
} }
func fetchArticlesAsync(folder: Folder, _ callback: @escaping ArticleSetBlock) { func fetchArticlesAsync(folder: Folder, _ callback: @escaping ArticleSetBlock) {
fetchArticlesAsync(forContainer: folder, callback)
}
func fetchUnreadArticles(folder: Folder) -> Set<Article> {
return fetchUnreadArticles(forContainer: folder)
}
func fetchUnreadArticlesAsync(folder: Folder, _ callback: @escaping ArticleSetBlock) {
fetchUnreadArticlesAsync(forContainer: folder, callback) fetchUnreadArticlesAsync(forContainer: folder, callback)
} }
@ -945,6 +961,21 @@ private extension Account {
} }
func fetchArticles(forContainer container: Container) -> Set<Article> {
let feeds = container.flattenedWebFeeds()
let articles = database.fetchArticles(feeds.webFeedIDs())
validateUnreadCountsAfterFetchingUnreadArticles(feeds, articles)
return articles
}
func fetchArticlesAsync(forContainer container: Container, _ callback: @escaping ArticleSetBlock) {
let webFeeds = container.flattenedWebFeeds()
database.fetchArticlesAsync(webFeeds.webFeedIDs()) { [weak self] (articles) in
self?.validateUnreadCountsAfterFetchingUnreadArticles(webFeeds, articles)
callback(articles)
}
}
func fetchUnreadArticles(forContainer container: Container) -> Set<Article> { func fetchUnreadArticles(forContainer container: Container) -> Set<Article> {
let feeds = container.flattenedWebFeeds() let feeds = container.flattenedWebFeeds()
let articles = database.fetchUnreadArticles(feeds.webFeedIDs()) let articles = database.fetchUnreadArticles(feeds.webFeedIDs())

View File

@ -49,11 +49,20 @@ extension WebFeed: ArticleFetcher {
extension Folder: ArticleFetcher { extension Folder: ArticleFetcher {
public func fetchArticles() -> Set<Article> { public func fetchArticles() -> Set<Article> {
return fetchUnreadArticles() guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
return Set<Article>()
}
return account.fetchArticles(.folder(self, false))
} }
public func fetchArticlesAsync(_ callback: @escaping ArticleSetBlock) { public func fetchArticlesAsync(_ callback: @escaping ArticleSetBlock) {
fetchUnreadArticlesAsync(callback) guard let account = account else {
assertionFailure("Expected folder.account, but got nil.")
callback(Set<Article>())
return
}
account.fetchArticlesAsync(.folder(self, false), callback)
} }
public func fetchUnreadArticles() -> Set<Article> { public func fetchUnreadArticles() -> Set<Article> {
@ -61,7 +70,7 @@ extension Folder: ArticleFetcher {
assertionFailure("Expected folder.account, but got nil.") assertionFailure("Expected folder.account, but got nil.")
return Set<Article>() return Set<Article>()
} }
return account.fetchArticles(.unreadForFolder(self)) return account.fetchArticles(.folder(self, true))
} }
public func fetchUnreadArticlesAsync(_ callback: @escaping ArticleSetBlock) { public func fetchUnreadArticlesAsync(_ callback: @escaping ArticleSetBlock) {
@ -70,6 +79,6 @@ extension Folder: ArticleFetcher {
callback(Set<Article>()) callback(Set<Article>())
return return
} }
account.fetchArticlesAsync(.unreadForFolder(self), callback) account.fetchArticlesAsync(.folder(self, true), callback)
} }
} }

View File

@ -48,12 +48,16 @@ public final class ArticlesDatabase {
return articlesTable.fetchArticles(webFeedID) return articlesTable.fetchArticles(webFeedID)
} }
public func fetchArticles(_ webFeedIDs: Set<String>) -> Set<Article> {
return articlesTable.fetchArticles(webFeedIDs)
}
public func fetchArticles(articleIDs: Set<String>) -> Set<Article> { public func fetchArticles(articleIDs: Set<String>) -> Set<Article> {
return articlesTable.fetchArticles(articleIDs: articleIDs) return articlesTable.fetchArticles(articleIDs: articleIDs)
} }
public func fetchUnreadArticles(_ webFeedID: Set<String>) -> Set<Article> { public func fetchUnreadArticles(_ webFeedIDs: Set<String>) -> Set<Article> {
return articlesTable.fetchUnreadArticles(webFeedID) return articlesTable.fetchUnreadArticles(webFeedIDs)
} }
public func fetchTodayArticles(_ webFeedIDs: Set<String>) -> Set<Article> { public func fetchTodayArticles(_ webFeedIDs: Set<String>) -> Set<Article> {
@ -78,6 +82,10 @@ public final class ArticlesDatabase {
articlesTable.fetchArticlesAsync(webFeedID, callback) articlesTable.fetchArticlesAsync(webFeedID, callback)
} }
public func fetchArticlesAsync(_ webFeedIDs: Set<String>, _ callback: @escaping ArticleSetBlock) {
articlesTable.fetchArticlesAsync(webFeedIDs, callback)
}
public func fetchArticlesAsync(articleIDs: Set<String>, _ callback: @escaping ArticleSetBlock) { public func fetchArticlesAsync(articleIDs: Set<String>, _ callback: @escaping ArticleSetBlock) {
articlesTable.fetchArticlesAsync(articleIDs: articleIDs, callback) articlesTable.fetchArticlesAsync(articleIDs: articleIDs, callback)
} }

View File

@ -60,6 +60,25 @@ final class ArticlesTable: DatabaseTable {
return fetchArticlesWithWhereClause(database, whereClause: "articles.feedID = ?", parameters: [webFeedID as AnyObject], withLimits: withLimits) return fetchArticlesWithWhereClause(database, whereClause: "articles.feedID = ?", parameters: [webFeedID as AnyObject], withLimits: withLimits)
} }
func fetchArticles(_ webFeedIDs: Set<String>) -> Set<Article> {
return fetchArticles{ self.fetchArticles(webFeedIDs, $0) }
}
func fetchArticlesAsync(_ webFeedIDs: Set<String>, _ callback: @escaping ArticleSetBlock) {
fetchArticlesAsync({ self.fetchArticles(webFeedIDs, $0) }, callback)
}
private func fetchArticles(_ webFeedIDs: Set<String>, _ database: FMDatabase) -> Set<Article> {
// select * from articles natural join statuses where feedID in ('http://ranchero.com/xml/rss.xml') and read=0
if webFeedIDs.isEmpty {
return Set<Article>()
}
let parameters = webFeedIDs.map { $0 as AnyObject }
let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(webFeedIDs.count))!
let whereClause = "feedID in \(placeholders)"
return fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: true)
}
// MARK: - Fetching Articles by articleID // MARK: - Fetching Articles by articleID
func fetchArticles(articleIDs: Set<String>) -> Set<Article> { func fetchArticles(articleIDs: Set<String>) -> Set<Article> {