diff --git a/Frameworks/Database/ArticlesTable.swift b/Frameworks/Database/ArticlesTable.swift index 041d68faf..76bf6edf4 100644 --- a/Frameworks/Database/ArticlesTable.swift +++ b/Frameworks/Database/ArticlesTable.swift @@ -108,7 +108,29 @@ final class ArticlesTable: DatabaseTable { func mark(_ articles: Set
, _ statusKey: String, _ flag: Bool) { - // TODO + // Sets flag in both memory and in database. + + let articleIDs = articles.flatMap { (article) -> String? in + + guard let status = article.status else { + assertionFailure("Each article must have a status.") + return nil + } + + if status.boolStatus(forKey: statusKey) == flag { + return nil + } + status.setBoolStatus(flag, forKey: statusKey) + return article.articleID + } + + if articleIDs.isEmpty { + return + } + + queue.update { (database) in + self.statusesTable.markArticleIDs(Set(articleIDs), statusKey, flag, database) + } } diff --git a/Frameworks/Database/Extensions/Article+Database.swift b/Frameworks/Database/Extensions/Article+Database.swift index db7ebd8ec..109ced656 100644 --- a/Frameworks/Database/Extensions/Article+Database.swift +++ b/Frameworks/Database/Extensions/Article+Database.swift @@ -86,4 +86,9 @@ extension Set where Element == Article { return withNilProperty(\Article.status) } + + func statuses() -> Set { + + return Set(self.flatMap { $0.status }) + } } diff --git a/Frameworks/Database/StatusesTable.swift b/Frameworks/Database/StatusesTable.swift index bb4a4c98c..8737bbc81 100644 --- a/Frameworks/Database/StatusesTable.swift +++ b/Frameworks/Database/StatusesTable.swift @@ -76,14 +76,16 @@ final class StatusesTable: DatabaseTable { assert(articles.eachHasAStatus()) } -// func markArticles(_ articles: Set
, statusKey: String, flag: Bool) { -// -// // Main thread. -// -// assertNoMissingStatuses(articles) -// let statuses = Set(articles.flatMap { $0.status }) -// markArticleStatuses(statuses, statusKey: statusKey, flag: flag) -// } + // MARK: Marking + + func markArticleIDs(_ articleIDs: Set, _ statusKey: String, _ flag: Bool, _ database: FMDatabase) { + + updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey, whereKey: DatabaseKey.articleID, matches: Array(articleIDs), database: database) + } + + // MARK: Updating + + // func attachStatuses(_ articles: Set
, _ database: FMDatabase) { // @@ -155,16 +157,6 @@ private extension StatusesTable { } } -// func assertNoMissingStatuses(_ articles: Set
) { -// -// for oneArticle in articles { -// if oneArticle.status == nil { -// assertionFailure("All articles must have a status at this point.") -// return -// } -// } -// } - // MARK: Fetching // func fetchAndCacheStatusesForArticles(_ articles: Set
, _ database: FMDatabase) { @@ -203,34 +195,6 @@ private extension StatusesTable { // let status = ArticleStatus(articleID: articleID, row: row) // cache[articleID] = status // return status -// } - - // MARK: Updating - -// func markArticleStatuses(_ statuses: Set, statusKey: String, flag: Bool) { -// -// // Ignore the statuses where status.[statusKey] == flag. Update the remainder and save in database. -// -// var articleIDsToUpdate = Set() -// -// statuses.forEach { (oneStatus) in -// -// if oneStatus.boolStatus(forKey: statusKey) == flag { -// return -// } -// -// oneStatus.setBoolStatus(flag, forKey: statusKey) -// articleIDsToUpdate.insert(oneStatus.articleID) -// } -// -// if !articleIDsToUpdate.isEmpty { -// updateArticleStatusesInDatabase(articleIDsToUpdate, statusKey: statusKey, flag: flag) -// } -// } - -// private func updateArticleStatusesInDatabase(_ articleIDs: Set, statusKey: String, flag: Bool) { -// -// updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey, whereKey: DatabaseKey.articleID, matches: Array(articleIDs)) // } // MARK: Creating diff --git a/Frameworks/RSDatabase/DatabaseTable.swift b/Frameworks/RSDatabase/DatabaseTable.swift index 4aa758736..a390b5ad9 100644 --- a/Frameworks/RSDatabase/DatabaseTable.swift +++ b/Frameworks/RSDatabase/DatabaseTable.swift @@ -64,14 +64,11 @@ public extension DatabaseTable { // MARK: Updating -// public func updateRowsWithValue(_ value: Any, valueKey: String, whereKey: String, matches: [Any]) { -// -// queue.update { (database: FMDatabase!) in -// -// let _ = database.rs_updateRows(withValue: value, valueKey: valueKey, whereKey: whereKey, inValues: matches, tableName: self.name) -// } -// } - + public func updateRowsWithValue(_ value: Any, valueKey: String, whereKey: String, matches: [Any], database: FMDatabase) { + + let _ = database.rs_updateRows(withValue: value, valueKey: valueKey, whereKey: whereKey, inValues: matches, tableName: self.name) + } + // MARK: Saving public func insertRows(_ dictionaries: [NSDictionary], insertType: RSDatabaseInsertType, in database: FMDatabase) {