From a00ac9c2acecc5efad930e2488458127f39dd0dc Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 1 Sep 2017 13:31:27 -0700 Subject: [PATCH] Implement database.fetchUnreadCounts. --- Frameworks/Database/ArticlesTable.swift | 45 ++++----- Frameworks/Database/Database.swift | 118 +----------------------- 2 files changed, 19 insertions(+), 144 deletions(-) diff --git a/Frameworks/Database/ArticlesTable.swift b/Frameworks/Database/ArticlesTable.swift index 5dc006ecf..37760eaae 100644 --- a/Frameworks/Database/ArticlesTable.swift +++ b/Frameworks/Database/ArticlesTable.swift @@ -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, _ 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
, _ 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: - diff --git a/Frameworks/Database/Database.swift b/Frameworks/Database/Database.swift index 38a8f032f..8fbbc10de 100644 --- a/Frameworks/Database/Database.swift +++ b/Frameworks/Database/Database.swift @@ -60,22 +60,9 @@ public final class Database { // MARK: - Unread Counts - public func fetchUnreadCounts(for feeds: Set, completion: @escaping UnreadCountCompletionBlock) { + public func fetchUnreadCounts(for feeds: Set, _ 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
, 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
{ - // - // if feedIDs.isEmpty { - // return Set
() - // } - // - // var fetchedArticles = Set
() - // 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
, feedCounts: [String: Int]) -> Set
{ -// -// var articlesSet = Set
() -// -// for oneArticle in articles { -// if let feedCount = feedCounts[oneArticle.feedID], articleShouldAppearToUser(oneArticle, feedCount) { -// articlesSet.insert(oneArticle) -// } -// -// } -// -// return articlesSet -// } // // func deletePossibleOldArticles(_ articles: Set
) { //