Tidies up Delegate and Coordinator code

This commit is contained in:
Stuart Breckenridge 2020-11-19 11:30:52 +08:00
parent cf0e3ba6a2
commit 0b4640db04
2 changed files with 21 additions and 45 deletions

View File

@ -807,24 +807,27 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
}
}
func selectTodayFeed() {
func selectTodayFeed(completion: @escaping () -> () = {}) {
markExpanded(SmartFeedsController.shared)
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.todayFeed) {
self.selectFeed(SmartFeedsController.shared.todayFeed, animations: [.navigation, .scroll])
completion()
}
}
func selectAllUnreadFeed() {
func selectAllUnreadFeed(completion: @escaping () -> () = {}) {
markExpanded(SmartFeedsController.shared)
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.unreadFeed) {
self.selectFeed(SmartFeedsController.shared.unreadFeed, animations: [.navigation, .scroll])
completion()
}
}
func selectStarredFeed() {
func selectStarredFeed(completion: @escaping () -> () = {}) {
markExpanded(SmartFeedsController.shared)
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.starredFeed) {
self.selectFeed(SmartFeedsController.shared.starredFeed, animations: [.navigation, .scroll])
completion()
}
}
@ -1277,6 +1280,12 @@ class SceneCoordinator: NSObject, UndoableCommandRunner, UnreadCountProvider {
func toggleSidebar() {
rootSplitViewController.preferredDisplayMode = rootSplitViewController.displayMode == .allVisible ? .primaryHidden : .allVisible
}
func selectArticleInCurrentFeed(_ articleID: String) {
if let article = self.articles.first(where: { $0.articleID == articleID }) {
self.selectArticle(article)
}
}
}
// MARK: UISplitViewControllerDelegate
@ -2291,10 +2300,4 @@ private extension SceneCoordinator {
return false
}
func selectArticleInCurrentFeed(_ articleID: String) {
if let article = self.articles.first(where: { $0.articleID == articleID }) {
self.selectArticle(article)
}
}
}

View File

@ -105,22 +105,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
let articles = try! AccountManager.shared.fetchArticles(.unread)
let article = articles.filter({ $0.articleID == id }).first
if article != nil {
self.coordinator.selectAllUnreadFeed()
self.coordinator.selectAllUnreadFeed(completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.coordinator.selectArticle(article!, animations: [.navigation, .scroll])
self.coordinator.selectArticleInCurrentFeed(id!)
})
return
} else {
self.coordinator.selectAllUnreadFeed()
return
}
})
} else {
self.coordinator.selectAllUnreadFeed()
return
}
}
@ -132,22 +123,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
let articles = try! AccountManager.shared.fetchArticles(.today)
let article = articles.filter({ $0.articleID == id }).first
if article != nil {
self.coordinator.selectTodayFeed()
self.coordinator.selectTodayFeed(completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.coordinator.selectArticle(article!, animations: [.navigation, .scroll])
self.coordinator.selectArticleInCurrentFeed(id!)
})
return
} else {
self.coordinator.selectTodayFeed()
return
}
})
} else {
self.coordinator.selectTodayFeed()
return
}
}
@ -159,22 +141,13 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
if AccountManager.shared.isSuspended {
AccountManager.shared.resumeAll()
}
let articles = try! AccountManager.shared.fetchArticles(.starred)
let article = articles.filter({ $0.articleID == id }).first
if article != nil {
self.coordinator.selectStarredFeed()
self.coordinator.selectStarredFeed(completion: {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.coordinator.selectArticle(article!, animations: [.navigation, .scroll])
self.coordinator.selectArticleInCurrentFeed(id!)
})
return
} else {
self.coordinator.selectStarredFeed()
return
}
})
} else {
self.coordinator.selectStarredFeed()
return
}
}