From efe8a4ab159aa60e83e7ee2ebebd66da670acadb Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Mon, 18 Feb 2019 22:29:43 -0800 Subject: [PATCH] =?UTF-8?q?Implement=20search=20=E2=80=94=20fetch=20articl?= =?UTF-8?q?es=20matching=20a=20search=20string=20from=20the=20database.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Frameworks/Account/Account.swift | 8 ++++++++ .../ArticlesDatabase/ArticlesDatabase.swift | 18 ++++-------------- .../ArticlesDatabase/ArticlesTable.swift | 14 ++++++++++++++ NetNewsWire/AppDelegate.swift | 8 ++++++++ NetNewsWire/Base.lproj/Main.storyboard | 6 ++++++ 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 31654ea8b..5ce5534f7 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -496,6 +496,14 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, #endif } + public func debugRunSearch() { + #if DEBUG + database.fetchArticlesMatching("Brent Simmons") { (articles) in + print(articles) + } + #endif + } + // MARK: - Notifications @objc func downloadProgressDidChange(_ note: Notification) { diff --git a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift index d369dcb7c..8443efdb9 100644 --- a/Frameworks/ArticlesDatabase/ArticlesDatabase.swift +++ b/Frameworks/ArticlesDatabase/ArticlesDatabase.swift @@ -25,7 +25,6 @@ public final class ArticlesDatabase { private let articlesTable: ArticlesTable public init(databaseFilePath: String, accountID: String) { - self.accountID = accountID let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false) @@ -43,69 +42,60 @@ public final class ArticlesDatabase { // MARK: - Fetching Articles public func fetchArticles(for feedID: String) -> Set
{ - return articlesTable.fetchArticles(feedID) } public func fetchArticlesAsync(for feedID: String, _ resultBlock: @escaping ArticleResultBlock) { - articlesTable.fetchArticlesAsync(feedID, withLimits: true, resultBlock) } public func fetchUnreadArticles(for feedIDs: Set) -> Set
{ - return articlesTable.fetchUnreadArticles(for: feedIDs) } public func fetchTodayArticles(for feedIDs: Set) -> Set
{ - return articlesTable.fetchTodayArticles(for: feedIDs) } public func fetchStarredArticles(for feedIDs: Set) -> Set
{ - return articlesTable.fetchStarredArticles(for: feedIDs) } + public func fetchArticlesMatching(_ searchString: String, _ resultBlock: @escaping ArticleResultBlock) { + articlesTable.fetchArticlesMatching(searchString, resultBlock) + } + // MARK: - Unread Counts public func fetchUnreadCounts(for feedIDs: Set, _ completion: @escaping UnreadCountCompletionBlock) { - articlesTable.fetchUnreadCounts(feedIDs, completion) } public func fetchUnreadCount(for feedIDs: Set, since: Date, callback: @escaping (Int) -> Void) { - articlesTable.fetchUnreadCount(feedIDs, since, callback) } public func fetchStarredAndUnreadCount(for feedIDs: Set, callback: @escaping (Int) -> Void) { - articlesTable.fetchStarredAndUnreadCount(feedIDs, callback) } public func fetchAllNonZeroUnreadCounts(_ completion: @escaping UnreadCountCompletionBlock) { - articlesTable.fetchAllUnreadCounts(completion) } // MARK: - Saving and Updating Articles public func update(feedID: String, parsedFeed: ParsedFeed, completion: @escaping UpdateArticlesWithFeedCompletionBlock) { - return articlesTable.update(feedID, parsedFeed, completion) } // MARK: - Status public func mark(_ articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) -> Set? { - return articlesTable.mark(articles, statusKey, flag) } public func markEverywhereAsRead() { - articlesTable.markEverywhereAsRead() } } - diff --git a/Frameworks/ArticlesDatabase/ArticlesTable.swift b/Frameworks/ArticlesDatabase/ArticlesTable.swift index 96c840e06..058c37513 100644 --- a/Frameworks/ArticlesDatabase/ArticlesTable.swift +++ b/Frameworks/ArticlesDatabase/ArticlesTable.swift @@ -79,6 +79,15 @@ final class ArticlesTable: DatabaseTable { return fetchStarredArticles(feedIDs) } + func fetchArticlesMatching(_ searchString: String, _ resultBlock: @escaping ArticleResultBlock) { + queue.fetch { (database) in + let articles = self.fetchArticlesMatching(searchString, database) + DispatchQueue.main.async { + resultBlock(articles) + } + } + } + // MARK: Updating func update(_ feedID: String, _ parsedFeed: ParsedFeed, _ completion: @escaping UpdateArticlesWithFeedCompletionBlock) { @@ -425,6 +434,11 @@ private extension ArticlesTable { return articles } + func fetchArticlesMatching(_ searchString: String, _ database: FMDatabase) -> Set
{ + let whereClause = "(starred=1 or dateArrived>?) and userDeleted=0 and (textMatchesSearchString(title,?) or textMatchesSearchString(contentHTML,?) or textMatchesSearchString(contentText,?) or textMatchesSearchString(summary,?))" + let parameters: [AnyObject] = [articleCutoffDate as AnyObject, searchString as AnyObject, searchString as AnyObject, searchString as AnyObject, searchString as AnyObject] + return self.fetchArticlesWithWhereClause(database, whereClause: whereClause, parameters: parameters, withLimits: false) + } func articlesWithSQL(_ sql: String, _ parameters: [AnyObject], _ database: FMDatabase) -> Set
{ diff --git a/NetNewsWire/AppDelegate.swift b/NetNewsWire/AppDelegate.swift index d6bdf761d..b2faf8c8a 100644 --- a/NetNewsWire/AppDelegate.swift +++ b/NetNewsWire/AppDelegate.swift @@ -558,6 +558,14 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidations, } } +// MARK: - Debug Menu +extension AppDelegate { + + @IBAction func debugSearch(_ sender: Any?) { + AccountManager.shared.localAccount.debugRunSearch() + } +} + private extension AppDelegate { func createReaderWindow() -> MainWindowController { diff --git a/NetNewsWire/Base.lproj/Main.storyboard b/NetNewsWire/Base.lproj/Main.storyboard index 017eadecc..8043ab3cc 100644 --- a/NetNewsWire/Base.lproj/Main.storyboard +++ b/NetNewsWire/Base.lproj/Main.storyboard @@ -461,6 +461,12 @@ + + + + + +