From 40a80356f1a1eaa7df981dff8f5abbe2631d773e Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 19 Aug 2019 16:03:07 -0500 Subject: [PATCH] Move business logic out of controller into coordinator where we typically keep it --- iOS/AppCoordinator.swift | 32 ++++++++++++++ .../MasterTimelineViewController.swift | 42 +++---------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/iOS/AppCoordinator.swift b/iOS/AppCoordinator.swift index d0c3dcd31..e7846a1de 100644 --- a/iOS/AppCoordinator.swift +++ b/iOS/AppCoordinator.swift @@ -555,18 +555,50 @@ class AppCoordinator: NSObject, UndoableCommandRunner { masterNavigationController.popViewController(animated: true) } + func markAsReadOlderArticlesInTimeline(_ indexPath: IndexPath) { + let article = articles[indexPath.row] + let articlesToMark = articles.filter { $0.logicalDatePublished < article.logicalDatePublished } + if articlesToMark.isEmpty { + return + } + + guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articlesToMark, markingRead: true, undoManager: undoManager) else { + return + } + runCommand(markReadCommand) + } + func toggleReadForCurrentArticle() { if let article = currentArticle { markArticles(Set([article]), statusKey: .read, flag: !article.status.read) } } + func toggleRead(for indexPath: IndexPath) { + let article = articles[indexPath.row] + guard let undoManager = undoManager, + let markReadCommand = MarkStatusCommand(initialArticles: [article], markingRead: !article.status.read, undoManager: undoManager) else { + return + } + runCommand(markReadCommand) + } + func toggleStarForCurrentArticle() { if let article = currentArticle { markArticles(Set([article]), statusKey: .starred, flag: !article.status.starred) } } + + func toggleStar(for indexPath: IndexPath) { + let article = articles[indexPath.row] + guard let undoManager = undoManager, + let markReadCommand = MarkStatusCommand(initialArticles: [article], markingStarred: !article.status.starred, undoManager: undoManager) else { + return + } + runCommand(markReadCommand) + } + func discloseFeed(_ feed: Feed) { masterNavigationController.popViewController(animated: true) masterFeedViewController.discloseFeed(feed) diff --git a/iOS/MasterTimeline/MasterTimelineViewController.swift b/iOS/MasterTimeline/MasterTimelineViewController.swift index 7471f4f41..52fcabc5c 100644 --- a/iOS/MasterTimeline/MasterTimelineViewController.swift +++ b/iOS/MasterTimeline/MasterTimelineViewController.swift @@ -130,7 +130,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner NSLocalizedString("Read", comment: "Read") let readAction = UIContextualAction(style: .normal, title: readTitle) { [weak self] (action, view, completionHandler) in - self?.toggleArticleReadStatus(article) + self?.coordinator.toggleRead(for: indexPath) completionHandler(true) } @@ -143,7 +143,7 @@ class MasterTimelineViewController: UITableViewController, UndoableCommandRunner NSLocalizedString("Star", comment: "Star") let starAction = UIContextualAction(style: .normal, title: starTitle) { [weak self] (action, view, completionHandler) in - self?.toggleArticleStarStatus(article) + self?.coordinator.toggleStar(for: indexPath) completionHandler(true) } @@ -491,34 +491,6 @@ private extension MasterTimelineViewController { } } - func toggleArticleReadStatus(_ article: Article) { - guard let undoManager = undoManager, - let markReadCommand = MarkStatusCommand(initialArticles: [article], markingRead: !article.status.read, undoManager: undoManager) else { - return - } - runCommand(markReadCommand) - } - - func toggleArticleStarStatus(_ article: Article) { - guard let undoManager = undoManager, - let markReadCommand = MarkStatusCommand(initialArticles: [article], markingStarred: !article.status.starred, undoManager: undoManager) else { - return - } - runCommand(markReadCommand) - } - - func markOlderArticlesRead(_ article: Article) { - let articlesToMark = coordinator.articles.filter { $0.logicalDatePublished < article.logicalDatePublished } - if articlesToMark.isEmpty { - return - } - - guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: articlesToMark, markingRead: true, undoManager: undoManager) else { - return - } - runCommand(markReadCommand) - } - func toggleArticleReadStatusAction(indexPath: IndexPath) -> UIAction { let article = coordinator.articles[indexPath.row] @@ -528,7 +500,7 @@ private extension MasterTimelineViewController { let image = article.status.read ? AppAssets.circleClosedImage : AppAssets.circleOpenImage let action = UIAction(title: title, image: image) { [weak self] action in - self?.toggleArticleReadStatus(article) + self?.coordinator.toggleRead(for: indexPath) } return action @@ -543,27 +515,25 @@ private extension MasterTimelineViewController { let image = article.status.starred ? AppAssets.starOpenImage : AppAssets.starClosedImage let action = UIAction(title: title, image: image) { [weak self] action in - self?.toggleArticleStarStatus(article) + self?.coordinator.toggleStar(for: indexPath) } return action } func markOlderAsReadAction(indexPath: IndexPath) -> UIAction { - let article = coordinator.articles[indexPath.row] let title = NSLocalizedString("Mark Older as Read", comment: "Mark Older as Read") let image = coordinator.sortDirection == .orderedDescending ? AppAssets.markOlderAsReadDownImage : AppAssets.markOlderAsReadUpImage let action = UIAction(title: title, image: image) { [weak self] action in - self?.markOlderArticlesRead(article) + self?.coordinator.markAsReadOlderArticlesInTimeline(indexPath) } return action } func markOlderAsReadAlertAction(indexPath: IndexPath, completionHandler: @escaping (Bool) -> Void) -> UIAlertAction { - let article = coordinator.articles[indexPath.row] let title = NSLocalizedString("Mark Older as Read", comment: "Mark Older as Read") let action = UIAlertAction(title: title, style: .default) { [weak self] action in - self?.markOlderArticlesRead(article) + self?.coordinator.markAsReadOlderArticlesInTimeline(indexPath) completionHandler(true) } return action