Implement database.fetchUnreadCounts.

This commit is contained in:
Brent Simmons 2017-09-01 13:31:27 -07:00
parent 66ae62233f
commit a00ac9c2ac
2 changed files with 19 additions and 144 deletions

View File

@ -92,21 +92,33 @@ final class ArticlesTable: DatabaseTable {
return fetchUnreadArticles(feeds.feedIDs())
}
// MARK: Updating
func update(_ feed: Feed, _ parsedFeed: ParsedFeed, _ completion: @escaping RSVoidCompletionBlock) {
// TODO
}
// MARK: Unread Counts
func fetchUnreadCounts(_ feeds: Set<Feed>, _ completion: @escaping UnreadCountCompletionBlock) {
// TODO
let feedIDs = feeds.feedIDs()
var unreadCountTable = UnreadCountTable()
queue.fetch { (database) in
for feedID in feedIDs {
unreadCountTable[feedID] = self.fetchUnreadCount(feedID, database)
}
DispatchQueue.main.async() {
completion(unreadCountTable)
}
}
}
// MARK: Status
func mark(_ articles: Set<Article>, _ statusKey: String, _ flag: Bool) {
@ -135,31 +147,6 @@ final class ArticlesTable: DatabaseTable {
self.statusesTable.markArticleIDs(Set(articleIDs), statusKey, flag, database)
}
}
// typealias FeedCountCallback = (Int) -> Void
//
// func numberOfArticlesWithFeedID(_ feedID: String, callback: @escaping FeedCountCallback) {
//
// queue.fetch { (database: FMDatabase!)
//
// let sql = "select count(*) from articles where feedID = ?;"
// var numberOfArticles = -1
//
// if let resultSet = database.executeQuery(sql, withArgumentsIn: [feedID]) {
//
// while (resultSet.next()) {
// numberOfArticles = resultSet.long(forColumnIndex: 0)
// break
// }
// }
//
// DispatchQueue.main.async() {
// callback(numberOfArticles)
// }
// }
//
// }
}
// MARK: -

View File

@ -60,22 +60,9 @@ public final class Database {
// MARK: - Unread Counts
public func fetchUnreadCounts(for feeds: Set<Feed>, completion: @escaping UnreadCountCompletionBlock) {
public func fetchUnreadCounts(for feeds: Set<Feed>, _ completion: @escaping UnreadCountCompletionBlock) {
return articlesTable.fetchUnreadCounts(feeds, completion)
// let feedIDs = feeds.feedIDs()
//
// queue.fetch { (database: FMDatabase!) -> Void in
//
// var unreadCounts = UnreadCountTable()
// for oneFeedID in feedIDs {
// unreadCounts[oneFeedID] = self.unreadCount(oneFeedID, database)
// }
//
// DispatchQueue.main.async() {
// completion(unreadCounts)
// }
// }
articlesTable.fetchUnreadCounts(feeds, completion)
}
// MARK: - Updating Articles
@ -101,9 +88,8 @@ public final class Database {
// MARK: - Status
public func mark(_ articles: Set<Article>, statusKey: String, flag: Bool) {
articlesTable.mark(articles, statusKey, flag)
// statusesTable.markArticles(articles, statusKey: statusKey, flag: flag)
}
}
@ -112,74 +98,7 @@ public final class Database {
private extension Database {
// func feedIDCountDictionariesWithResultSet(_ resultSet: FMResultSet) -> [String: Int] {
//
// var counts = [String: Int]()
//
// while (resultSet.next()) {
//
// if let oneFeedID = resultSet.string(forColumnIndex: 0) {
// let count = resultSet.int(forColumnIndex: 1)
// counts[oneFeedID] = Int(count)
// }
// }
//
// return counts
// }
// func countsForAllFeeds(_ database: FMDatabase) -> [String: Int] {
//
// let sql = "select distinct feedID, count(*) as count from articles group by feedID;"
//
// if let resultSet = database.executeQuery(sql, withArgumentsIn: []) {
// return feedIDCountDictionariesWithResultSet(resultSet)
// }
//
// return [String: Int]()
// }
// func countsForFeedIDs(_ feedIDs: [String], _ database: FMDatabase) -> [String: Int] {
//
// let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
// let sql = "select distinct feedID, count(*) from articles where feedID in \(placeholders) group by feedID;"
// logSQL(sql)
//
// if let resultSet = database.executeQuery(sql, withArgumentsIn: feedIDs) {
// return feedIDCountDictionariesWithResultSet(resultSet)
// }
//
// return [String: Int]()
//
// }
// func fetchUnreadArticlesForFeedIDs(_ feedIDs: [String]) -> Set<Article> {
//
// if feedIDs.isEmpty {
// return Set<Article>()
// }
//
// var fetchedArticles = Set<Article>()
// var counts = [String: Int]()
//
// queue.fetchSync { (database: FMDatabase!) -> Void in
//
// counts = self.countsForFeedIDs(feedIDs, database)
//
// // select * from articles natural join statuses where feedID in ('http://ranchero.com/xml/rss.xml') and read = 0
//
// let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))!
// let sql = "select * from articles natural join statuses where feedID in \(placeholders) and read=0;"
// logSQL(sql)
//
// if let resultSet = database.executeQuery(sql, withArgumentsIn: feedIDs) {
// fetchedArticles = self.articlesWithResultSet(resultSet)
// }
// }
//
// let articles = articleCache.uniquedArticles(fetchedArticles, statusesTable: statusesTable)
// return filteredArticles(articles, feedCounts: counts)
// }
// MARK: Saving Articles
@ -303,17 +222,6 @@ private extension Database {
// return result
// }
//
// // MARK: Unread counts
//
// func numberOfArticles(_ feedID: String, _ database: FMDatabase) -> Int {
//
// let sql = "select count(*) from articles where feedID = ?;"
// logSQL(sql)
//
// return numberWithSQLAndParameters(sql, parameters: [feedID], database)
// }
//
//
// // MARK: Filtering out old articles
//
// func articleIsOlderThanCutoffDate(_ article: Article) -> Bool {
@ -324,11 +232,6 @@ private extension Database {
// return false
// }
//
// func articleShouldBeSavedForever(_ article: Article) -> Bool {
//
// return article.status.starred
// }
//
// func articleShouldAppearToUser(_ article: Article, _ numberOfArticlesInFeed: Int) -> Bool {
//
// if numberOfArticlesInFeed <= minimumNumberOfArticles {
@ -337,21 +240,6 @@ private extension Database {
// return articleShouldBeSavedForever(article) || !articleIsOlderThanCutoffDate(article)
// }
//
// private static let minimumNumberOfArticlesInFeed = 10
//
// func filteredArticles(_ articles: Set<Article>, feedCounts: [String: Int]) -> Set<Article> {
//
// var articlesSet = Set<Article>()
//
// for oneArticle in articles {
// if let feedCount = feedCounts[oneArticle.feedID], articleShouldAppearToUser(oneArticle, feedCount) {
// articlesSet.insert(oneArticle)
// }
//
// }
//
// return articlesSet
// }
//
// func deletePossibleOldArticles(_ articles: Set<Article>) {
//