Move more logic out of MasterFeedViewController and into AppCoordinator

This commit is contained in:
Maurice Parker 2019-07-06 12:25:45 -05:00
parent d543b979ac
commit 6e59c56515
2 changed files with 47 additions and 35 deletions

View File

@ -21,8 +21,13 @@ public extension Notification.Name {
static let ArticleSelectionDidChange = Notification.Name(rawValue: "ArticleSelectionDidChange")
}
class AppCoordinator {
class AppCoordinator: UndoableCommandRunner {
var undoableCommands = [UndoableCommand]()
var undoManager: UndoManager? {
return rootSplitViewController.undoManager
}
private var rootSplitViewController: UISplitViewController!
private var masterNavigationController: UINavigationController!
private var masterFeedViewController: MasterFeedViewController!
@ -485,6 +490,21 @@ class AppCoordinator {
}
func markAllAsRead() {
let accounts = AccountManager.shared.activeAccounts
var articles = Set<Article>()
accounts.forEach { account in
articles.formUnion(account.fetchUnreadArticles())
}
guard let undoManager = undoManager,
let markReadCommand = MarkStatusCommand(initialArticles: Array(articles), markingRead: true, undoManager: undoManager) else {
return
}
runCommand(markReadCommand)
}
func toggleReadForCurrentArticle() {
if let article = currentArticle {
markArticles(Set([article]), statusKey: .read, flag: !article.status.read)
@ -497,6 +517,24 @@ class AppCoordinator {
}
}
func showSettings() {
let settingsNavViewController = UIStoryboard.settings.instantiateInitialViewController() as! UINavigationController
settingsNavViewController.modalPresentationStyle = .formSheet
let settingsViewController = settingsNavViewController.topViewController as! SettingsViewController
settingsViewController.presentingParentController = masterFeedViewController
masterFeedViewController.present(settingsNavViewController, animated: true)
// let settings = UIHostingController(rootView: SettingsView(viewModel: SettingsView.ViewModel()))
// self.present(settings, animated: true)
}
func showAdd() {
let addViewController = UIStoryboard.add.instantiateInitialViewController()!
addViewController.modalPresentationStyle = .formSheet
addViewController.preferredContentSize = AddContainerViewController.preferredContentSizeForFormSheetDisplay
masterFeedViewController.present(addViewController, animated: true)
}
func showBrowserForCurrentArticle() {
guard let preferredLink = currentArticle?.preferredLink, let url = URL(string: preferredLink) else {
return
@ -511,11 +549,14 @@ class AppCoordinator {
guard let preferredLink = currentArticle?.preferredLink, let url = URL(string: preferredLink) else {
return
}
let itemSource = ArticleActivityItemSource(url: url, subject: currentArticle?.title)
let activityViewController = UIActivityViewController(activityItems: [itemSource], applicationActivities: nil)
activityViewController.popoverPresentationController?.barButtonItem = detailViewController.actionBarButtonItem
detailViewController.present(activityViewController, animated: true) }
detailViewController.present(activityViewController, animated: true)
}
}
// MARK: UISplitViewControllerDelegate

View File

@ -393,18 +393,7 @@ class MasterFeedViewController: ProgressTableViewController, UndoableCommandRunn
// MARK: Actions
@IBAction func settings(_ sender: UIBarButtonItem) {
let settingsNavViewController = UIStoryboard.settings.instantiateInitialViewController() as! UINavigationController
settingsNavViewController.modalPresentationStyle = .formSheet
let settingsViewController = settingsNavViewController.topViewController as! SettingsViewController
settingsViewController.presentingParentController = self
self.present(settingsNavViewController, animated: true)
// let settings = UIHostingController(rootView: SettingsView(viewModel: SettingsView.ViewModel()))
// self.present(settings, animated: true)
coordinator.showSettings()
}
@IBAction func markAllAsRead(_ sender: Any) {
@ -419,20 +408,7 @@ class MasterFeedViewController: ProgressTableViewController, UndoableCommandRunn
let markTitle = NSLocalizedString("Mark All Read", comment: "Mark All Read")
let markAction = UIAlertAction(title: markTitle, style: .default) { [weak self] (action) in
let accounts = AccountManager.shared.activeAccounts
var articles = Set<Article>()
accounts.forEach { account in
articles.formUnion(account.fetchUnreadArticles())
}
guard let undoManager = self?.undoManager,
let markReadCommand = MarkStatusCommand(initialArticles: Array(articles), markingRead: true, undoManager: undoManager) else {
return
}
self?.runCommand(markReadCommand)
self?.coordinator.markAllAsRead()
}
alertController.addAction(markAction)
@ -442,12 +418,7 @@ class MasterFeedViewController: ProgressTableViewController, UndoableCommandRunn
}
@IBAction func add(_ sender: UIBarButtonItem) {
let addViewController = UIStoryboard.add.instantiateInitialViewController()!
addViewController.modalPresentationStyle = .formSheet
addViewController.preferredContentSize = AddContainerViewController.preferredContentSizeForFormSheetDisplay
addViewController.popoverPresentationController?.barButtonItem = sender
self.present(addViewController, animated: true)
coordinator.showAdd()
}
@objc func toggleSectionHeader(_ sender: UITapGestureRecognizer) {