From c24ea4d1b525c66b359a29ac4da48c7fbbb4feb1 Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Sat, 12 Nov 2022 12:33:25 -0600 Subject: [PATCH] Code cleanup --- Mac/AppDelegate.swift | 36 ++++++++++------------------ iOS/AppDelegate.swift | 56 ++++++++++++++++--------------------------- 2 files changed, 34 insertions(+), 58 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 4dbe1da45..753883538 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -1050,41 +1050,31 @@ extension AppDelegate: NSWindowRestoration { private extension AppDelegate { func handleMarkAsRead(userInfo: [AnyHashable: Any]) { + markArticle(userInfo: userInfo, statusKey: .read) + } + + func handleMarkAsStarred(userInfo: [AnyHashable: Any]) { + markArticle(userInfo: userInfo, statusKey: .starred) + } + + func markArticle(userInfo: [AnyHashable: Any], statusKey: ArticleStatus.Key) { guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any], let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String, let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else { return } - let account = AccountManager.shared.existingAccount(with: accountID) - guard account != nil else { + guard let account = AccountManager.shared.existingAccount(with: accountID) else { logger.debug("No account found from notification.") return } - let article = try? account!.fetchArticles(.articleIDs([articleID])) - guard article != nil else { + + guard let articles = try? account.fetchArticles(.articleIDs([articleID])), !articles.isEmpty else { logger.debug("No article found from search using: \(articleID, privacy: .public)") return } - account!.markArticles(article!, statusKey: .read, flag: true) { _ in } + + account.mark(articles: articles, statusKey: statusKey, flag: true) { _ in } } - func handleMarkAsStarred(userInfo: [AnyHashable: Any]) { - guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any], - let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String, - let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else { - return - } - let account = AccountManager.shared.existingAccount(with: accountID) - guard account != nil else { - logger.debug("No account found from notification.") - return - } - let article = try? account!.fetchArticles(.articleIDs([articleID])) - guard article != nil else { - logger.debug("No article found from search using: \(articleID, privacy: .public)") - return - } - account!.markArticles(article!, statusKey: .starred, flag: true) { _ in } - } } diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 0bac49d03..75e32552a 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -10,6 +10,7 @@ import UIKit import RSCore import RSWeb import Account +import Articles import BackgroundTasks import Secrets import WidgetKit @@ -432,55 +433,40 @@ private extension AppDelegate { private extension AppDelegate { func handleMarkAsRead(userInfo: [AnyHashable: Any]) { - guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any], - let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String, - let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else { - return - } - resumeDatabaseProcessingIfNecessary() - let account = AccountManager.shared.existingAccount(with: accountID) - guard account != nil else { - logger.debug("No account found from notification.") - return - } - let article = try? account!.fetchArticles(.articleIDs([articleID])) - guard article != nil else { - logger.debug("No account found from search using \(articleID, privacy: .public)") - return - } - account!.markArticles(article!, statusKey: .read, flag: true) { _ in } - self.prepareAccountsForBackground() - account!.syncArticleStatus(completion: { [weak self] _ in - if !AccountManager.shared.isSuspended { - self?.prepareAccountsForBackground() - self?.suspendApplication() - } - }) + markArticle(userInfo: userInfo, statusKey: .read) } func handleMarkAsStarred(userInfo: [AnyHashable: Any]) { + markArticle(userInfo: userInfo, statusKey: .starred) + } + + func markArticle(userInfo: [AnyHashable: Any], statusKey: ArticleStatus.Key) { guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any], let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String, let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String else { return } + resumeDatabaseProcessingIfNecessary() - let account = AccountManager.shared.existingAccount(with: accountID) - guard account != nil else { + + guard let account = AccountManager.shared.existingAccount(with: accountID) else { logger.debug("No account found from notification.") return } - let article = try? account!.fetchArticles(.articleIDs([articleID])) - guard article != nil else { + + guard let articles = try? account.fetchArticles(.articleIDs([articleID])), !articles.isEmpty else { logger.debug("No article found from search using \(articleID, privacy: .public)") return } - account!.markArticles(article!, statusKey: .starred, flag: true) { _ in } - account!.syncArticleStatus(completion: { [weak self] _ in - if !AccountManager.shared.isSuspended { - self?.prepareAccountsForBackground() - self?.suspendApplication() - } - }) + + account.mark(articles: articles, statusKey: statusKey, flag: true) { [weak self] _ in + account.syncArticleStatus(completion: { [weak self] _ in + if !AccountManager.shared.isSuspended { + self?.prepareAccountsForBackground() + self?.suspendApplication() + } + }) + } } + }