diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index 08b567716..19ba89f6a 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -7,7 +7,6 @@ // import Foundation -import RSCore import RSDatabase import RSParser import Articles @@ -23,12 +22,9 @@ public typealias UpdateArticlesWithFeedCompletionBlock = (Set
?, Set Set
{ return fetchArticles{ self.fetchArticlesForFeedID(feedID, withLimits: true, $0) } @@ -59,7 +59,7 @@ final class ArticlesTable: DatabaseTable { return fetchArticlesWithWhereClause(database, whereClause: "articles.feedID = ?", parameters: [feedID as AnyObject], withLimits: withLimits) } - // MARK: - Fetch Articles by articleID + // MARK: - Fetching Articles by articleID func fetchArticles(articleIDs: Set) -> Set
{ return fetchArticles{ self.fetchArticles(articleIDs: articleIDs, $0) } @@ -79,7 +79,7 @@ final class ArticlesTable: DatabaseTable { return fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: false) } - // MARK: - Fetch Unread Articles + // MARK: - Fetching Unread Articles func fetchUnreadArticles(_ feedIDs: Set) -> Set
{ return fetchArticles{ self.fetchUnreadArticles(feedIDs, $0) } @@ -100,7 +100,7 @@ final class ArticlesTable: DatabaseTable { return fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: true) } - // MARK: - Fetch Today Articles + // MARK: - Fetching Today Articles func fetchTodayArticles(_ feedIDs: Set) -> Set
{ return fetchArticles{ self.fetchTodayArticles(feedIDs, $0) } @@ -124,7 +124,7 @@ final class ArticlesTable: DatabaseTable { return fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: false) } - // MARK: - Fetch Starred Articles + // MARK: - Fetching Starred Articles func fetchStarredArticles(_ feedIDs: Set) -> Set
{ return fetchArticles{ self.fetchStarredArticles(feedIDs, $0) } @@ -145,7 +145,7 @@ final class ArticlesTable: DatabaseTable { return fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: false) } - // MARK: - Fetch Search Articles + // MARK: - Fetching Search Articles func fetchArticlesMatching(_ searchString: String, _ feedIDs: Set) -> Set
{ var articles: Set
= Set
() @@ -180,7 +180,7 @@ final class ArticlesTable: DatabaseTable { return articles.filter{ feedIDs.contains($0.feedID) } } - // MARK: - Fetch Articles for Indexer + // MARK: - Fetching Articles for Indexer func fetchArticleSearchInfos(_ articleIDs: Set, in database: FMDatabase) -> Set? { let parameters = articleIDs.map { $0 as AnyObject } @@ -207,10 +207,9 @@ final class ArticlesTable: DatabaseTable { return nil } - // MARK: Updating + // MARK: - Updating func update(_ feedID: String, _ parsedItems: Set, _ read: Bool, _ completion: @escaping UpdateArticlesWithFeedCompletionBlock) { - if parsedItems.isEmpty { completion(nil, nil) return @@ -228,7 +227,6 @@ final class ArticlesTable: DatabaseTable { let articleIDs = Set(parsedItems.map { $0.articleID }) self.queue.update { (database) in - let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, read, database) //1 assert(statusesDictionary.count == articleIDs.count) @@ -264,7 +262,7 @@ final class ArticlesTable: DatabaseTable { if articleIDs.isEmpty { return } - DispatchQueue.main.async() { + DispatchQueue.main.async { self.searchTable.ensureIndexedArticles(for: articleIDs) } } @@ -278,10 +276,9 @@ final class ArticlesTable: DatabaseTable { } } - // MARK: Unread Counts + // MARK: - Unread Counts func fetchUnreadCounts(_ feedIDs: Set, _ completion: @escaping UnreadCountCompletionBlock) { - if feedIDs.isEmpty { completion(UnreadCountDictionary()) return @@ -290,19 +287,17 @@ final class ArticlesTable: DatabaseTable { var unreadCountDictionary = UnreadCountDictionary() queue.fetch { (database) in - for feedID in feedIDs { unreadCountDictionary[feedID] = self.fetchUnreadCount(feedID, database) } - DispatchQueue.main.async() { + DispatchQueue.main.async { completion(unreadCountDictionary) } } } func fetchUnreadCount(_ feedIDs: Set, _ since: Date, _ callback: @escaping (Int) -> Void) { - // Get unread count for today, for instance. if feedIDs.isEmpty { @@ -311,7 +306,6 @@ final class ArticlesTable: DatabaseTable { } queue.fetch { (database) in - let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and (datePublished > ? or (datePublished is null and dateArrived > ?)) and read=0 and userDeleted=0;" @@ -322,24 +316,22 @@ final class ArticlesTable: DatabaseTable { let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database) - DispatchQueue.main.async() { + DispatchQueue.main.async { callback(unreadCount) } } } func fetchAllUnreadCounts(_ completion: @escaping UnreadCountCompletionBlock) { - // Returns only where unreadCount > 0. let cutoffDate = articleCutoffDate queue.fetch { (database) in - let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 and userDeleted=0 and (starred=1 or dateArrived>?) group by feedID;" guard let resultSet = database.executeQuery(sql, withArgumentsIn: [cutoffDate]) else { - DispatchQueue.main.async() { + DispatchQueue.main.async { completion(UnreadCountDictionary()) } return @@ -353,34 +345,32 @@ final class ArticlesTable: DatabaseTable { } } - DispatchQueue.main.async() { + DispatchQueue.main.async { completion(d) } } } func fetchStarredAndUnreadCount(_ feedIDs: Set, _ callback: @escaping (Int) -> Void) { - if feedIDs.isEmpty { callback(0) return } queue.fetch { (database) in - let placeholders = NSString.rs_SQLValueList(withPlaceholders: UInt(feedIDs.count))! let sql = "select count(*) from articles natural join statuses where feedID in \(placeholders) and read=0 and starred=1 and userDeleted=0;" let parameters = Array(feedIDs) as [Any] let unreadCount = self.numberWithSQLAndParameters(sql, parameters, in: database) - DispatchQueue.main.async() { + DispatchQueue.main.async { callback(unreadCount) } } } - // MARK: Status + // MARK: - Statuses func fetchUnreadArticleIDs(_ callback: @escaping (Set) -> Void) { statusesTable.fetchUnreadArticleIDs(callback) @@ -402,7 +392,7 @@ final class ArticlesTable: DatabaseTable { return statuses } - // MARK: Indexing + // MARK: - Indexing func indexUnindexedArticles() { queue.fetch { (database) in @@ -427,7 +417,7 @@ final class ArticlesTable: DatabaseTable { private extension ArticlesTable { - // MARK: Fetching + // MARK: - Fetching private func fetchArticles(_ fetchMethod: @escaping ArticlesFetchMethod) -> Set
{ var articles = Set
() @@ -447,7 +437,6 @@ private extension ArticlesTable { } func articlesWithResultSet(_ resultSet: FMResultSet, _ database: FMDatabase) -> Set
{ - // 1. Create DatabaseArticles without related objects. // 2. Then fetch the related objects, given the set of articleIDs. // 3. Then create set of Articles with DatabaseArticles and related objects and return it. @@ -485,7 +474,6 @@ private extension ArticlesTable { } func makeDatabaseArticles(with resultSet: FMResultSet) -> Set { - let articles = resultSet.mapToSet { (row) -> DatabaseArticle? in // The resultSet is a result of a JOIN query with the statuses table, @@ -527,7 +515,6 @@ private extension ArticlesTable { } func fetchArticlesWithWhereClause(_ database: FMDatabase, whereClause: String, parameters: [AnyObject], withLimits: Bool) -> Set
{ - // Don’t fetch articles that shouldn’t appear in the UI. The rules: // * Must not be deleted. // * Must be either 1) starred or 2) dateArrived must be newer than cutoff date. @@ -543,7 +530,6 @@ private extension ArticlesTable { } func fetchUnreadCount(_ feedID: String, _ database: FMDatabase) -> Int { - // Count only the articles that would appear in the UI. // * Must be unread. // * Must not be deleted. @@ -587,33 +573,28 @@ private extension ArticlesTable { } func articlesWithSQL(_ sql: String, _ parameters: [AnyObject], _ database: FMDatabase) -> Set
{ - guard let resultSet = database.executeQuery(sql, withArgumentsIn: parameters) else { return Set
() } return articlesWithResultSet(resultSet, database) } - // MARK: Saving Parsed Items - + // MARK: - Saving Parsed Items func callUpdateArticlesCompletionBlock(_ newArticles: Set
?, _ updatedArticles: Set
?, _ completion: @escaping UpdateArticlesWithFeedCompletionBlock) { - DispatchQueue.main.async { completion(newArticles, updatedArticles) } } - // MARK: Save New Articles + // MARK: - Saving New Articles func findNewArticles(_ incomingArticles: Set
, _ fetchedArticlesDictionary: [String: Article]) -> Set
? { - let newArticles = Set(incomingArticles.filter { fetchedArticlesDictionary[$0.articleID] == nil }) return newArticles.isEmpty ? nil : newArticles } func findAndSaveNewArticles(_ incomingArticles: Set
, _ fetchedArticlesDictionary: [String: Article], _ database: FMDatabase) -> Set
? { //5 - guard let newArticles = findNewArticles(incomingArticles, fetchedArticlesDictionary) else { return nil } @@ -622,7 +603,6 @@ private extension ArticlesTable { } func saveNewArticles(_ articles: Set
, _ database: FMDatabase) { - saveRelatedObjectsForNewArticles(articles, database) if let databaseDictionaries = articles.databaseDictionaries() { @@ -631,17 +611,15 @@ private extension ArticlesTable { } func saveRelatedObjectsForNewArticles(_ articles: Set
, _ database: FMDatabase) { - let databaseObjects = articles.databaseObjects() authorsLookupTable.saveRelatedObjects(for: databaseObjects, in: database) attachmentsLookupTable.saveRelatedObjects(for: databaseObjects, in: database) } - // MARK: Update Existing Articles + // MARK: - Updating Existing Articles func articlesWithRelatedObjectChanges(_ comparisonKeyPath: KeyPath?>, _ updatedArticles: Set
, _ fetchedArticles: [String: Article]) -> Set
{ - return updatedArticles.filter{ (updatedArticle) -> Bool in if let fetchedArticle = fetchedArticles[updatedArticle.articleID] { return updatedArticle[keyPath: comparisonKeyPath] != fetchedArticle[keyPath: comparisonKeyPath] @@ -652,7 +630,6 @@ private extension ArticlesTable { } func updateRelatedObjects(_ comparisonKeyPath: KeyPath?>, _ updatedArticles: Set
, _ fetchedArticles: [String: Article], _ lookupTable: DatabaseLookupTable, _ database: FMDatabase) { - let articlesWithChanges = articlesWithRelatedObjectChanges(comparisonKeyPath, updatedArticles, fetchedArticles) if !articlesWithChanges.isEmpty { lookupTable.saveRelatedObjects(for: articlesWithChanges.databaseObjects(), in: database) @@ -660,13 +637,11 @@ private extension ArticlesTable { } func saveUpdatedRelatedObjects(_ updatedArticles: Set
, _ fetchedArticles: [String: Article], _ database: FMDatabase) { - updateRelatedObjects(\Article.authors, updatedArticles, fetchedArticles, authorsLookupTable, database) updateRelatedObjects(\Article.attachments, updatedArticles, fetchedArticles, attachmentsLookupTable, database) } func findUpdatedArticles(_ incomingArticles: Set
, _ fetchedArticlesDictionary: [String: Article]) -> Set
? { - let updatedArticles = incomingArticles.filter{ (incomingArticle) -> Bool in //6 if let existingArticle = fetchedArticlesDictionary[incomingArticle.articleID] { if existingArticle != incomingArticle { @@ -680,7 +655,6 @@ private extension ArticlesTable { } func findAndSaveUpdatedArticles(_ incomingArticles: Set
, _ fetchedArticlesDictionary: [String: Article], _ database: FMDatabase) -> Set
? { //6 - guard let updatedArticles = findUpdatedArticles(incomingArticles, fetchedArticlesDictionary) else { return nil } @@ -690,7 +664,6 @@ private extension ArticlesTable { func saveUpdatedArticles(_ updatedArticles: Set
, _ fetchedArticles: [String: Article], _ database: FMDatabase) { - saveUpdatedRelatedObjects(updatedArticles, fetchedArticles, database) for updatedArticle in updatedArticles { @@ -699,7 +672,6 @@ private extension ArticlesTable { } func saveUpdatedArticle(_ updatedArticle: Article, _ fetchedArticles: [String: Article], _ database: FMDatabase) { - // Only update exactly what has changed in the Article (if anything). // Untested theory: this gets us better performance and less database fragmentation. @@ -708,7 +680,6 @@ private extension ArticlesTable { saveNewArticles(Set([updatedArticle]), database) return } - guard let changesDictionary = updatedArticle.changesFrom(fetchedArticle), changesDictionary.count > 0 else { // Not unexpected. There may be no changes. return @@ -718,9 +689,7 @@ private extension ArticlesTable { } func statusIndicatesArticleIsIgnorable(_ status: ArticleStatus) -> Bool { - // Ignorable articles: either userDeleted==1 or (not starred and arrival date > 4 months). - if status.userDeleted { return true } @@ -731,9 +700,7 @@ private extension ArticlesTable { } func filterIncomingArticles(_ articles: Set
) -> Set
{ - // Drop Articles that we can ignore. - return Set(articles.filter{ !statusIndicatesArticleIsIgnorable($0.status) }) } } diff --git a/Frameworks/ArticlesDatabase/AttachmentsTable.swift b/Frameworks/ArticlesDatabase/AttachmentsTable.swift index f243c9a76..0a6067503 100644 --- a/Frameworks/ArticlesDatabase/AttachmentsTable.swift +++ b/Frameworks/ArticlesDatabase/AttachmentsTable.swift @@ -1,6 +1,6 @@ // // AttachmentsTable.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/15/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -17,14 +17,12 @@ final class AttachmentsTable: DatabaseRelatedObjectsTable { var cache = DatabaseObjectCache() init(name: String) { - self.name = name } - // MARK: DatabaseRelatedObjectsTable + // MARK: - DatabaseRelatedObjectsTable func objectWithRow(_ row: FMResultSet) -> DatabaseObject? { - if let attachment = Attachment(row: row) { return attachment as DatabaseObject } diff --git a/Frameworks/ArticlesDatabase/AuthorsTable.swift b/Frameworks/ArticlesDatabase/AuthorsTable.swift index 8d041f927..543e83af4 100644 --- a/Frameworks/ArticlesDatabase/AuthorsTable.swift +++ b/Frameworks/ArticlesDatabase/AuthorsTable.swift @@ -1,6 +1,6 @@ // // AuthorsTable.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/13/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -24,14 +24,12 @@ final class AuthorsTable: DatabaseRelatedObjectsTable { var cache = DatabaseObjectCache() init(name: String) { - self.name = name } - // MARK: DatabaseRelatedObjectsTable + // MARK: - DatabaseRelatedObjectsTable func objectWithRow(_ row: FMResultSet) -> DatabaseObject? { - if let author = Author(row: row) { return author as DatabaseObject } diff --git a/Frameworks/ArticlesDatabase/Constants.swift b/Frameworks/ArticlesDatabase/Constants.swift index 054f96919..e7b13b08e 100644 --- a/Frameworks/ArticlesDatabase/Constants.swift +++ b/Frameworks/ArticlesDatabase/Constants.swift @@ -1,6 +1,6 @@ // // Keys.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/3/17. // Copyright © 2017 Ranchero Software. All rights reserved. diff --git a/Frameworks/ArticlesDatabase/DatabaseArticle.swift b/Frameworks/ArticlesDatabase/DatabaseArticle.swift index 597decbf9..7ac09c90c 100644 --- a/Frameworks/ArticlesDatabase/DatabaseArticle.swift +++ b/Frameworks/ArticlesDatabase/DatabaseArticle.swift @@ -1,6 +1,6 @@ // // DatabaseArticle.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 9/21/17. // Copyright © 2017 Ranchero Software. All rights reserved. diff --git a/Frameworks/ArticlesDatabase/DatabaseObject+Database.swift b/Frameworks/ArticlesDatabase/DatabaseObject+Database.swift index 0d9c3d8e1..edc02e7de 100644 --- a/Frameworks/ArticlesDatabase/DatabaseObject+Database.swift +++ b/Frameworks/ArticlesDatabase/DatabaseObject+Database.swift @@ -1,6 +1,6 @@ // // DatabaseObject+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 9/13/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -13,13 +13,11 @@ import Articles extension Array where Element == DatabaseObject { func asAuthors() -> Set? { - let authors = Set(self.map { $0 as! Author }) return authors.isEmpty ? nil : authors } func asAttachments() -> Set? { - let attachments = Set(self.map { $0 as! Attachment }) return attachments.isEmpty ? nil : attachments } diff --git a/Frameworks/ArticlesDatabase/Extensions/Article+Database.swift b/Frameworks/ArticlesDatabase/Extensions/Article+Database.swift index 5ac4dbb09..36c5332f1 100644 --- a/Frameworks/ArticlesDatabase/Extensions/Article+Database.swift +++ b/Frameworks/ArticlesDatabase/Extensions/Article+Database.swift @@ -1,6 +1,6 @@ // // Article+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/3/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -14,12 +14,10 @@ import RSParser extension Article { init(databaseArticle: DatabaseArticle, accountID: String, authors: Set?, attachments: Set?) { - self.init(accountID: accountID, articleID: databaseArticle.articleID, feedID: databaseArticle.feedID, uniqueID: databaseArticle.uniqueID, title: databaseArticle.title, contentHTML: databaseArticle.contentHTML, contentText: databaseArticle.contentText, url: databaseArticle.url, externalURL: databaseArticle.externalURL, summary: databaseArticle.summary, imageURL: databaseArticle.imageURL, bannerImageURL: databaseArticle.bannerImageURL, datePublished: databaseArticle.datePublished, dateModified: databaseArticle.dateModified, authors: authors, attachments: attachments, status: databaseArticle.status) } init(parsedItem: ParsedItem, maximumDateAllowed: Date, accountID: String, feedID: String, status: ArticleStatus) { - let authors = Author.authorsWithParsedAuthors(parsedItem.authors) let attachments = Attachment.attachmentsWithParsedAttachments(parsedItem.attachments) @@ -135,7 +133,6 @@ extension Article: DatabaseObject { } public func relatedObjectsWithName(_ name: String) -> [DatabaseObject]? { - switch name { case RelationshipName.authors: return databaseObjectArray(with: authors) @@ -147,7 +144,6 @@ extension Article: DatabaseObject { } private func databaseObjectArray(with objects: Set?) -> [DatabaseObject]? { - guard let objects = objects else { return nil } @@ -158,12 +154,10 @@ extension Article: DatabaseObject { extension Set where Element == Article { func statuses() -> Set { - return Set(map { $0.status }) } func dictionary() -> [String: Article] { - var d = [String: Article]() for article in self { d[article.articleID] = article @@ -172,12 +166,10 @@ extension Set where Element == Article { } func databaseObjects() -> [DatabaseObject] { - return self.map{ $0 as DatabaseObject } } func databaseDictionaries() -> [DatabaseDictionary]? { - return self.compactMap { $0.databaseDictionary() } } } diff --git a/Frameworks/ArticlesDatabase/Extensions/ArticleStatus+Database.swift b/Frameworks/ArticlesDatabase/Extensions/ArticleStatus+Database.swift index fbf6f4e1a..b4d46650e 100644 --- a/Frameworks/ArticlesDatabase/Extensions/ArticleStatus+Database.swift +++ b/Frameworks/ArticlesDatabase/Extensions/ArticleStatus+Database.swift @@ -1,6 +1,6 @@ // // ArticleStatus+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/3/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -13,7 +13,6 @@ import Articles extension ArticleStatus { convenience init(articleID: String, dateArrived: Date, row: FMResultSet) { - let read = row.bool(forColumn: DatabaseKey.read) let starred = row.bool(forColumn: DatabaseKey.starred) let userDeleted = row.bool(forColumn: DatabaseKey.userDeleted) diff --git a/Frameworks/ArticlesDatabase/Extensions/Attachment+Database.swift b/Frameworks/ArticlesDatabase/Extensions/Attachment+Database.swift index 15cea3bb4..dc30e5bee 100644 --- a/Frameworks/ArticlesDatabase/Extensions/Attachment+Database.swift +++ b/Frameworks/ArticlesDatabase/Extensions/Attachment+Database.swift @@ -1,6 +1,6 @@ // // Attachment+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/4/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -14,7 +14,6 @@ import RSParser extension Attachment { init?(row: FMResultSet) { - guard let url = row.string(forColumn: DatabaseKey.url) else { return nil } @@ -22,19 +21,17 @@ extension Attachment { let attachmentID = row.string(forColumn: DatabaseKey.attachmentID) let mimeType = row.string(forColumn: DatabaseKey.mimeType) let title = row.string(forColumn: DatabaseKey.title) - let sizeInBytes = optionalIntForColumn(row, DatabaseKey.sizeInBytes) - let durationInSeconds = optionalIntForColumn(row, DatabaseKey.durationInSeconds) + let sizeInBytes = row.optionalIntForColumn(DatabaseKey.sizeInBytes) + let durationInSeconds = row.optionalIntForColumn(DatabaseKey.durationInSeconds) self.init(attachmentID: attachmentID, url: url, mimeType: mimeType, title: title, sizeInBytes: sizeInBytes, durationInSeconds: durationInSeconds) } init?(parsedAttachment: ParsedAttachment) { - self.init(attachmentID: nil, url: parsedAttachment.url, mimeType: parsedAttachment.mimeType, title: parsedAttachment.title, sizeInBytes: parsedAttachment.sizeInBytes, durationInSeconds: parsedAttachment.durationInSeconds) } static func attachmentsWithParsedAttachments(_ parsedAttachments: Set?) -> Set? { - guard let parsedAttachments = parsedAttachments else { return nil } @@ -42,17 +39,8 @@ extension Attachment { let attachments = parsedAttachments.compactMap{ Attachment(parsedAttachment: $0) } return attachments.isEmpty ? nil : Set(attachments) } - } -private func optionalIntForColumn(_ row: FMResultSet, _ columnName: String) -> Int? { - - let intValue = row.long(forColumn: columnName) - if intValue < 1 { - return nil - } - return intValue -} extension Attachment: DatabaseObject { @@ -79,15 +67,24 @@ extension Attachment: DatabaseObject { } +private extension FMResultSet { + + func optionalIntForColumn(_ columnName: String) -> Int? { + let intValue = long(forColumn: columnName) + if intValue < 1 { + return nil + } + return intValue + } +} + extension Set where Element == Attachment { func databaseDictionaries() -> [DatabaseDictionary] { - return self.compactMap { $0.databaseDictionary() } } func databaseObjects() -> [DatabaseObject] { - return self.compactMap { $0 as DatabaseObject } } } diff --git a/Frameworks/ArticlesDatabase/Extensions/Author+Database.swift b/Frameworks/ArticlesDatabase/Extensions/Author+Database.swift index 1b2d7fb43..d1d69b715 100644 --- a/Frameworks/ArticlesDatabase/Extensions/Author+Database.swift +++ b/Frameworks/ArticlesDatabase/Extensions/Author+Database.swift @@ -1,6 +1,6 @@ // // Author+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 7/8/17. // Copyright © 2017 Ranchero Software. All rights reserved. @@ -16,7 +16,6 @@ import RSParser extension Author { init?(row: FMResultSet) { - let authorID = row.string(forColumn: DatabaseKey.authorID) let name = row.string(forColumn: DatabaseKey.name) let url = row.string(forColumn: DatabaseKey.url) @@ -27,12 +26,10 @@ extension Author { } init?(parsedAuthor: ParsedAuthor) { - self.init(authorID: nil, name: parsedAuthor.name, url: parsedAuthor.url, avatarURL: parsedAuthor.avatarURL, emailAddress: parsedAuthor.emailAddress) } public static func authorsWithParsedAuthors(_ parsedAuthors: Set?) -> Set? { - guard let parsedAuthors = parsedAuthors else { return nil } @@ -49,7 +46,6 @@ extension Author: DatabaseObject { } public func databaseDictionary() -> DatabaseDictionary? { - var d: DatabaseDictionary = [DatabaseKey.authorID: authorID] if let name = name { d[DatabaseKey.name] = name diff --git a/Frameworks/ArticlesDatabase/Extensions/ParsedArticle+Database.swift b/Frameworks/ArticlesDatabase/Extensions/ParsedArticle+Database.swift index d89f707ab..271876057 100644 --- a/Frameworks/ArticlesDatabase/Extensions/ParsedArticle+Database.swift +++ b/Frameworks/ArticlesDatabase/Extensions/ParsedArticle+Database.swift @@ -1,6 +1,6 @@ // // ParsedArticle+Database.swift -// Database +// NetNewsWire // // Created by Brent Simmons on 9/18/17. // Copyright © 2017 Ranchero Software. All rights reserved. diff --git a/Frameworks/ArticlesDatabase/RelatedObjectsMap+Database.swift b/Frameworks/ArticlesDatabase/RelatedObjectsMap+Database.swift index 09a78b807..80c1f0692 100644 --- a/Frameworks/ArticlesDatabase/RelatedObjectsMap+Database.swift +++ b/Frameworks/ArticlesDatabase/RelatedObjectsMap+Database.swift @@ -13,7 +13,6 @@ import Articles extension RelatedObjectsMap { func attachments(for articleID: String) -> Set? { - if let objects = self[articleID] { return objects.asAttachments() } @@ -21,7 +20,6 @@ extension RelatedObjectsMap { } func authors(for articleID: String) -> Set? { - if let objects = self[articleID] { return objects.asAuthors() } diff --git a/Frameworks/ArticlesDatabase/SearchTable.swift b/Frameworks/ArticlesDatabase/SearchTable.swift index 608b01cab..d0c95838a 100644 --- a/Frameworks/ArticlesDatabase/SearchTable.swift +++ b/Frameworks/ArticlesDatabase/SearchTable.swift @@ -1,6 +1,6 @@ // // SearchTable.swift -// ArticlesDatabase +// NetNewsWire // // Created by Brent Simmons on 2/23/19. // Copyright © 2019 Ranchero Software. All rights reserved. @@ -109,10 +109,6 @@ private extension SearchTable { func insert(_ article: ArticleSearchInfo, _ database: FMDatabase) -> Int { let rowDictionary: DatabaseDictionary = [DatabaseKey.body: article.bodyForIndex, DatabaseKey.title: article.title ?? ""] -// rowDictionary[DatabaseKey.title] = article.title ?? "" -// rowDictionary[DatabaseKey.body] = article.bodyForIndex -// rowDictionary.setObject(article.title ?? "", forKey: DatabaseKey.title as NSString) -// rowDictionary.setObject(article.bodyForIndex, forKey: DatabaseKey.body as NSString) insertRow(rowDictionary, insertType: .normal, in: database) return Int(database.lastInsertRowId()) } diff --git a/Frameworks/ArticlesDatabase/StatusesTable.swift b/Frameworks/ArticlesDatabase/StatusesTable.swift index 39dffb167..e8b5078c3 100644 --- a/Frameworks/ArticlesDatabase/StatusesTable.swift +++ b/Frameworks/ArticlesDatabase/StatusesTable.swift @@ -22,14 +22,12 @@ final class StatusesTable: DatabaseTable { private let queue: RSDatabaseQueue init(queue: RSDatabaseQueue) { - self.queue = queue } - // MARK: Creating/Updating + // MARK: - Creating/Updating func ensureStatusesForArticleIDs(_ articleIDs: Set, _ read: Bool, _ database: FMDatabase) -> [String: ArticleStatus] { - // Check cache. let articleIDsMissingCachedStatus = articleIDsWithNoCachedStatus(articleIDs) if articleIDsMissingCachedStatus.isEmpty { @@ -48,17 +46,15 @@ final class StatusesTable: DatabaseTable { return statusesDictionary(articleIDs) } - // MARK: Marking + // MARK: - Marking @discardableResult func mark(_ statuses: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) -> Set? { - // Sets flag in both memory and in database. var updatedStatuses = Set() for status in statuses { - if status.boolStatus(forKey: statusKey) == flag { continue } @@ -76,7 +72,7 @@ final class StatusesTable: DatabaseTable { return updatedStatuses } - // MARK: Fetching + // MARK: - Fetching func fetchUnreadArticleIDs(_ callback: @escaping (Set) -> Void) { fetchArticleIDs("select articleID from statuses where read=0 and userDeleted=0;", callback) @@ -106,7 +102,6 @@ final class StatusesTable: DatabaseTable { } func statusWithRow(_ row: FMResultSet) -> ArticleStatus? { - guard let articleID = row.string(forColumn: DatabaseKey.articleID) else { return nil } @@ -125,7 +120,6 @@ final class StatusesTable: DatabaseTable { } func statusesDictionary(_ articleIDs: Set) -> [String: ArticleStatus] { - var d = [String: ArticleStatus]() for articleID in articleIDs { @@ -142,23 +136,20 @@ final class StatusesTable: DatabaseTable { private extension StatusesTable { - // MARK: Cache + // MARK: - Cache func articleIDsWithNoCachedStatus(_ articleIDs: Set) -> Set { - return Set(articleIDs.filter { cache[$0] == nil }) } - // MARK: Creating + // MARK: - Creating func saveStatuses(_ statuses: Set, _ database: FMDatabase) { - let statusArray = statuses.map { $0.databaseDictionary()! } self.insertRows(statusArray, insertType: .orIgnore, in: database) } func createAndSaveStatusesForArticleIDs(_ articleIDs: Set, _ read: Bool, _ database: FMDatabase) { - let now = Date() let statuses = Set(articleIDs.map { ArticleStatus(articleID: $0, read: read, dateArrived: now) }) cache.addIfNotCached(statuses) @@ -167,7 +158,6 @@ private extension StatusesTable { } func fetchAndCacheStatusesForArticleIDs(_ articleIDs: Set, _ database: FMDatabase) { - guard let resultSet = self.selectRowsWhere(key: DatabaseKey.articleID, inValues: Array(articleIDs), in: database) else { return } @@ -176,10 +166,9 @@ private extension StatusesTable { self.cache.addIfNotCached(statuses) } - // MARK: Marking + // MARK: - Marking func markArticleIDs(_ articleIDs: Set, _ statusKey: ArticleStatus.Key, _ flag: Bool, _ database: FMDatabase) { - updateRowsWithValue(NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: DatabaseKey.articleID, matches: Array(articleIDs), database: database) } } @@ -196,21 +185,17 @@ private final class StatusCache { } func add(_ statuses: Set) { - // Replaces any cached statuses. - for status in statuses { self[status.articleID] = status } } func addStatusIfNotCached(_ status: ArticleStatus) { - addIfNotCached(Set([status])) } func addIfNotCached(_ statuses: Set) { - // Does not replace already cached statuses. for status in statuses {