2019-06-29 20:35:12 +02:00
|
|
|
|
//
|
|
|
|
|
// NavigationModelController.swift
|
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 4/21/19.
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
2019-09-07 21:00:31 +02:00
|
|
|
|
import UIKit
|
2024-05-07 07:16:05 +02:00
|
|
|
|
import UIKitExtras
|
2019-10-03 16:53:21 +02:00
|
|
|
|
import UserNotifications
|
2019-06-29 20:35:12 +02:00
|
|
|
|
import Account
|
|
|
|
|
import Articles
|
2024-03-22 01:21:50 +01:00
|
|
|
|
import Tree
|
2020-05-14 14:28:38 +02:00
|
|
|
|
import SafariServices
|
2024-03-04 07:51:53 +01:00
|
|
|
|
import SwiftUI
|
2024-03-21 04:54:21 +01:00
|
|
|
|
import Core
|
2024-04-16 07:21:17 +02:00
|
|
|
|
import Images
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
protocol MainControllerIdentifiable {
|
|
|
|
|
var mainControllerIdentifier: MainControllerIdentifier { get }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum MainControllerIdentifier {
|
|
|
|
|
case none
|
|
|
|
|
case mainFeed
|
|
|
|
|
case mainTimeline
|
|
|
|
|
case article
|
2019-11-20 23:41:13 +01:00
|
|
|
|
}
|
2021-10-21 02:03:02 +02:00
|
|
|
|
|
2019-08-31 22:53:47 +02:00
|
|
|
|
enum SearchScope: Int {
|
|
|
|
|
case timeline = 0
|
|
|
|
|
case global = 1
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 14:53:56 +02:00
|
|
|
|
enum ShowFeedName {
|
|
|
|
|
case none
|
|
|
|
|
case byline
|
|
|
|
|
case feed
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
struct SidebarItemNode: Hashable {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
var node: Node
|
2024-02-27 06:47:24 +01:00
|
|
|
|
var sidebarItemID: SidebarItemIdentifier
|
2024-02-26 06:34:22 +01:00
|
|
|
|
|
2024-03-22 01:25:01 +01:00
|
|
|
|
@MainActor init(_ node: Node) {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
self.node = node
|
2024-02-27 06:47:24 +01:00
|
|
|
|
self.sidebarItemID = (node.representedObject as! SidebarItem).sidebarItemID!
|
2021-10-21 02:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
2024-02-27 06:47:24 +01:00
|
|
|
|
hasher.combine(sidebarItemID)
|
2021-10-21 02:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-19 18:15:30 +01:00
|
|
|
|
@MainActor final class SceneCoordinator: NSObject, UndoableCommandRunner {
|
|
|
|
|
|
2019-07-06 19:25:45 +02:00
|
|
|
|
var undoableCommands = [UndoableCommand]()
|
|
|
|
|
var undoManager: UndoManager? {
|
|
|
|
|
return rootSplitViewController.undoManager
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 02:30:21 +02:00
|
|
|
|
private var activityManager = ActivityManager()
|
|
|
|
|
|
2019-09-04 23:24:16 +02:00
|
|
|
|
private var rootSplitViewController: RootSplitViewController!
|
2024-03-04 07:51:53 +01:00
|
|
|
|
private var sidebarViewController: SidebarViewController!
|
2024-02-26 17:37:15 +01:00
|
|
|
|
private var timelineViewController: TimelineViewController?
|
2024-03-04 07:51:53 +01:00
|
|
|
|
private var articleViewController: ArticleViewController?
|
|
|
|
|
|
|
|
|
|
private var lastMainControllerToAppear = MainControllerIdentifier.none
|
|
|
|
|
|
2019-07-06 00:45:39 +02:00
|
|
|
|
private let fetchAndMergeArticlesQueue = CoalescingQueue(name: "Fetch and Merge Articles", interval: 0.5)
|
2020-06-16 01:03:20 +02:00
|
|
|
|
private let rebuildBackingStoresQueue = CoalescingQueue(name: "Rebuild The Backing Stores", interval: 0.5)
|
2019-08-21 22:27:53 +02:00
|
|
|
|
private var fetchSerialNumber = 0
|
|
|
|
|
private let fetchRequestQueue = FetchRequestQueue()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
/// Which Containers are expanded
|
2019-11-25 01:29:00 +01:00
|
|
|
|
private var expandedTable = Set<ContainerIdentifier>()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
/// Which Containers used to be expanded. Reset by rebuilding the Shadow Table.
|
|
|
|
|
private var lastExpandedTable = Set<ContainerIdentifier>()
|
|
|
|
|
|
|
|
|
|
/// Which Feeds have the Read Articles Filter enabled
|
2024-02-26 06:17:00 +01:00
|
|
|
|
private var readFilterEnabledTable = [SidebarItemIdentifier: Bool]()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
/// Flattened tree structure for the Sidebar
|
2024-03-04 08:01:00 +01:00
|
|
|
|
private var shadowTable = [(sectionID: String, sidebarItemNodes: [SidebarItemNode])]()
|
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
private(set) var preSearchTimelineFeed: SidebarItem?
|
2019-08-31 18:50:34 +02:00
|
|
|
|
private var lastSearchString = ""
|
2019-08-31 22:53:47 +02:00
|
|
|
|
private var lastSearchScope: SearchScope? = nil
|
2019-08-31 19:12:50 +02:00
|
|
|
|
private var isSearching: Bool = false
|
2019-11-19 18:16:43 +01:00
|
|
|
|
private var savedSearchArticles: ArticleArray? = nil
|
2024-04-08 01:09:23 +02:00
|
|
|
|
private var savedSearchArticleIDs: Set<String>? = nil
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2020-07-02 04:47:45 +02:00
|
|
|
|
private(set) var sortDirection = AppDefaults.shared.timelineSortDirection {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
didSet {
|
|
|
|
|
if sortDirection != oldValue {
|
2019-09-13 15:29:56 +02:00
|
|
|
|
sortParametersDidChange()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-14 23:29:20 +01:00
|
|
|
|
|
2020-07-02 04:47:45 +02:00
|
|
|
|
private(set) var groupByFeed = AppDefaults.shared.timelineGroupByFeed {
|
2019-09-09 00:41:00 +02:00
|
|
|
|
didSet {
|
|
|
|
|
if groupByFeed != oldValue {
|
2019-09-13 15:29:56 +02:00
|
|
|
|
sortParametersDidChange()
|
2019-09-09 00:41:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-08 02:33:30 +02:00
|
|
|
|
|
2019-11-19 02:12:24 +01:00
|
|
|
|
var prefersStatusBarHidden = false
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
private let treeControllerDelegate = FeedTreeControllerDelegate()
|
2019-11-13 22:22:22 +01:00
|
|
|
|
private let treeController: TreeController
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-11-26 23:33:11 +01:00
|
|
|
|
var stateRestorationActivity: NSUserActivity {
|
|
|
|
|
let activity = activityManager.stateRestorationActivity
|
2021-09-13 08:11:23 +02:00
|
|
|
|
var userInfo = activity.userInfo ?? [AnyHashable: Any]()
|
|
|
|
|
|
|
|
|
|
userInfo[UserInfoKey.windowState] = windowState()
|
|
|
|
|
|
|
|
|
|
let articleState = articleViewController?.currentState
|
|
|
|
|
userInfo[UserInfoKey.isShowingExtractedArticle] = articleState?.isShowingExtractedArticle ?? false
|
|
|
|
|
userInfo[UserInfoKey.articleWindowScrollY] = articleState?.windowScrollY ?? 0
|
|
|
|
|
|
2019-11-26 23:33:11 +01:00
|
|
|
|
activity.userInfo = userInfo
|
|
|
|
|
return activity
|
2019-09-01 02:30:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
var isNavigationDisabled = false
|
|
|
|
|
|
2019-08-02 17:25:47 +02:00
|
|
|
|
var isRootSplitCollapsed: Bool {
|
|
|
|
|
return rootSplitViewController.isCollapsed
|
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
|
var isReadFeedsFiltered: Bool {
|
2019-11-22 17:55:54 +01:00
|
|
|
|
return treeControllerDelegate.isReadFiltered
|
2019-11-21 22:55:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 18:43:36 +01:00
|
|
|
|
var isReadArticlesFiltered: Bool {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
if let sidebarItemID = timelineFeed?.sidebarItemID, let readFilterEnabled = readFilterEnabledTable[sidebarItemID] {
|
2019-11-27 18:43:36 +01:00
|
|
|
|
return readFilterEnabled
|
|
|
|
|
} else {
|
|
|
|
|
return timelineDefaultReadFilterType != .none
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var timelineDefaultReadFilterType: ReadFilterType {
|
|
|
|
|
return timelineFeed?.defaultReadFilterType ?? .none
|
|
|
|
|
}
|
2019-11-22 01:22:43 +01:00
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
var rootNode: Node {
|
|
|
|
|
return treeController.rootNode
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 01:06:35 +01:00
|
|
|
|
// At some point we should refactor the current Feed IndexPath out and only use the timeline feed
|
2019-09-05 04:06:29 +02:00
|
|
|
|
private(set) var currentFeedIndexPath: IndexPath?
|
2021-05-08 21:42:44 +02:00
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
|
var timelineIconImage: IconImage? {
|
2021-05-08 21:42:44 +02:00
|
|
|
|
guard let timelineFeed = timelineFeed else {
|
|
|
|
|
return nil
|
2019-10-29 03:33:13 +01:00
|
|
|
|
}
|
2021-05-08 21:42:44 +02:00
|
|
|
|
return IconImageCache.shared.imageForFeed(timelineFeed)
|
2019-09-22 00:59:58 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 21:31:15 +01:00
|
|
|
|
private var exceptionArticleFetcher: ArticleFetcher?
|
2024-02-26 06:14:10 +01:00
|
|
|
|
private(set) var timelineFeed: SidebarItem?
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-11-11 23:59:42 +01:00
|
|
|
|
var timelineMiddleIndexPath: IndexPath?
|
|
|
|
|
|
2020-04-18 14:53:56 +02:00
|
|
|
|
private(set) var showFeedNames = ShowFeedName.none
|
2019-11-06 01:05:57 +01:00
|
|
|
|
private(set) var showIcons = false
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
|
var prevFeedIndexPath: IndexPath? {
|
2020-01-31 02:37:22 +01:00
|
|
|
|
guard let indexPath = currentFeedIndexPath else {
|
2019-09-05 04:06:29 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 02:37:22 +01:00
|
|
|
|
let prevIndexPath: IndexPath? = {
|
2019-09-05 04:06:29 +02:00
|
|
|
|
if indexPath.row - 1 < 0 {
|
2020-01-31 02:37:22 +01:00
|
|
|
|
for i in (0..<indexPath.section).reversed() {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if shadowTable[i].sidebarItemNodes.count > 0 {
|
|
|
|
|
return IndexPath(row: shadowTable[i].sidebarItemNodes.count - 1, section: i)
|
2020-01-31 02:37:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-09-05 04:06:29 +02:00
|
|
|
|
} else {
|
|
|
|
|
return IndexPath(row: indexPath.row - 1, section: indexPath.section)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return prevIndexPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var nextFeedIndexPath: IndexPath? {
|
2020-01-31 02:37:22 +01:00
|
|
|
|
guard let indexPath = currentFeedIndexPath else {
|
2019-09-05 04:06:29 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 02:37:22 +01:00
|
|
|
|
let nextIndexPath: IndexPath? = {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if indexPath.row + 1 >= shadowTable[indexPath.section].sidebarItemNodes.count {
|
2020-01-31 02:37:22 +01:00
|
|
|
|
for i in indexPath.section + 1..<shadowTable.count {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if shadowTable[i].sidebarItemNodes.count > 0 {
|
2020-01-31 02:37:22 +01:00
|
|
|
|
return IndexPath(row: 0, section: i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2019-09-05 04:06:29 +02:00
|
|
|
|
} else {
|
|
|
|
|
return IndexPath(row: indexPath.row + 1, section: indexPath.section)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return nextIndexPath
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
var isPrevArticleAvailable: Bool {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
guard let articleRow = currentArticleRow else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
2019-09-11 16:11:33 +02:00
|
|
|
|
return articleRow > 0
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isNextArticleAvailable: Bool {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
guard let articleRow = currentArticleRow else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
2019-09-11 16:11:33 +02:00
|
|
|
|
return articleRow + 1 < articles.count
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
var prevArticle: Article? {
|
|
|
|
|
guard isPrevArticleAvailable, let articleRow = currentArticleRow else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-09-11 16:45:48 +02:00
|
|
|
|
return articles[articleRow - 1]
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
var nextArticle: Article? {
|
|
|
|
|
guard isNextArticleAvailable, let articleRow = currentArticleRow else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-09-11 16:45:48 +02:00
|
|
|
|
return articles[articleRow + 1]
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var firstUnreadArticleIndexPath: IndexPath? {
|
|
|
|
|
for (row, article) in articles.enumerated() {
|
|
|
|
|
if !article.status.read {
|
|
|
|
|
return IndexPath(row: row, section: 0)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
var currentArticle: Article?
|
2019-09-11 16:16:04 +02:00
|
|
|
|
|
2020-01-08 19:06:41 +01:00
|
|
|
|
private(set) var articles = ArticleArray() {
|
|
|
|
|
didSet {
|
|
|
|
|
timelineMiddleIndexPath = nil
|
2020-01-27 20:58:32 +01:00
|
|
|
|
articleDictionaryNeedsUpdate = true
|
2020-01-08 19:06:41 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-27 20:58:32 +01:00
|
|
|
|
|
|
|
|
|
private var articleDictionaryNeedsUpdate = true
|
|
|
|
|
private var _idToArticleDictionary = [String: Article]()
|
|
|
|
|
private var idToAticleDictionary: [String: Article] {
|
|
|
|
|
if articleDictionaryNeedsUpdate {
|
|
|
|
|
rebuildArticleDictionaries()
|
|
|
|
|
}
|
|
|
|
|
return _idToArticleDictionary
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:15:22 +02:00
|
|
|
|
private var currentArticleRow: Int? {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
guard let article = currentArticle else { return nil }
|
|
|
|
|
return articles.firstIndex(of: article)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
var isTimelineUnreadAvailable: Bool {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
return timelineUnreadCount > 0
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var isAnyUnreadAvailable: Bool {
|
|
|
|
|
return appDelegate.unreadCount > 0
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
var timelineUnreadCount: Int = 0
|
2019-08-24 21:57:51 +02:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
init(rootSplitViewController: RootSplitViewController) {
|
|
|
|
|
self.rootSplitViewController = rootSplitViewController
|
|
|
|
|
self.treeController = TreeController(delegate: treeControllerDelegate)
|
2019-11-13 22:22:22 +01:00
|
|
|
|
|
2019-07-19 19:29:17 +02:00
|
|
|
|
super.init()
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.sidebarViewController = rootSplitViewController.viewController(for: .primary) as? SidebarViewController
|
|
|
|
|
self.sidebarViewController.coordinator = self
|
|
|
|
|
if let navController = self.sidebarViewController?.navigationController {
|
|
|
|
|
navController.delegate = self
|
|
|
|
|
configureNavigationController(navController)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.timelineViewController = rootSplitViewController.viewController(for: .supplementary) as? TimelineViewController
|
|
|
|
|
self.timelineViewController?.coordinator = self
|
|
|
|
|
if let navController = self.timelineViewController?.navigationController {
|
|
|
|
|
navController.delegate = self
|
|
|
|
|
configureNavigationController(navController)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.articleViewController = rootSplitViewController.viewController(for: .secondary) as? ArticleViewController
|
|
|
|
|
self.articleViewController?.coordinator = self
|
|
|
|
|
if let navController = self.articleViewController?.navigationController {
|
|
|
|
|
configureNavigationController(navController)
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
|
for sectionNode in treeController.rootNode.childNodes {
|
|
|
|
|
markExpanded(sectionNode)
|
2024-03-04 08:01:00 +01:00
|
|
|
|
shadowTable.append((sectionID: "", sidebarItemNodes: [SidebarItemNode]()))
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidInitialize(_:)), name: .UnreadCountDidInitialize, object: nil)
|
2020-01-20 00:44:13 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2019-08-21 20:10:08 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountStateDidChange(_:)), name: .AccountStateDidChange, object: nil)
|
2019-09-08 16:43:51 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddAccount(_:)), name: .UserDidAddAccount, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidDeleteAccount(_:)), name: .UserDidDeleteAccount, object: nil)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(accountDidDownloadArticles(_:)), name: .AccountDidDownloadArticles, object: nil)
|
2020-02-03 23:05:22 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground(_:)), name: UIApplication.willEnterForegroundNotification, object: nil)
|
2021-09-21 03:10:56 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(importDownloadedTheme(_:)), name: .didEndDownloadingTheme, object: nil)
|
2021-09-21 03:22:45 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(themeDownloadDidFail(_:)), name: .didFailToImportThemeWithError, object: nil)
|
2019-07-19 22:59:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
|
func restoreWindowState(_ activity: NSUserActivity?) {
|
|
|
|
|
if let activity = activity, let windowState = activity.userInfo?[UserInfoKey.windowState] as? [AnyHashable: Any] {
|
2019-11-27 21:52:40 +01:00
|
|
|
|
|
|
|
|
|
if let containerExpandedWindowState = windowState[UserInfoKey.containerExpandedWindowState] as? [[AnyHashable: AnyHashable]] {
|
|
|
|
|
let containerIdentifers = containerExpandedWindowState.compactMap( { ContainerIdentifier(userInfo: $0) })
|
|
|
|
|
expandedTable = Set(containerIdentifers)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let readArticlesFilterState = windowState[UserInfoKey.readArticlesFilterState] as? [[AnyHashable: AnyHashable]: Bool] {
|
|
|
|
|
for key in readArticlesFilterState.keys {
|
2024-02-27 06:47:24 +01:00
|
|
|
|
if let sidebarItemID = SidebarItemIdentifier(userInfo: key) {
|
|
|
|
|
readFilterEnabledTable[sidebarItemID] = readArticlesFilterState[key]
|
2019-11-27 21:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-28 02:54:52 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
// if let isSidebarHidden = windowState[UserInfoKey.isSidebarHidden] as? Bool, isSidebarHidden {
|
|
|
|
|
// DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
|
|
|
|
|
// self.rootSplitViewController.preferredDisplayMode = .secondaryOnly
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
2019-11-28 02:54:52 +01:00
|
|
|
|
rebuildBackingStores(initialLoad: true)
|
|
|
|
|
|
2019-11-27 21:52:40 +01:00
|
|
|
|
// You can't assign the Feeds Read Filter until we've built the backing stores at least once or there is nothing
|
|
|
|
|
// for state restoration to work with while we are waiting for the unread counts to initialize.
|
|
|
|
|
if let readFeedsFilterState = windowState[UserInfoKey.readFeedsFilterState] as? Bool {
|
|
|
|
|
treeControllerDelegate.isReadFiltered = readFeedsFilterState
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
rebuildBackingStores(initialLoad: true)
|
|
|
|
|
|
2019-11-26 23:33:11 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func handle(_ activity: NSUserActivity) {
|
|
|
|
|
selectFeed(indexPath: nil) {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
guard let activityType = ActivityType(rawValue: activity.activityType) else { return }
|
|
|
|
|
switch activityType {
|
2019-11-26 23:33:11 +01:00
|
|
|
|
case .restoration:
|
|
|
|
|
break
|
2019-11-19 18:16:43 +01:00
|
|
|
|
case .selectFeed:
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.handleSelectFeed(activity.userInfo)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
case .nextUnread:
|
|
|
|
|
self.selectFirstUnreadInAllUnread()
|
|
|
|
|
case .readArticle:
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.handleReadArticle(activity.userInfo)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
case .addFeedIntent:
|
2024-02-26 08:12:21 +01:00
|
|
|
|
self.showAddFeed()
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
2019-08-25 02:31:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func handle(_ response: UNNotificationResponse) {
|
2019-10-03 16:53:21 +02:00
|
|
|
|
let userInfo = response.notification.request.content.userInfo
|
2020-02-18 02:40:40 +01:00
|
|
|
|
handleReadArticle(userInfo)
|
2019-10-03 16:53:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 00:16:34 +02:00
|
|
|
|
func resetFocus() {
|
|
|
|
|
if currentArticle != nil {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.focus()
|
2020-04-29 00:16:34 +02:00
|
|
|
|
} else {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController?.focus()
|
2020-04-29 00:16:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 00:41:46 +02:00
|
|
|
|
func selectFirstUnreadInAllUnread() {
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(SmartFeedsController.shared)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.unreadFeed) {
|
|
|
|
|
self.selectFeed(SmartFeedsController.shared.unreadFeed) {
|
2020-01-26 00:13:33 +01:00
|
|
|
|
self.selectFirstUnreadArticleInTimeline()
|
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
2019-09-02 00:41:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showSearch() {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
selectFeed(indexPath: nil) {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.rootSplitViewController.show(.supplementary)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now()) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController!.showSearchAll()
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
2019-09-06 17:29:00 +02:00
|
|
|
|
}
|
2019-09-02 00:41:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
// MARK: Notifications
|
|
|
|
|
|
2019-11-27 03:23:12 +01:00
|
|
|
|
@objc func unreadCountDidInitialize(_ notification: Notification) {
|
|
|
|
|
guard notification.object is AccountManager else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-09-11 21:28:00 +02:00
|
|
|
|
|
2021-10-21 20:08:18 +02:00
|
|
|
|
if isReadFeedsFiltered {
|
|
|
|
|
rebuildBackingStores()
|
|
|
|
|
}
|
2019-11-27 03:23:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 00:44:13 +01:00
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2020-01-29 20:19:28 +01:00
|
|
|
|
// We will handle the filtering of unread feeds in unreadCountDidInitialize after they have all be calculated
|
|
|
|
|
guard AccountManager.shared.isUnreadCountsInitialized else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 07:43:54 +01:00
|
|
|
|
queueRebuildBackingStores()
|
2020-01-20 00:44:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 20:10:08 +02:00
|
|
|
|
@objc func statusesDidChange(_ note: Notification) {
|
|
|
|
|
updateUnreadCount()
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
@objc func containerChildrenDidChange(_ note: Notification) {
|
2019-08-29 22:02:45 +02:00
|
|
|
|
if timelineFetcherContainsAnyPseudoFeed() || timelineFetcherContainsAnyFolder() {
|
2020-01-24 02:07:20 +01:00
|
|
|
|
fetchAndMergeArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: false)
|
2020-01-24 02:07:20 +01:00
|
|
|
|
self.rebuildBackingStores()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
rebuildBackingStores()
|
2019-08-29 22:02:45 +02:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func batchUpdateDidPerform(_ notification: Notification) {
|
2020-03-28 00:24:57 +01:00
|
|
|
|
rebuildBackingStores()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
|
|
|
|
rebuildBackingStores()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func accountStateDidChange(_ note: Notification) {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
if timelineFetcherContainsAnyPseudoFeed() {
|
2020-01-24 02:07:20 +01:00
|
|
|
|
fetchAndMergeArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: false)
|
2021-10-21 16:44:07 +02:00
|
|
|
|
self.rebuildBackingStores()
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-21 16:44:07 +02:00
|
|
|
|
self.rebuildBackingStores()
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 16:43:51 +02:00
|
|
|
|
@objc func userDidAddAccount(_ note: Notification) {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
let expandNewAccount = {
|
|
|
|
|
if let account = note.userInfo?[Account.UserInfoKey.account] as? Account,
|
|
|
|
|
let node = self.treeController.rootNode.childNodeRepresentingObject(account) {
|
|
|
|
|
self.markExpanded(node)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if timelineFetcherContainsAnyPseudoFeed() {
|
2020-01-24 02:07:20 +01:00
|
|
|
|
fetchAndMergeArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: false)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.rebuildBackingStores(updateExpandedNodes: expandNewAccount)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.rebuildBackingStores(updateExpandedNodes: expandNewAccount)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
2019-09-08 16:43:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func userDidDeleteAccount(_ note: Notification) {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
let cleanupAccount = {
|
|
|
|
|
if let account = note.userInfo?[Account.UserInfoKey.account] as? Account,
|
|
|
|
|
let node = self.treeController.rootNode.childNodeRepresentingObject(account) {
|
|
|
|
|
self.unmarkExpanded(node)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if timelineFetcherContainsAnyPseudoFeed() {
|
2020-01-24 02:07:20 +01:00
|
|
|
|
fetchAndMergeArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: false)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.rebuildBackingStores(updateExpandedNodes: cleanupAccount)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.rebuildBackingStores(updateExpandedNodes: cleanupAccount)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
@objc func userDidAddFeed(_ notification: Notification) {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
guard let feed = notification.userInfo?[UserInfoKey.feed] as? Feed else {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
|
discloseFeed(feed, animations: [.scroll, .navigation])
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
@objc func userDefaultsDidChange(_ note: Notification) {
|
2020-07-02 04:47:45 +02:00
|
|
|
|
self.sortDirection = AppDefaults.shared.timelineSortDirection
|
|
|
|
|
self.groupByFeed = AppDefaults.shared.timelineGroupByFeed
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func accountDidDownloadArticles(_ note: Notification) {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
guard let feeds = note.userInfo?[Account.UserInfoKey.feeds] as? Set<Feed> else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let shouldFetchAndMergeArticles = timelineFetcherContainsAnyFeed(feeds) || timelineFetcherContainsAnyPseudoFeed()
|
|
|
|
|
if shouldFetchAndMergeArticles {
|
|
|
|
|
queueFetchAndMergeArticles()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-03 23:05:22 +01:00
|
|
|
|
|
|
|
|
|
@objc func willEnterForeground(_ note: Notification) {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
// Don't interfere with any fetch requests that we may have initiated before the app was returned to the foreground.
|
|
|
|
|
// For example if you select Next Unread from the Home Screen Quick actions, you can start a request before we are
|
|
|
|
|
// in the foreground.
|
|
|
|
|
if !fetchRequestQueue.isAnyCurrentRequest {
|
|
|
|
|
queueFetchAndMergeArticles()
|
|
|
|
|
}
|
2020-02-03 23:05:22 +01:00
|
|
|
|
}
|
2021-09-21 03:10:56 +02:00
|
|
|
|
|
|
|
|
|
@objc func importDownloadedTheme(_ note: Notification) {
|
|
|
|
|
guard let userInfo = note.userInfo,
|
|
|
|
|
let url = userInfo["url"] as? URL else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.importTheme(filename: url.path)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func themeDownloadDidFail(_ note: Notification) {
|
|
|
|
|
guard let userInfo = note.userInfo,
|
|
|
|
|
let error = userInfo["error"] as? Error else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.rootSplitViewController.presentError(error, dismiss: nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
2019-12-02 21:14:35 +01:00
|
|
|
|
func suspend() {
|
|
|
|
|
fetchAndMergeArticlesQueue.performCallsImmediately()
|
2020-03-28 00:24:57 +01:00
|
|
|
|
rebuildBackingStoresQueue.performCallsImmediately()
|
2019-12-02 21:14:35 +01:00
|
|
|
|
fetchRequestQueue.cancelAllRequests()
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 22:00:01 +01:00
|
|
|
|
func cleanUp(conditional: Bool) {
|
2020-03-11 21:47:00 +01:00
|
|
|
|
if isReadFeedsFiltered {
|
|
|
|
|
rebuildBackingStores()
|
|
|
|
|
}
|
2020-07-02 04:47:45 +02:00
|
|
|
|
if isReadArticlesFiltered && (AppDefaults.shared.refreshClearsReadArticles || !conditional) {
|
2020-03-11 21:47:00 +01:00
|
|
|
|
refreshTimeline(resetScroll: false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 16:18:07 +01:00
|
|
|
|
func toggleReadFeedsFilter() {
|
|
|
|
|
if isReadFeedsFiltered {
|
|
|
|
|
treeControllerDelegate.isReadFiltered = false
|
|
|
|
|
} else {
|
|
|
|
|
treeControllerDelegate.isReadFiltered = true
|
|
|
|
|
}
|
|
|
|
|
rebuildBackingStores()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController?.updateUI()
|
2020-03-22 16:18:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toggleReadArticlesFilter() {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
guard let sidebarItemID = timelineFeed?.sidebarItemID else {
|
2020-03-22 16:18:07 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if isReadArticlesFiltered {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
readFilterEnabledTable[sidebarItemID] = false
|
2020-03-22 16:18:07 +01:00
|
|
|
|
} else {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
readFilterEnabledTable[sidebarItemID] = true
|
2020-03-22 16:18:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
refreshTimeline(resetScroll: false)
|
|
|
|
|
}
|
2020-06-16 01:03:20 +02:00
|
|
|
|
|
2024-02-26 06:34:22 +01:00
|
|
|
|
func nodeFor(sidebarItemID: SidebarItemIdentifier) -> Node? {
|
2020-06-16 01:03:20 +02:00
|
|
|
|
return treeController.rootNode.descendantNode(where: { node in
|
2024-02-26 06:14:10 +01:00
|
|
|
|
if let feed = node.representedObject as? SidebarItem {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
return feed.sidebarItemID == sidebarItemID
|
2020-06-16 01:03:20 +02:00
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
func numberOfSections() -> Int {
|
|
|
|
|
return shadowTable.count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func numberOfRows(in section: Int) -> Int {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
return shadowTable[section].sidebarItemNodes.count
|
2021-10-21 02:03:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
func nodeFor(_ section: Int) -> Node? {
|
|
|
|
|
return treeController.rootNode.childAtIndex(section)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
func nodeFor(_ indexPath: IndexPath) -> Node? {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
guard indexPath.section > -1 &&
|
|
|
|
|
indexPath.row > -1 &&
|
|
|
|
|
indexPath.section < shadowTable.count &&
|
2024-03-04 08:01:00 +01:00
|
|
|
|
indexPath.row < shadowTable[indexPath.section].sidebarItemNodes.count else {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-03-04 08:01:00 +01:00
|
|
|
|
return shadowTable[indexPath.section].sidebarItemNodes[indexPath.row].node
|
2021-10-21 02:03:02 +02:00
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
func indexPathFor(_ object: AnyObject) -> IndexPath? {
|
|
|
|
|
guard let node = treeController.rootNode.descendantNodeRepresentingObject(object) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return indexPathFor(node)
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
func indexPathFor(_ node: Node) -> IndexPath? {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
let sidebarItemNode = SidebarItemNode(node)
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
for i in 0..<shadowTable.count {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if let row = shadowTable[i].sidebarItemNodes.firstIndex(of: sidebarItemNode) {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
return IndexPath(row: row, section: i)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 20:58:32 +01:00
|
|
|
|
func articleFor(_ articleID: String) -> Article? {
|
|
|
|
|
return idToAticleDictionary[articleID]
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-03 19:07:18 +02:00
|
|
|
|
func cappedIndexPath(_ indexPath: IndexPath) -> IndexPath {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
guard indexPath.section < shadowTable.count && indexPath.row < shadowTable[indexPath.section].sidebarItemNodes.count else {
|
|
|
|
|
return IndexPath(row: shadowTable[shadowTable.count - 1].sidebarItemNodes.count - 1, section: shadowTable.count - 1)
|
2019-09-03 19:07:18 +02:00
|
|
|
|
}
|
|
|
|
|
return indexPath
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 20:10:08 +02:00
|
|
|
|
func unreadCountFor(_ node: Node) -> Int {
|
2020-01-16 23:24:48 +01:00
|
|
|
|
// The coordinator supplies the unread count for the currently selected feed
|
2020-06-16 15:27:59 +02:00
|
|
|
|
if node.representedObject === timelineFeed as AnyObject {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
return timelineUnreadCount
|
2019-08-21 20:10:08 +02:00
|
|
|
|
}
|
|
|
|
|
if let unreadCountProvider = node.representedObject as? UnreadCountProvider {
|
|
|
|
|
return unreadCountProvider.unreadCount
|
|
|
|
|
}
|
2020-01-16 23:24:48 +01:00
|
|
|
|
assertionFailure("This method should only be called for nodes that have an UnreadCountProvider as the represented object.")
|
2019-08-21 20:10:08 +02:00
|
|
|
|
return 0
|
|
|
|
|
}
|
2019-11-21 22:55:50 +01:00
|
|
|
|
|
2019-12-09 02:14:33 +01:00
|
|
|
|
func refreshTimeline(resetScroll: Bool) {
|
2020-02-18 23:14:18 +01:00
|
|
|
|
if let article = self.currentArticle, let account = article.account {
|
|
|
|
|
exceptionArticleFetcher = SingleArticleFetcher(account: account, articleID: article.articleID)
|
|
|
|
|
}
|
2019-11-24 17:27:02 +01:00
|
|
|
|
fetchAndReplaceArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: resetScroll)
|
2019-11-22 03:31:58 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func isExpanded(_ containerID: ContainerIdentifier) -> Bool {
|
|
|
|
|
return expandedTable.contains(containerID)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func isExpanded(_ containerIdentifiable: ContainerIdentifiable) -> Bool {
|
|
|
|
|
if let containerID = containerIdentifiable.containerID {
|
2020-06-16 01:03:20 +02:00
|
|
|
|
return isExpanded(containerID)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-08-21 20:10:08 +02:00
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func isExpanded(_ node: Node) -> Bool {
|
|
|
|
|
if let containerIdentifiable = node.representedObject as? ContainerIdentifiable {
|
|
|
|
|
return isExpanded(containerIdentifiable)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func expand(_ containerID: ContainerIdentifier) {
|
|
|
|
|
markExpanded(containerID)
|
2021-08-24 23:55:44 +02:00
|
|
|
|
rebuildBackingStores()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
/// This is a special function that expects the caller to change the disclosure arrow state outside this function.
|
|
|
|
|
/// Failure to do so will get the Sidebar into an invalid state.
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func expand(_ node: Node) {
|
|
|
|
|
guard let containerID = (node.representedObject as? ContainerIdentifiable)?.containerID else { return }
|
2024-03-04 07:51:53 +01:00
|
|
|
|
lastExpandedTable.insert(containerID)
|
2020-06-16 01:03:20 +02:00
|
|
|
|
expand(containerID)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 23:38:33 +02:00
|
|
|
|
func expandAllSectionsAndFolders() {
|
2019-09-11 23:53:27 +02:00
|
|
|
|
for sectionNode in treeController.rootNode.childNodes {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
markExpanded(sectionNode)
|
2019-09-05 23:38:33 +02:00
|
|
|
|
for topLevelNode in sectionNode.childNodes {
|
2019-09-11 23:53:27 +02:00
|
|
|
|
if topLevelNode.representedObject is Folder {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
markExpanded(topLevelNode)
|
2019-09-05 23:38:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-24 23:55:44 +02:00
|
|
|
|
rebuildBackingStores()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func collapse(_ containerID: ContainerIdentifier) {
|
|
|
|
|
unmarkExpanded(containerID)
|
2021-08-24 23:55:44 +02:00
|
|
|
|
rebuildBackingStores()
|
2020-01-27 05:18:43 +01:00
|
|
|
|
clearTimelineIfNoLongerAvailable()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
/// This is a special function that expects the caller to change the disclosure arrow state outside this function.
|
|
|
|
|
/// Failure to do so will get the Sidebar into an invalid state.
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func collapse(_ node: Node) {
|
|
|
|
|
guard let containerID = (node.representedObject as? ContainerIdentifiable)?.containerID else { return }
|
2024-03-04 07:51:53 +01:00
|
|
|
|
lastExpandedTable.remove(containerID)
|
2020-06-16 01:03:20 +02:00
|
|
|
|
collapse(containerID)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 23:38:33 +02:00
|
|
|
|
func collapseAllFolders() {
|
|
|
|
|
for sectionNode in treeController.rootNode.childNodes {
|
|
|
|
|
for topLevelNode in sectionNode.childNodes {
|
2019-09-11 23:53:27 +02:00
|
|
|
|
if topLevelNode.representedObject is Folder {
|
2019-11-25 01:29:00 +01:00
|
|
|
|
unmarkExpanded(topLevelNode)
|
2019-09-05 23:38:33 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-24 23:55:44 +02:00
|
|
|
|
rebuildBackingStores()
|
2020-01-27 05:18:43 +01:00
|
|
|
|
clearTimelineIfNoLongerAvailable()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 17:37:15 +01:00
|
|
|
|
func feedIndexPathForCurrentTimeline() -> IndexPath? {
|
2019-11-15 13:19:14 +01:00
|
|
|
|
guard let node = treeController.rootNode.descendantNodeRepresentingObject(timelineFeed as AnyObject) else {
|
2019-09-03 20:59:22 +02:00
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return indexPathFor(node)
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
func selectFeed(_ feed: SidebarItem?, animations: Animations = [], deselectArticle: Bool = true, completion: (() -> Void)? = nil) {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
let indexPath: IndexPath? = {
|
|
|
|
|
if let feed = feed, let indexPath = indexPathFor(feed as AnyObject) {
|
|
|
|
|
return indexPath
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
selectFeed(indexPath: indexPath, animations: animations, deselectArticle: deselectArticle, completion: completion)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectFeed(indexPath: IndexPath?, animations: Animations = [], deselectArticle: Bool = true, completion: (() -> Void)? = nil) {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
guard indexPath != currentFeedIndexPath else {
|
|
|
|
|
completion?()
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-09-13 18:19:19 +02:00
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
|
currentFeedIndexPath = indexPath
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController.updateFeedSelection(animations: animations)
|
2019-09-06 20:45:45 +02:00
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
if deselectArticle {
|
|
|
|
|
selectArticle(nil)
|
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
if let ip = indexPath, let node = nodeFor(ip), let feed = node.representedObject as? SidebarItem {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
|
|
|
|
self.activityManager.selecting(feed: feed)
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.rootSplitViewController.show(.supplementary)
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(feed, animated: false) {
|
2021-10-21 18:55:59 +02:00
|
|
|
|
if self.isReadFeedsFiltered {
|
|
|
|
|
self.rebuildBackingStores()
|
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
completion?()
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-02 00:41:46 +02:00
|
|
|
|
} else {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(nil, animated: false) {
|
2020-03-28 07:43:54 +01:00
|
|
|
|
if self.isReadFeedsFiltered {
|
2020-05-16 00:06:49 +02:00
|
|
|
|
self.rebuildBackingStores()
|
2020-03-28 07:43:54 +01:00
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
self.activityManager.invalidateSelecting()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.rootSplitViewController.show(.primary)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
completion?()
|
2019-09-06 14:29:36 +02:00
|
|
|
|
}
|
2019-09-02 00:41:46 +02:00
|
|
|
|
}
|
2019-07-06 18:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
|
func selectPrevFeed() {
|
|
|
|
|
if let indexPath = prevFeedIndexPath {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
selectFeed(indexPath: indexPath, animations: [.navigation, .scroll])
|
2019-09-05 04:06:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectNextFeed() {
|
|
|
|
|
if let indexPath = nextFeedIndexPath {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
selectFeed(indexPath: indexPath, animations: [.navigation, .scroll])
|
2019-09-05 04:06:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-05 20:14:14 +02:00
|
|
|
|
|
2020-11-19 12:23:07 +01:00
|
|
|
|
func selectTodayFeed(completion: (() -> Void)? = nil) {
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(SmartFeedsController.shared)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.todayFeed) {
|
2020-11-19 12:23:07 +01:00
|
|
|
|
self.selectFeed(SmartFeedsController.shared.todayFeed, animations: [.navigation, .scroll], completion: completion)
|
2019-09-05 20:14:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 12:23:07 +01:00
|
|
|
|
func selectAllUnreadFeed(completion: (() -> Void)? = nil) {
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(SmartFeedsController.shared)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.unreadFeed) {
|
2020-11-19 12:23:07 +01:00
|
|
|
|
self.selectFeed(SmartFeedsController.shared.unreadFeed, animations: [.navigation, .scroll], completion: completion)
|
2019-09-05 20:14:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 12:23:07 +01:00
|
|
|
|
func selectStarredFeed(completion: (() -> Void)? = nil) {
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(SmartFeedsController.shared)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.ensureFeedIsAvailableToSelect(SmartFeedsController.shared.starredFeed) {
|
2020-11-19 12:23:07 +01:00
|
|
|
|
self.selectFeed(SmartFeedsController.shared.starredFeed, animations: [.navigation, .scroll], completion: completion)
|
2019-09-05 20:14:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-05 04:06:29 +02:00
|
|
|
|
|
2021-09-13 09:22:15 +02:00
|
|
|
|
func selectArticle(_ article: Article?, animations: Animations = [], isShowingExtractedArticle: Bool? = nil, articleWindowScrollY: Int? = nil) {
|
2019-09-13 18:19:19 +02:00
|
|
|
|
guard article != currentArticle else { return }
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
currentArticle = article
|
2019-11-15 13:19:14 +01:00
|
|
|
|
activityManager.reading(feed: timelineFeed, article: article)
|
2019-08-24 21:57:51 +02:00
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if article == nil {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
rootSplitViewController.show(.supplementary)
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.updateArticleSelection(animations: animations)
|
2024-03-04 07:51:53 +01:00
|
|
|
|
articleViewController?.article = nil
|
2019-08-01 13:53:34 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
rootSplitViewController.show(.secondary)
|
|
|
|
|
|
2021-04-29 12:46:52 +02:00
|
|
|
|
// Mark article as read before navigating to it, so the read status does not flash unread/read on display
|
|
|
|
|
markArticles(Set([article!]), statusKey: .read, flag: true)
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.updateArticleSelection(animations: animations)
|
2024-03-04 07:51:53 +01:00
|
|
|
|
articleViewController?.article = article
|
2021-09-13 09:22:15 +02:00
|
|
|
|
if let isShowingExtractedArticle = isShowingExtractedArticle, let articleWindowScrollY = articleWindowScrollY {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
articleViewController?.restoreScrollPosition = (isShowingExtractedArticle, articleWindowScrollY)
|
2021-09-13 09:22:15 +02:00
|
|
|
|
}
|
2019-07-06 18:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 21:49:56 +02:00
|
|
|
|
func beginSearching() {
|
|
|
|
|
isSearching = true
|
2020-06-18 23:16:30 +02:00
|
|
|
|
preSearchTimelineFeed = timelineFeed
|
2019-11-19 18:16:43 +01:00
|
|
|
|
savedSearchArticles = articles
|
2024-04-08 01:09:23 +02:00
|
|
|
|
savedSearchArticleIDs = Set(articles.map { $0.articleID })
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(nil, animated: true)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
selectArticle(nil)
|
2019-09-01 21:49:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func endSearching() {
|
2020-06-18 23:16:30 +02:00
|
|
|
|
if let oldTimelineFeed = preSearchTimelineFeed {
|
2020-01-29 19:41:05 +01:00
|
|
|
|
emptyTheTimeline()
|
2020-06-18 23:16:30 +02:00
|
|
|
|
timelineFeed = oldTimelineFeed
|
2019-11-24 17:27:02 +01:00
|
|
|
|
replaceArticles(with: savedSearchArticles!, animated: true)
|
2024-03-04 07:51:53 +01:00
|
|
|
|
timelineViewController?.reinitializeArticles(resetScroll: true)
|
2019-09-03 19:25:27 +02:00
|
|
|
|
} else {
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(nil, animated: true)
|
2019-08-31 22:53:47 +02:00
|
|
|
|
}
|
2019-09-12 17:32:58 +02:00
|
|
|
|
|
2019-11-19 18:16:43 +01:00
|
|
|
|
lastSearchString = ""
|
|
|
|
|
lastSearchScope = nil
|
2020-06-18 23:16:30 +02:00
|
|
|
|
preSearchTimelineFeed = nil
|
2024-04-08 01:09:23 +02:00
|
|
|
|
savedSearchArticleIDs = nil
|
2019-11-19 18:16:43 +01:00
|
|
|
|
savedSearchArticles = nil
|
|
|
|
|
isSearching = false
|
2019-09-12 17:32:58 +02:00
|
|
|
|
selectArticle(nil)
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.focus()
|
2019-09-01 21:49:56 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func searchArticles(_ searchString: String, _ searchScope: SearchScope) {
|
|
|
|
|
|
|
|
|
|
guard isSearching else { return }
|
|
|
|
|
|
2019-08-31 19:12:50 +02:00
|
|
|
|
if searchString.count < 3 {
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(nil, animated: true)
|
2019-08-31 19:12:50 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 22:53:47 +02:00
|
|
|
|
if searchString != lastSearchString || searchScope != lastSearchScope {
|
|
|
|
|
|
|
|
|
|
switch searchScope {
|
|
|
|
|
case .global:
|
2019-11-24 17:27:02 +01:00
|
|
|
|
setTimelineFeed(SmartFeed(delegate: SearchFeedDelegate(searchString: searchString)), animated: true)
|
2019-08-31 22:53:47 +02:00
|
|
|
|
case .timeline:
|
2024-04-08 01:09:23 +02:00
|
|
|
|
setTimelineFeed(SmartFeed(delegate: SearchTimelineFeedDelegate(searchString: searchString, articleIDs: savedSearchArticleIDs!)), animated: true)
|
2019-08-31 22:53:47 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-31 18:50:34 +02:00
|
|
|
|
lastSearchString = searchString
|
2019-08-31 22:53:47 +02:00
|
|
|
|
lastSearchScope = searchScope
|
2019-08-31 18:50:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-26 22:21:04 +01:00
|
|
|
|
func findPrevArticle(_ article: Article) -> Article? {
|
|
|
|
|
guard let index = articles.firstIndex(of: article), index > 0 else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return articles[index - 1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func findNextArticle(_ article: Article) -> Article? {
|
|
|
|
|
guard let index = articles.firstIndex(of: article), index + 1 != articles.count else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return articles[index + 1]
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 18:32:19 +02:00
|
|
|
|
func selectPrevArticle() {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if let article = prevArticle {
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectArticle(article, animations: [.navigation, .scroll])
|
2019-07-06 18:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectNextArticle() {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if let article = nextArticle {
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectArticle(article, animations: [.navigation, .scroll])
|
2019-07-06 18:32:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-04 00:07:43 +02:00
|
|
|
|
func selectFirstUnread() {
|
2019-09-03 22:52:59 +02:00
|
|
|
|
if selectFirstUnreadArticleInTimeline() {
|
|
|
|
|
activityManager.selectingNextUnread()
|
|
|
|
|
}
|
2019-08-04 00:07:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
func selectPrevUnread() {
|
|
|
|
|
|
|
|
|
|
// This should never happen, but I don't want to risk throwing us
|
2024-03-04 07:51:53 +01:00
|
|
|
|
// into an infinite loop searching for an unread that isn't there.
|
2019-09-06 01:02:40 +02:00
|
|
|
|
if appDelegate.unreadCount < 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
isNavigationDisabled = true
|
|
|
|
|
defer {
|
|
|
|
|
isNavigationDisabled = false
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
if selectPrevUnreadArticleInTimeline() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectPrevUnreadFeedFetcher()
|
|
|
|
|
selectPrevUnreadArticleInTimeline()
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
func selectNextUnread() {
|
|
|
|
|
|
|
|
|
|
// This should never happen, but I don't want to risk throwing us
|
2024-03-04 07:51:53 +01:00
|
|
|
|
// into an infinite loop searching for an unread that isn't there.
|
2019-06-29 20:35:12 +02:00
|
|
|
|
if appDelegate.unreadCount < 1 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
isNavigationDisabled = true
|
|
|
|
|
defer {
|
|
|
|
|
isNavigationDisabled = false
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
if selectNextUnreadArticleInTimeline() {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-01-29 01:16:49 +01:00
|
|
|
|
|
|
|
|
|
if self.isSearching {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.hideSearch()
|
2020-01-29 01:16:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
selectNextUnreadFeed() {
|
2020-03-30 22:42:42 +02:00
|
|
|
|
self.selectNextUnreadArticleInTimeline()
|
2019-09-03 22:52:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 04:14:19 +02:00
|
|
|
|
func scrollOrGoToNextUnread() {
|
2019-09-24 11:29:15 +02:00
|
|
|
|
if articleViewController?.canScrollDown() ?? false {
|
|
|
|
|
articleViewController?.scrollPageDown()
|
2019-09-06 04:14:19 +02:00
|
|
|
|
} else {
|
|
|
|
|
selectNextUnread()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-10 20:51:41 +02:00
|
|
|
|
|
|
|
|
|
func scrollUp() {
|
|
|
|
|
if articleViewController?.canScrollUp() ?? false {
|
|
|
|
|
articleViewController?.scrollPageUp()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-06 04:14:19 +02:00
|
|
|
|
|
2021-04-13 02:41:01 +02:00
|
|
|
|
func markAllAsRead(_ articles: [Article], completion: (() -> Void)? = nil) {
|
|
|
|
|
markArticlesWithUndo(articles, statusKey: .read, flag: true, completion: completion)
|
2019-08-20 00:26:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 02:41:01 +02:00
|
|
|
|
func markAllAsReadInTimeline(completion: (() -> Void)? = nil) {
|
|
|
|
|
markAllAsRead(articles) {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.rootSplitViewController.show(.primary)
|
2021-04-13 02:41:01 +02:00
|
|
|
|
completion?()
|
|
|
|
|
}
|
2019-07-06 19:31:07 +02:00
|
|
|
|
}
|
2020-01-03 08:16:55 +01:00
|
|
|
|
|
|
|
|
|
func canMarkAboveAsRead(for article: Article) -> Bool {
|
2020-01-07 21:54:21 +01:00
|
|
|
|
let articlesAboveArray = articles.articlesAbove(article: article)
|
|
|
|
|
return articlesAboveArray.canMarkAllAsRead()
|
2020-01-03 08:16:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 09:42:29 +01:00
|
|
|
|
func markAboveAsRead() {
|
|
|
|
|
guard let currentArticle = currentArticle else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
markAboveAsRead(currentArticle)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 08:16:55 +01:00
|
|
|
|
func markAboveAsRead(_ article: Article) {
|
2020-01-07 21:54:21 +01:00
|
|
|
|
let articlesAboveArray = articles.articlesAbove(article: article)
|
|
|
|
|
markAllAsRead(articlesAboveArray)
|
2019-09-05 21:50:05 +02:00
|
|
|
|
}
|
2020-01-03 08:16:55 +01:00
|
|
|
|
|
|
|
|
|
func canMarkBelowAsRead(for article: Article) -> Bool {
|
2020-01-07 21:54:21 +01:00
|
|
|
|
let articleBelowArray = articles.articlesBelow(article: article)
|
|
|
|
|
return articleBelowArray.canMarkAllAsRead()
|
2020-01-03 08:16:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markBelowAsRead() {
|
|
|
|
|
guard let currentArticle = currentArticle else {
|
2019-08-19 23:03:07 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-01-03 08:16:55 +01:00
|
|
|
|
|
|
|
|
|
markBelowAsRead(currentArticle)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markBelowAsRead(_ article: Article) {
|
2020-01-07 21:54:21 +01:00
|
|
|
|
let articleBelowArray = articles.articlesBelow(article: article)
|
|
|
|
|
markAllAsRead(articleBelowArray)
|
2019-08-19 23:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 21:50:05 +02:00
|
|
|
|
func markAsReadForCurrentArticle() {
|
|
|
|
|
if let article = currentArticle {
|
2019-10-04 18:41:30 +02:00
|
|
|
|
markArticlesWithUndo([article], statusKey: .read, flag: true)
|
2019-09-05 21:50:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markAsUnreadForCurrentArticle() {
|
|
|
|
|
if let article = currentArticle {
|
2019-10-04 18:41:30 +02:00
|
|
|
|
markArticlesWithUndo([article], statusKey: .read, flag: false)
|
2019-09-05 21:50:05 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 18:49:53 +02:00
|
|
|
|
func toggleReadForCurrentArticle() {
|
|
|
|
|
if let article = currentArticle {
|
2019-10-04 18:41:30 +02:00
|
|
|
|
toggleRead(article)
|
2019-07-06 18:49:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
func toggleRead(_ article: Article) {
|
2020-02-18 22:49:29 +01:00
|
|
|
|
guard !article.status.read || article.isAvailableToMarkUnread else { return }
|
2019-10-04 18:41:30 +02:00
|
|
|
|
markArticlesWithUndo([article], statusKey: .read, flag: !article.status.read)
|
2019-08-19 23:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 22:43:01 +02:00
|
|
|
|
func toggleStarredForCurrentArticle() {
|
2019-07-06 18:49:53 +02:00
|
|
|
|
if let article = currentArticle {
|
2019-10-04 18:41:30 +02:00
|
|
|
|
toggleStar(article)
|
2019-07-06 18:49:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
func toggleStar(_ article: Article) {
|
2019-10-04 18:41:30 +02:00
|
|
|
|
markArticlesWithUndo([article], statusKey: .starred, flag: !article.status.starred)
|
2019-08-19 23:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 06:41:18 +01:00
|
|
|
|
func timelineFeedIsEqualTo(_ feed: Feed) -> Bool {
|
|
|
|
|
guard let timelineFeed = timelineFeed as? Feed else {
|
2020-02-02 21:44:54 +01:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return timelineFeed == feed
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
func discloseFeed(_ feed: Feed, initialLoad: Bool = false, animations: Animations = [], completion: (() -> Void)? = nil) {
|
2020-01-16 01:53:12 +01:00
|
|
|
|
if isSearching {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.hideSearch()
|
2020-01-16 01:53:12 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
guard let account = feed.account else {
|
2019-09-01 03:23:14 +02:00
|
|
|
|
completion?()
|
2020-02-18 02:40:40 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let parentFolder = account.sortedFolders?.first(where: { $0.objectIsChild(feed) })
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(account)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
if let parentFolder = parentFolder {
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(parentFolder)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if let sidebarItemID = feed.sidebarItemID {
|
|
|
|
|
self.treeControllerDelegate.addFilterException(sidebarItemID)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
2024-02-26 06:34:22 +01:00
|
|
|
|
if let parentFolderFeedID = parentFolder?.sidebarItemID {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.treeControllerDelegate.addFilterException(parentFolderFeedID)
|
2019-09-01 03:23:14 +02:00
|
|
|
|
}
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
rebuildBackingStores(initialLoad: initialLoad, completion: {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.treeControllerDelegate.resetFilterExceptions()
|
2024-02-26 08:12:21 +01:00
|
|
|
|
self.selectFeed(feed, animations: animations, completion: completion)
|
2020-10-25 18:34:02 +01:00
|
|
|
|
})
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2019-08-19 22:45:52 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 02:12:24 +01:00
|
|
|
|
func showStatusBar() {
|
|
|
|
|
prefersStatusBarHidden = false
|
|
|
|
|
UIView.animate(withDuration: 0.15) {
|
|
|
|
|
self.rootSplitViewController.setNeedsStatusBarAppearanceUpdate()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func hideStatusBar() {
|
|
|
|
|
prefersStatusBarHidden = true
|
|
|
|
|
UIView.animate(withDuration: 0.15) {
|
|
|
|
|
self.rootSplitViewController.setNeedsStatusBarAppearanceUpdate()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-11 19:30:16 +01:00
|
|
|
|
func showSettings(scrollToArticlesSection: Bool = false) {
|
2019-10-21 18:51:33 +02:00
|
|
|
|
let settingsNavController = UIStoryboard.settings.instantiateInitialViewController() as! UINavigationController
|
2019-10-22 01:02:44 +02:00
|
|
|
|
let settingsViewController = settingsNavController.topViewController as! SettingsViewController
|
2020-01-11 19:30:16 +01:00
|
|
|
|
settingsViewController.scrollToArticlesSection = scrollToArticlesSection
|
2019-10-21 18:51:33 +02:00
|
|
|
|
settingsNavController.modalPresentationStyle = .formSheet
|
2019-10-22 01:02:44 +02:00
|
|
|
|
settingsViewController.presentingParentController = rootSplitViewController
|
|
|
|
|
rootSplitViewController.present(settingsNavController, animated: true)
|
2019-07-06 19:25:45 +02:00
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-10-24 02:58:18 +02:00
|
|
|
|
func showAccountInspector(for account: Account) {
|
|
|
|
|
let accountInspectorNavController =
|
|
|
|
|
UIStoryboard.inspector.instantiateViewController(identifier: "AccountInspectorNavigationViewController") as! UINavigationController
|
|
|
|
|
let accountInspectorController = accountInspectorNavController.topViewController as! AccountInspectorViewController
|
|
|
|
|
accountInspectorNavController.modalPresentationStyle = .formSheet
|
|
|
|
|
accountInspectorNavController.preferredContentSize = AccountInspectorViewController.preferredContentSizeForFormSheetDisplay
|
|
|
|
|
accountInspectorController.isModal = true
|
|
|
|
|
accountInspectorController.account = account
|
|
|
|
|
rootSplitViewController.present(accountInspectorNavController, animated: true)
|
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-09-28 02:45:09 +02:00
|
|
|
|
func showFeedInspector() {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let timelineFeed = timelineFeed as? Feed
|
|
|
|
|
let articleFeed = currentArticle?.feed
|
|
|
|
|
guard let feed = timelineFeed ?? articleFeed else {
|
2019-09-28 02:45:09 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-07 13:40:10 +01:00
|
|
|
|
showFeedInspector(for: feed)
|
2019-09-28 02:45:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 06:41:18 +01:00
|
|
|
|
func showFeedInspector(for feed: Feed) {
|
2019-11-07 13:40:10 +01:00
|
|
|
|
let feedInspectorNavController =
|
|
|
|
|
UIStoryboard.inspector.instantiateViewController(identifier: "FeedInspectorNavigationViewController") as! UINavigationController
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let feedInspectorController = feedInspectorNavController.topViewController as! FeedInspectorViewController
|
2019-11-14 00:02:14 +01:00
|
|
|
|
feedInspectorNavController.modalPresentationStyle = .formSheet
|
2024-02-26 08:12:21 +01:00
|
|
|
|
feedInspectorNavController.preferredContentSize = FeedInspectorViewController.preferredContentSizeForFormSheetDisplay
|
|
|
|
|
feedInspectorController.feed = feed
|
2019-11-07 13:40:10 +01:00
|
|
|
|
rootSplitViewController.present(feedInspectorNavController, animated: true)
|
2019-09-28 14:00:18 +02:00
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
func showAddFeed(initialFeed: String? = nil, initialFeedName: String? = nil) {
|
2020-08-11 22:00:31 +02:00
|
|
|
|
|
|
|
|
|
// Since Add Feed can be opened from anywhere with a keyboard shortcut, we have to deselect any currently selected feeds
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectFeed(nil)
|
2019-09-06 17:57:37 +02:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let addNavViewController = UIStoryboard.add.instantiateViewController(withIdentifier: "AddFeedViewControllerNav") as! UINavigationController
|
2019-09-15 19:03:28 +02:00
|
|
|
|
|
2020-08-12 00:04:11 +02:00
|
|
|
|
let addViewController = addNavViewController.topViewController as! AddFeedViewController
|
2020-08-11 22:00:31 +02:00
|
|
|
|
addViewController.initialFeed = initialFeed
|
|
|
|
|
addViewController.initialFeedName = initialFeedName
|
2019-09-15 19:03:28 +02:00
|
|
|
|
|
2020-08-11 22:00:31 +02:00
|
|
|
|
addNavViewController.modalPresentationStyle = .formSheet
|
2020-08-12 00:04:11 +02:00
|
|
|
|
addNavViewController.preferredContentSize = AddFeedViewController.preferredContentSizeForFormSheetDisplay
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController.present(addNavViewController, animated: true)
|
2020-08-11 22:00:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showAddFolder() {
|
|
|
|
|
let addNavViewController = UIStoryboard.add.instantiateViewController(withIdentifier: "AddFolderViewControllerNav") as! UINavigationController
|
|
|
|
|
addNavViewController.modalPresentationStyle = .formSheet
|
|
|
|
|
addNavViewController.preferredContentSize = AddFolderViewController.preferredContentSizeForFormSheetDisplay
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController.present(addNavViewController, animated: true)
|
2019-07-06 19:25:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-01 02:06:27 +01:00
|
|
|
|
func showFullScreenImage(image: UIImage, imageTitle: String?, transitioningDelegate: UIViewControllerTransitioningDelegate) {
|
2019-10-17 03:20:36 +02:00
|
|
|
|
let imageVC = UIStoryboard.main.instantiateController(ofType: ImageViewController.self)
|
|
|
|
|
imageVC.image = image
|
2020-01-01 02:06:27 +01:00
|
|
|
|
imageVC.imageTitle = imageTitle
|
2019-10-17 03:20:36 +02:00
|
|
|
|
imageVC.modalPresentationStyle = .currentContext
|
|
|
|
|
imageVC.transitioningDelegate = transitioningDelegate
|
|
|
|
|
rootSplitViewController.present(imageVC, animated: true)
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-04 23:24:16 +02:00
|
|
|
|
func homePageURLForFeed(_ indexPath: IndexPath) -> URL? {
|
|
|
|
|
guard let node = nodeFor(indexPath),
|
2024-02-26 06:41:18 +01:00
|
|
|
|
let feed = node.representedObject as? Feed,
|
2019-09-04 23:24:16 +02:00
|
|
|
|
let homePageURL = feed.homePageURL,
|
|
|
|
|
let url = URL(string: homePageURL) else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return url
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showBrowserForFeed(_ indexPath: IndexPath) {
|
|
|
|
|
if let url = homePageURLForFeed(indexPath) {
|
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func showBrowserForCurrentFeed() {
|
2019-09-05 04:06:29 +02:00
|
|
|
|
if let ip = currentFeedIndexPath, let url = homePageURLForFeed(ip) {
|
2019-09-04 23:24:16 +02:00
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 16:11:33 +02:00
|
|
|
|
func showBrowserForArticle(_ article: Article) {
|
2021-04-25 23:28:19 +02:00
|
|
|
|
guard let url = article.preferredURL else { return }
|
2019-08-20 00:38:30 +02:00
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-06 18:49:53 +02:00
|
|
|
|
func showBrowserForCurrentArticle() {
|
2021-04-25 23:28:19 +02:00
|
|
|
|
guard let url = currentArticle?.preferredURL else { return }
|
2019-07-06 18:49:53 +02:00
|
|
|
|
UIApplication.shared.open(url, options: [:])
|
|
|
|
|
}
|
2020-05-29 17:09:04 +02:00
|
|
|
|
|
|
|
|
|
func showInAppBrowser() {
|
|
|
|
|
if currentArticle != nil {
|
|
|
|
|
articleViewController?.openInAppBrowser()
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController.openInAppBrowser()
|
2020-05-29 17:09:04 +02:00
|
|
|
|
}
|
2020-05-14 14:28:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-05 04:06:29 +02:00
|
|
|
|
func navigateToFeeds() {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController?.focus()
|
2019-09-05 04:06:29 +02:00
|
|
|
|
selectArticle(nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func navigateToTimeline() {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if currentArticle == nil && articles.count > 0 {
|
|
|
|
|
selectArticle(articles[0])
|
2019-09-05 04:06:29 +02:00
|
|
|
|
}
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.focus()
|
2019-09-05 04:06:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func navigateToDetail() {
|
2019-09-24 11:29:15 +02:00
|
|
|
|
articleViewController?.focus()
|
2019-09-05 04:06:29 +02:00
|
|
|
|
}
|
2020-05-13 13:59:59 +02:00
|
|
|
|
|
|
|
|
|
func toggleSidebar() {
|
2024-02-26 03:56:41 +01:00
|
|
|
|
rootSplitViewController.preferredDisplayMode = rootSplitViewController.displayMode == .oneBesideSecondary ? .secondaryOnly : .oneBesideSecondary
|
2020-05-13 13:59:59 +02:00
|
|
|
|
}
|
2020-11-19 04:30:52 +01:00
|
|
|
|
|
2021-09-13 09:22:15 +02:00
|
|
|
|
func selectArticleInCurrentFeed(_ articleID: String, isShowingExtractedArticle: Bool? = nil, articleWindowScrollY: Int? = nil) {
|
2020-11-19 04:30:52 +01:00
|
|
|
|
if let article = self.articles.first(where: { $0.articleID == articleID }) {
|
2021-09-13 09:22:15 +02:00
|
|
|
|
self.selectArticle(article, isShowingExtractedArticle: isShowingExtractedArticle, articleWindowScrollY: articleWindowScrollY)
|
2020-11-19 04:30:52 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-12 21:46:15 +02:00
|
|
|
|
|
|
|
|
|
func importTheme(filename: String) {
|
2021-09-21 03:22:45 +02:00
|
|
|
|
do {
|
|
|
|
|
try ArticleThemeImporter.importTheme(controller: rootSplitViewController, filename: filename)
|
|
|
|
|
} catch {
|
|
|
|
|
NotificationCenter.default.post(name: .didFailToImportThemeWithError, object: nil, userInfo: ["error" : error])
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 21:46:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 02:52:12 +01:00
|
|
|
|
/// This will dismiss the foremost view controller if the user
|
|
|
|
|
/// has launched from an external action (i.e., a widget tap, or
|
2022-01-04 23:25:20 +01:00
|
|
|
|
/// selecting an article via a notification).
|
2021-11-08 02:52:12 +01:00
|
|
|
|
///
|
|
|
|
|
/// The dismiss is only applicable if the view controller is a
|
|
|
|
|
/// `SFSafariViewController` or `SettingsViewController`,
|
|
|
|
|
/// otherwise, this function does nothing.
|
|
|
|
|
func dismissIfLaunchingFromExternalAction() {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
guard let presentedController = sidebarViewController.presentedViewController else { return }
|
2024-02-26 17:37:15 +01:00
|
|
|
|
|
2021-11-08 02:52:12 +01:00
|
|
|
|
if presentedController.isKind(of: SFSafariViewController.self) {
|
|
|
|
|
presentedController.dismiss(animated: true, completion: nil)
|
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
// There's no obvious way to detect if the presented controller
|
|
|
|
|
// is the SwiftUI UIHostingController<SettingsView>. Posting a notification
|
|
|
|
|
// which it can react to seems to be the simplest solution.
|
|
|
|
|
NotificationCenter.default.post(name: .LaunchedFromExternalAction, object: nil)
|
2021-11-08 02:52:12 +01:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: UISplitViewControllerDelegate
|
|
|
|
|
|
2019-09-01 19:43:07 +02:00
|
|
|
|
extension SceneCoordinator: UISplitViewControllerDelegate {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
func splitViewController(_ svc: UISplitViewController, topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column) -> UISplitViewController.Column {
|
|
|
|
|
switch proposedTopColumn {
|
|
|
|
|
case .supplementary:
|
|
|
|
|
if currentFeedIndexPath != nil {
|
|
|
|
|
return .supplementary
|
|
|
|
|
} else {
|
|
|
|
|
return .primary
|
|
|
|
|
}
|
|
|
|
|
case .secondary:
|
2019-12-01 19:04:28 +01:00
|
|
|
|
if currentArticle != nil {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
return .secondary
|
|
|
|
|
} else {
|
|
|
|
|
if currentFeedIndexPath != nil {
|
|
|
|
|
return .supplementary
|
|
|
|
|
} else {
|
|
|
|
|
return .primary
|
|
|
|
|
}
|
2019-12-01 19:04:28 +01:00
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
default:
|
|
|
|
|
return .primary
|
2019-11-20 23:41:13 +01:00
|
|
|
|
}
|
2019-08-02 23:46:55 +02:00
|
|
|
|
}
|
2020-03-13 22:03:42 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
func splitViewController(_ svc: UISplitViewController, willChangeTo displayMode: UISplitViewController.DisplayMode) {
|
|
|
|
|
articleViewController?.splitViewControllerWillChangeTo(displayMode: displayMode)
|
2019-11-04 01:10:49 +01:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-09-01 22:31:11 +02:00
|
|
|
|
// MARK: UINavigationControllerDelegate
|
|
|
|
|
|
|
|
|
|
extension SceneCoordinator: UINavigationControllerDelegate {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-09-01 22:31:11 +02:00
|
|
|
|
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
guard UIApplication.shared.applicationState != .background else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard rootSplitViewController.isCollapsed else {
|
2019-10-01 11:31:42 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-19 02:12:24 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
defer {
|
|
|
|
|
if let mainController = viewController as? MainControllerIdentifiable {
|
|
|
|
|
lastMainControllerToAppear = mainController.mainControllerIdentifier
|
|
|
|
|
} else if let mainController = (viewController as? UINavigationController)?.topViewController as? MainControllerIdentifiable {
|
|
|
|
|
lastMainControllerToAppear = mainController.mainControllerIdentifier
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-11 00:18:10 +02:00
|
|
|
|
// If we are showing the Feeds and only the feeds start clearing stuff
|
2024-03-04 07:51:53 +01:00
|
|
|
|
if viewController === sidebarViewController && lastMainControllerToAppear == .mainTimeline {
|
2019-09-01 22:31:11 +02:00
|
|
|
|
activityManager.invalidateCurrentActivities()
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectFeed(nil, animations: [.scroll, .select, .navigation])
|
2019-10-01 10:51:48 +02:00
|
|
|
|
return
|
2019-09-01 22:31:11 +02:00
|
|
|
|
}
|
2019-11-01 02:55:08 +01:00
|
|
|
|
|
2019-10-01 10:51:48 +02:00
|
|
|
|
// If we are using a phone and navigate away from the detail, clear up the article resources (including activity).
|
|
|
|
|
// Don't clear it if we have pushed an ArticleViewController, but don't yet see it on the navigation stack.
|
|
|
|
|
// This happens when we are going to the next unread and we need to grab another timeline to continue. The
|
2022-01-04 23:25:20 +01:00
|
|
|
|
// ArticleViewController will be pushed, but we will briefly show the Timeline. Don't clear things out when that happens.
|
2024-03-04 07:51:53 +01:00
|
|
|
|
if viewController === timelineViewController && lastMainControllerToAppear == .article {
|
|
|
|
|
selectArticle(nil, animations: [.scroll, .select, .navigation])
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
|
|
|
|
// Restore any bars hidden by the article controller
|
|
|
|
|
showStatusBar()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
|
|
|
|
// We delay the showing of the navigation bars because it freaks out on iOS 15 with the new split view controller
|
|
|
|
|
// if it is trying to show at the same time as the show timeline animation
|
|
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
|
|
|
|
navigationController.setNavigationBarHidden(false, animated: true)
|
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
navigationController.setToolbarHidden(false, animated: true)
|
2019-10-01 10:51:48 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-09-01 22:31:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
// MARK: Private
|
|
|
|
|
|
2019-09-01 19:43:07 +02:00
|
|
|
|
private extension SceneCoordinator {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
func configureNavigationController(_ navController: UINavigationController) {
|
|
|
|
|
|
|
|
|
|
let scrollEdge = UINavigationBarAppearance()
|
|
|
|
|
scrollEdge.configureWithOpaqueBackground()
|
|
|
|
|
scrollEdge.shadowColor = nil
|
|
|
|
|
scrollEdge.shadowImage = UIImage()
|
|
|
|
|
|
|
|
|
|
let standard = UINavigationBarAppearance()
|
|
|
|
|
standard.shadowColor = .opaqueSeparator
|
|
|
|
|
standard.shadowImage = UIImage()
|
|
|
|
|
|
|
|
|
|
navController.navigationBar.standardAppearance = standard
|
|
|
|
|
navController.navigationBar.compactAppearance = standard
|
|
|
|
|
navController.navigationBar.scrollEdgeAppearance = scrollEdge
|
|
|
|
|
navController.navigationBar.compactScrollEdgeAppearance = scrollEdge
|
|
|
|
|
|
|
|
|
|
navController.navigationBar.tintColor = AppAssets.primaryAccentColor
|
|
|
|
|
|
|
|
|
|
let toolbarAppearance = UIToolbarAppearance()
|
|
|
|
|
navController.toolbar.standardAppearance = toolbarAppearance
|
|
|
|
|
navController.toolbar.compactAppearance = toolbarAppearance
|
|
|
|
|
navController.toolbar.scrollEdgeAppearance = toolbarAppearance
|
|
|
|
|
navController.toolbar.tintColor = AppAssets.primaryAccentColor
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 02:41:01 +02:00
|
|
|
|
func markArticlesWithUndo(_ articles: [Article], statusKey: ArticleStatus.Key, flag: Bool, completion: (() -> Void)? = nil) {
|
|
|
|
|
guard let undoManager = undoManager,
|
|
|
|
|
let markReadCommand = MarkStatusCommand(initialArticles: articles, statusKey: statusKey, flag: flag, undoManager: undoManager, completion: completion) else {
|
2021-04-13 04:09:34 +02:00
|
|
|
|
completion?()
|
2019-10-04 18:41:30 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runCommand(markReadCommand)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 20:10:08 +02:00
|
|
|
|
func updateUnreadCount() {
|
|
|
|
|
var count = 0
|
|
|
|
|
for article in articles {
|
|
|
|
|
if !article.status.read {
|
|
|
|
|
count += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-21 02:03:02 +02:00
|
|
|
|
timelineUnreadCount = count
|
2019-08-21 20:10:08 +02:00
|
|
|
|
}
|
2019-11-24 17:27:02 +01:00
|
|
|
|
|
2020-01-27 20:58:32 +01:00
|
|
|
|
func rebuildArticleDictionaries() {
|
|
|
|
|
var idDictionary = [String: Article]()
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
for article in articles {
|
2020-01-27 20:58:32 +01:00
|
|
|
|
idDictionary[article.articleID] = article
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_idToArticleDictionary = idDictionary
|
|
|
|
|
articleDictionaryNeedsUpdate = false
|
|
|
|
|
}
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
func ensureFeedIsAvailableToSelect(_ feed: SidebarItem, completion: @escaping () -> Void) {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
addToFilterExeptionsIfNecessary(feed)
|
|
|
|
|
addShadowTableToFilterExceptions()
|
|
|
|
|
|
2020-10-25 18:34:02 +01:00
|
|
|
|
rebuildBackingStores(completion: {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.treeControllerDelegate.resetFilterExceptions()
|
|
|
|
|
completion()
|
2020-10-25 18:34:02 +01:00
|
|
|
|
})
|
2020-01-29 00:51:50 +01:00
|
|
|
|
}
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
func addToFilterExeptionsIfNecessary(_ feed: SidebarItem?) {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
if isReadFeedsFiltered, let sidebarItemID = feed?.sidebarItemID {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
if feed is SmartFeed {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
} else if let folderFeed = feed as? Folder {
|
2020-01-29 00:51:50 +01:00
|
|
|
|
if folderFeed.account?.existingFolder(withID: folderFeed.folderID) != nil {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2020-01-29 00:51:50 +01:00
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
|
} else if let feed = feed as? Feed {
|
|
|
|
|
if feed.account?.existingFeed(withFeedID: feed.feedID) != nil {
|
2024-02-26 06:34:22 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2024-02-26 08:12:21 +01:00
|
|
|
|
addParentFolderToFilterExceptions(feed)
|
2020-01-29 00:51:50 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
func addParentFolderToFilterExceptions(_ feed: SidebarItem) {
|
2020-01-29 19:18:17 +01:00
|
|
|
|
guard let node = treeController.rootNode.descendantNodeRepresentingObject(feed as AnyObject),
|
|
|
|
|
let folder = node.parent?.representedObject as? Folder,
|
2024-02-26 06:34:22 +01:00
|
|
|
|
let folderFeedID = folder.sidebarItemID else {
|
2020-01-29 19:18:17 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
treeControllerDelegate.addFilterException(folderFeedID)
|
|
|
|
|
}
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
|
|
|
|
func addShadowTableToFilterExceptions() {
|
|
|
|
|
for section in shadowTable {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
for sidebarItemNode in section.sidebarItemNodes {
|
|
|
|
|
if let sidebarItem = sidebarItemNode.node.representedObject as? SidebarItem, let sidebarItemID = sidebarItem.sidebarItemID {
|
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-28 07:43:54 +01:00
|
|
|
|
|
|
|
|
|
func queueRebuildBackingStores() {
|
|
|
|
|
rebuildBackingStoresQueue.add(self, #selector(rebuildBackingStoresWithDefaults))
|
|
|
|
|
}
|
2020-01-29 19:18:17 +01:00
|
|
|
|
|
2020-03-28 00:24:57 +01:00
|
|
|
|
@objc func rebuildBackingStoresWithDefaults() {
|
|
|
|
|
rebuildBackingStores()
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func rebuildBackingStores(initialLoad: Bool = false, updateExpandedNodes: (() -> Void)? = nil, completion: (() -> Void)? = nil) {
|
2021-08-24 23:55:44 +02:00
|
|
|
|
if !BatchUpdate.shared.isPerforming {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
addToFilterExeptionsIfNecessary(timelineFeed)
|
|
|
|
|
treeController.rebuild()
|
|
|
|
|
treeControllerDelegate.resetFilterExceptions()
|
|
|
|
|
|
|
|
|
|
updateExpandedNodes?()
|
2021-10-21 02:03:02 +02:00
|
|
|
|
let changes = rebuildShadowTable()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
sidebarViewController.reloadFeeds(initialLoad: initialLoad, changes: changes, completion: completion)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 03:37:29 +02:00
|
|
|
|
func rebuildShadowTable() -> ShadowTableChanges {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
var newShadowTable = [(sectionID: String, sidebarItemNodes: [SidebarItemNode])]()
|
2019-08-31 18:38:03 +02:00
|
|
|
|
|
|
|
|
|
for i in 0..<treeController.rootNode.numberOfChildNodes {
|
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
var sidebarItemNodes = [SidebarItemNode]()
|
2019-09-08 15:55:07 +02:00
|
|
|
|
let sectionNode = treeController.rootNode.childAtIndex(i)!
|
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
|
if isExpanded(sectionNode) {
|
2019-09-08 15:55:07 +02:00
|
|
|
|
for node in sectionNode.childNodes {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
let sidebarItemNode = SidebarItemNode(node)
|
|
|
|
|
sidebarItemNodes.append(sidebarItemNode)
|
2019-11-25 01:29:00 +01:00
|
|
|
|
if isExpanded(node) {
|
2019-08-31 18:38:03 +02:00
|
|
|
|
for child in node.childNodes {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
let childNode = SidebarItemNode(child)
|
|
|
|
|
sidebarItemNodes.append(childNode)
|
2019-08-31 18:38:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2021-10-21 03:37:29 +02:00
|
|
|
|
let sectionID = (sectionNode.representedObject as? Account)?.accountID ?? ""
|
2024-03-04 08:01:00 +01:00
|
|
|
|
newShadowTable.append((sectionID: sectionID, sidebarItemNodes: sidebarItemNodes))
|
2019-08-31 18:38:03 +02:00
|
|
|
|
}
|
2020-01-30 01:06:35 +01:00
|
|
|
|
|
|
|
|
|
// If we have a current Feed IndexPath it is no longer valid and needs reset.
|
|
|
|
|
if currentFeedIndexPath != nil {
|
|
|
|
|
currentFeedIndexPath = indexPathFor(timelineFeed as AnyObject)
|
|
|
|
|
}
|
2021-10-21 02:03:02 +02:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
// Compute the differences in the shadow table rows and the expanded table entries
|
2021-10-21 03:37:29 +02:00
|
|
|
|
var changes = [ShadowTableChanges.RowChanges]()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
let expandedTableDifference = lastExpandedTable.symmetricDifference(expandedTable)
|
|
|
|
|
|
2021-10-21 03:37:29 +02:00
|
|
|
|
for (section, newSectionRows) in newShadowTable.enumerated() {
|
2021-10-21 02:03:02 +02:00
|
|
|
|
var moves = Set<ShadowTableChanges.Move>()
|
|
|
|
|
var inserts = Set<Int>()
|
|
|
|
|
var deletes = Set<Int>()
|
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
let oldFeedNodes = shadowTable.first(where: { $0.sectionID == newSectionRows.sectionID })?.sidebarItemNodes ?? [SidebarItemNode]()
|
|
|
|
|
|
|
|
|
|
let diff = newSectionRows.sidebarItemNodes.difference(from: oldFeedNodes).inferringMoves()
|
2021-10-21 02:03:02 +02:00
|
|
|
|
for change in diff {
|
|
|
|
|
switch change {
|
|
|
|
|
case .insert(let offset, _, let associated):
|
|
|
|
|
if let associated = associated {
|
|
|
|
|
moves.insert(ShadowTableChanges.Move(associated, offset))
|
|
|
|
|
} else {
|
|
|
|
|
inserts.insert(offset)
|
|
|
|
|
}
|
|
|
|
|
case .remove(let offset, _, let associated):
|
|
|
|
|
if let associated = associated {
|
|
|
|
|
moves.insert(ShadowTableChanges.Move(offset, associated))
|
|
|
|
|
} else {
|
|
|
|
|
deletes.insert(offset)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
// We need to reload the difference in expanded rows to get the disclosure arrows correct when programmatically changing their state
|
|
|
|
|
var reloads = Set<Int>()
|
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
for (index, newFeedNode) in newSectionRows.sidebarItemNodes.enumerated() {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
if let newFeedNodeContainerID = (newFeedNode.node.representedObject as? Container)?.containerID {
|
|
|
|
|
if expandedTableDifference.contains(newFeedNodeContainerID) {
|
|
|
|
|
reloads.insert(index)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changes.append(ShadowTableChanges.RowChanges(section: section, deletes: deletes, inserts: inserts, reloads: reloads, moves: moves))
|
2021-10-21 03:37:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
lastExpandedTable = expandedTable
|
|
|
|
|
|
2021-10-21 03:37:29 +02:00
|
|
|
|
// Compute the difference in the shadow table sections
|
|
|
|
|
var moves = Set<ShadowTableChanges.Move>()
|
|
|
|
|
var inserts = Set<Int>()
|
|
|
|
|
var deletes = Set<Int>()
|
|
|
|
|
|
|
|
|
|
let oldSections = shadowTable.map { $0.sectionID }
|
|
|
|
|
let newSections = newShadowTable.map { $0.sectionID }
|
|
|
|
|
let diff = newSections.difference(from: oldSections).inferringMoves()
|
|
|
|
|
for change in diff {
|
|
|
|
|
switch change {
|
|
|
|
|
case .insert(let offset, _, let associated):
|
|
|
|
|
if let associated = associated {
|
|
|
|
|
moves.insert(ShadowTableChanges.Move(associated, offset))
|
|
|
|
|
} else {
|
|
|
|
|
inserts.insert(offset)
|
|
|
|
|
}
|
|
|
|
|
case .remove(let offset, _, let associated):
|
|
|
|
|
if let associated = associated {
|
|
|
|
|
moves.insert(ShadowTableChanges.Move(offset, associated))
|
|
|
|
|
} else {
|
|
|
|
|
deletes.insert(offset)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-21 02:03:02 +02:00
|
|
|
|
}
|
2021-10-21 03:37:29 +02:00
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
|
shadowTable = newShadowTable
|
|
|
|
|
|
2021-10-21 03:37:29 +02:00
|
|
|
|
return ShadowTableChanges(deletes: deletes, inserts: inserts, moves: moves, rowChanges: changes)
|
2019-08-31 18:38:03 +02:00
|
|
|
|
}
|
2020-01-20 00:44:13 +01:00
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
func shadowTableContains(_ sidebarItem: SidebarItem) -> Bool {
|
2020-01-20 00:44:13 +01:00
|
|
|
|
for section in shadowTable {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
for sidebarItemNode in section.sidebarItemNodes {
|
|
|
|
|
if let node = sidebarItemNode.node.representedObject as? SidebarItem, node.sidebarItemID == sidebarItem.sidebarItemID {
|
2020-01-20 00:44:13 +01:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-01-27 05:18:43 +01:00
|
|
|
|
|
|
|
|
|
func clearTimelineIfNoLongerAvailable() {
|
|
|
|
|
if let feed = timelineFeed, !shadowTableContains(feed) {
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectFeed(nil, deselectArticle: true)
|
2020-01-27 05:18:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-31 18:38:03 +02:00
|
|
|
|
|
2024-02-26 06:14:10 +01:00
|
|
|
|
func setTimelineFeed(_ feed: SidebarItem?, animated: Bool, completion: (() -> Void)? = nil) {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
timelineFeed = feed
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-11-24 17:27:02 +01:00
|
|
|
|
fetchAndReplaceArticlesAsync(animated: animated) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: true)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
completion?()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateShowNamesAndIcons() {
|
|
|
|
|
|
2024-02-26 06:41:18 +01:00
|
|
|
|
if timelineFeed is Feed {
|
2020-04-18 14:53:56 +02:00
|
|
|
|
showFeedNames = {
|
|
|
|
|
for article in articles {
|
2020-04-20 00:29:11 +02:00
|
|
|
|
if !article.byline().isEmpty {
|
2020-04-18 14:53:56 +02:00
|
|
|
|
return .byline
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return .none
|
|
|
|
|
}()
|
2019-11-19 18:16:43 +01:00
|
|
|
|
} else {
|
2020-04-18 14:53:56 +02:00
|
|
|
|
showFeedNames = .feed
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 14:53:56 +02:00
|
|
|
|
if showFeedNames == .feed {
|
2019-11-06 01:05:57 +01:00
|
|
|
|
self.showIcons = true
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 00:29:11 +02:00
|
|
|
|
if showFeedNames == .none {
|
|
|
|
|
self.showIcons = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
for article in articles {
|
|
|
|
|
if let authors = article.authors {
|
|
|
|
|
for author in authors {
|
|
|
|
|
if author.avatarURL != nil {
|
2019-11-06 01:05:57 +01:00
|
|
|
|
self.showIcons = true
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
|
self.showIcons = false
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func markExpanded(_ containerID: ContainerIdentifier) {
|
|
|
|
|
expandedTable.insert(containerID)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func markExpanded(_ containerIdentifiable: ContainerIdentifiable) {
|
|
|
|
|
if let containerID = containerIdentifiable.containerID {
|
2020-06-16 01:03:20 +02:00
|
|
|
|
markExpanded(containerID)
|
2019-11-27 00:32:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func markExpanded(_ node: Node) {
|
|
|
|
|
if let containerIdentifiable = node.representedObject as? ContainerIdentifiable {
|
|
|
|
|
markExpanded(containerIdentifiable)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 01:03:20 +02:00
|
|
|
|
func unmarkExpanded(_ containerID: ContainerIdentifier) {
|
|
|
|
|
expandedTable.remove(containerID)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func unmarkExpanded(_ containerIdentifiable: ContainerIdentifiable) {
|
|
|
|
|
if let containerID = containerIdentifiable.containerID {
|
2020-06-16 01:03:20 +02:00
|
|
|
|
unmarkExpanded(containerID)
|
2019-11-27 00:32:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func unmarkExpanded(_ node: Node) {
|
|
|
|
|
if let containerIdentifiable = node.representedObject as? ContainerIdentifiable {
|
|
|
|
|
unmarkExpanded(containerIdentifiable)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
// MARK: Select Prev Unread
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
@discardableResult
|
|
|
|
|
func selectPrevUnreadArticleInTimeline() -> Bool {
|
|
|
|
|
let startingRow: Int = {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if let articleRow = currentArticleRow {
|
|
|
|
|
return articleRow
|
2019-09-06 01:02:40 +02:00
|
|
|
|
} else {
|
|
|
|
|
return articles.count - 1
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
return selectPrevArticleInTimeline(startingRow: startingRow)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectPrevArticleInTimeline(startingRow: Int) -> Bool {
|
|
|
|
|
|
|
|
|
|
guard startingRow >= 0 else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i in (0...startingRow).reversed() {
|
|
|
|
|
let article = articles[i]
|
|
|
|
|
if !article.status.read {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
selectArticle(article)
|
2019-09-06 01:02:40 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectPrevUnreadFeedFetcher() {
|
|
|
|
|
|
|
|
|
|
let indexPath: IndexPath = {
|
|
|
|
|
if currentFeedIndexPath == nil {
|
|
|
|
|
return IndexPath(row: 0, section: 0)
|
|
|
|
|
} else {
|
|
|
|
|
return currentFeedIndexPath!
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Increment or wrap around the IndexPath
|
|
|
|
|
let nextIndexPath: IndexPath = {
|
|
|
|
|
if indexPath.row - 1 < 0 {
|
|
|
|
|
if indexPath.section - 1 < 0 {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
return IndexPath(row: shadowTable[shadowTable.count - 1].sidebarItemNodes.count - 1, section: shadowTable.count - 1)
|
2019-09-06 01:02:40 +02:00
|
|
|
|
} else {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
return IndexPath(row: shadowTable[indexPath.section - 1].sidebarItemNodes.count - 1, section: indexPath.section - 1)
|
2019-09-06 01:02:40 +02:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return IndexPath(row: indexPath.row - 1, section: indexPath.section)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if selectPrevUnreadFeedFetcher(startingWith: nextIndexPath) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-04 08:01:00 +01:00
|
|
|
|
let maxIndexPath = IndexPath(row: shadowTable[shadowTable.count - 1].sidebarItemNodes.count - 1, section: shadowTable.count - 1)
|
2019-09-06 01:02:40 +02:00
|
|
|
|
selectPrevUnreadFeedFetcher(startingWith: maxIndexPath)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@discardableResult
|
|
|
|
|
func selectPrevUnreadFeedFetcher(startingWith indexPath: IndexPath) -> Bool {
|
|
|
|
|
|
|
|
|
|
for i in (0...indexPath.section).reversed() {
|
|
|
|
|
|
|
|
|
|
let startingRow: Int = {
|
|
|
|
|
if indexPath.section == i {
|
|
|
|
|
return indexPath.row
|
|
|
|
|
} else {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
return shadowTable[i].sidebarItemNodes.count - 1
|
2019-09-06 01:02:40 +02:00
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
for j in (0...startingRow).reversed() {
|
|
|
|
|
|
|
|
|
|
let prevIndexPath = IndexPath(row: j, section: i)
|
|
|
|
|
guard let node = nodeFor(prevIndexPath), let unreadCountProvider = node.representedObject as? UnreadCountProvider else {
|
|
|
|
|
assertionFailure()
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
|
if isExpanded(node) {
|
2019-09-06 01:02:40 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if unreadCountProvider.unreadCount > 0 {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
selectFeed(indexPath: prevIndexPath, animations: [.scroll, .navigation])
|
2019-09-06 01:02:40 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Select Next Unread
|
|
|
|
|
|
2019-08-04 00:07:43 +02:00
|
|
|
|
@discardableResult
|
|
|
|
|
func selectFirstUnreadArticleInTimeline() -> Bool {
|
2019-10-10 04:39:11 +02:00
|
|
|
|
return selectNextArticleInTimeline(startingRow: 0, animated: true)
|
2019-08-04 00:07:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
@discardableResult
|
|
|
|
|
func selectNextUnreadArticleInTimeline() -> Bool {
|
|
|
|
|
let startingRow: Int = {
|
2019-09-11 16:11:33 +02:00
|
|
|
|
if let articleRow = currentArticleRow {
|
|
|
|
|
return articleRow + 1
|
2019-06-29 20:35:12 +02:00
|
|
|
|
} else {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2019-10-10 04:39:11 +02:00
|
|
|
|
return selectNextArticleInTimeline(startingRow: startingRow, animated: false)
|
2019-08-04 00:07:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 04:39:11 +02:00
|
|
|
|
func selectNextArticleInTimeline(startingRow: Int, animated: Bool) -> Bool {
|
2019-08-04 00:07:43 +02:00
|
|
|
|
|
2019-09-03 00:24:20 +02:00
|
|
|
|
guard startingRow < articles.count else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
for i in startingRow..<articles.count {
|
|
|
|
|
let article = articles[i]
|
|
|
|
|
if !article.status.read {
|
2020-01-28 05:57:52 +01:00
|
|
|
|
selectArticle(article, animations: [.scroll, .navigation])
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
func selectNextUnreadFeed(completion: @escaping () -> Void) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
let indexPath: IndexPath = {
|
|
|
|
|
if currentFeedIndexPath == nil {
|
|
|
|
|
return IndexPath(row: -1, section: 0)
|
|
|
|
|
} else {
|
|
|
|
|
return currentFeedIndexPath!
|
|
|
|
|
}
|
|
|
|
|
}()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
|
|
|
|
// Increment or wrap around the IndexPath
|
|
|
|
|
let nextIndexPath: IndexPath = {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if indexPath.row + 1 >= shadowTable[indexPath.section].sidebarItemNodes.count {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
if indexPath.section + 1 >= shadowTable.count {
|
|
|
|
|
return IndexPath(row: 0, section: 0)
|
|
|
|
|
} else {
|
|
|
|
|
return IndexPath(row: 0, section: indexPath.section + 1)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return IndexPath(row: indexPath.row + 1, section: indexPath.section)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
selectNextUnreadFeed(startingWith: nextIndexPath) { found in
|
|
|
|
|
if !found {
|
|
|
|
|
self.selectNextUnreadFeed(startingWith: IndexPath(row: 0, section: 0)) { _ in
|
|
|
|
|
completion()
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
completion()
|
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
func selectNextUnreadFeed(startingWith indexPath: IndexPath, completion: @escaping (Bool) -> Void) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
|
|
|
|
for i in indexPath.section..<shadowTable.count {
|
|
|
|
|
|
2019-09-06 01:02:40 +02:00
|
|
|
|
let startingRow: Int = {
|
|
|
|
|
if indexPath.section == i {
|
|
|
|
|
return indexPath.row
|
|
|
|
|
} else {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
}()
|
2020-04-13 03:23:20 +02:00
|
|
|
|
|
2024-03-04 08:01:00 +01:00
|
|
|
|
for j in startingRow..<shadowTable[i].sidebarItemNodes.count {
|
2020-04-13 03:23:20 +02:00
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
let nextIndexPath = IndexPath(row: j, section: i)
|
|
|
|
|
guard let node = nodeFor(nextIndexPath), let unreadCountProvider = node.representedObject as? UnreadCountProvider else {
|
|
|
|
|
assertionFailure()
|
2019-11-24 21:36:17 +01:00
|
|
|
|
completion(false)
|
2019-11-24 20:37:56 +01:00
|
|
|
|
return
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
2020-04-13 03:23:20 +02:00
|
|
|
|
|
2019-11-25 01:29:00 +01:00
|
|
|
|
if isExpanded(node) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if unreadCountProvider.unreadCount > 0 {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
selectFeed(indexPath: nextIndexPath, animations: [.scroll, .navigation], deselectArticle: false) {
|
2019-11-24 21:36:17 +01:00
|
|
|
|
self.currentArticle = nil
|
2019-11-24 20:37:56 +01:00
|
|
|
|
completion(true)
|
|
|
|
|
}
|
|
|
|
|
return
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:37:56 +01:00
|
|
|
|
completion(false)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Fetching Articles
|
|
|
|
|
|
|
|
|
|
func emptyTheTimeline() {
|
|
|
|
|
if !articles.isEmpty {
|
2019-11-24 17:27:02 +01:00
|
|
|
|
replaceArticles(with: Set<Article>(), animated: false)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-13 15:29:56 +02:00
|
|
|
|
func sortParametersDidChange() {
|
2019-11-24 17:27:02 +01:00
|
|
|
|
replaceArticles(with: Set(articles), animated: true)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
2019-09-13 15:29:56 +02:00
|
|
|
|
|
2019-11-24 17:27:02 +01:00
|
|
|
|
func replaceArticles(with unsortedArticles: Set<Article>, animated: Bool) {
|
2019-09-09 00:41:00 +02:00
|
|
|
|
let sortedArticles = Array(unsortedArticles).sortedByDate(sortDirection, groupByFeed: groupByFeed)
|
2019-11-24 17:27:02 +01:00
|
|
|
|
replaceArticles(with: sortedArticles, animated: animated)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 17:27:02 +01:00
|
|
|
|
func replaceArticles(with sortedArticles: ArticleArray, animated: Bool) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
if articles != sortedArticles {
|
|
|
|
|
articles = sortedArticles
|
2019-11-19 18:16:43 +01:00
|
|
|
|
updateShowNamesAndIcons()
|
2019-09-03 23:07:02 +02:00
|
|
|
|
updateUnreadCount()
|
2024-02-26 17:37:15 +01:00
|
|
|
|
timelineViewController?.reloadArticles(animated: animated)
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queueFetchAndMergeArticles() {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
fetchAndMergeArticlesQueue.add(self, #selector(fetchAndMergeArticlesAsync))
|
2020-01-24 02:07:20 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc func fetchAndMergeArticlesAsync() {
|
2020-02-23 19:57:20 +01:00
|
|
|
|
fetchAndMergeArticlesAsync(animated: true) {
|
2024-02-26 17:37:15 +01:00
|
|
|
|
self.timelineViewController?.reinitializeArticles(resetScroll: false)
|
|
|
|
|
self.timelineViewController?.restoreSelectionIfNecessary(adjustScroll: false)
|
2020-02-23 19:57:20 +01:00
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-24 02:07:20 +01:00
|
|
|
|
func fetchAndMergeArticlesAsync(animated: Bool = true, completion: (() -> Void)? = nil) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
2019-11-15 13:19:14 +01:00
|
|
|
|
guard let timelineFeed = timelineFeed else {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 13:19:14 +01:00
|
|
|
|
fetchUnsortedArticlesAsync(for: [timelineFeed]) { [weak self] (unsortedArticles) in
|
2019-08-21 22:27:53 +02:00
|
|
|
|
// Merge articles by articleID. For any unique articleID in current articles, add to unsortedArticles.
|
|
|
|
|
guard let strongSelf = self else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let unsortedArticleIDs = unsortedArticles.articleIDs()
|
|
|
|
|
var updatedArticles = unsortedArticles
|
|
|
|
|
for article in strongSelf.articles {
|
|
|
|
|
if !unsortedArticleIDs.contains(article.articleID) {
|
|
|
|
|
updatedArticles.insert(article)
|
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if article.account?.existingFeed(withFeedID: article.feedID) == nil {
|
2020-01-24 02:07:20 +01:00
|
|
|
|
updatedArticles.remove(article)
|
|
|
|
|
}
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
2019-08-21 22:27:53 +02:00
|
|
|
|
|
2020-01-24 02:07:20 +01:00
|
|
|
|
strongSelf.replaceArticles(with: updatedArticles, animated: animated)
|
|
|
|
|
completion?()
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-21 22:58:55 +02:00
|
|
|
|
func cancelPendingAsyncFetches() {
|
|
|
|
|
fetchSerialNumber += 1
|
|
|
|
|
fetchRequestQueue.cancelAllRequests()
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 17:27:02 +01:00
|
|
|
|
func fetchAndReplaceArticlesAsync(animated: Bool, completion: @escaping () -> Void) {
|
2019-08-21 22:58:55 +02:00
|
|
|
|
// To be called when we need to do an entire fetch, but an async delay is okay.
|
|
|
|
|
// Example: we have the Today feed selected, and the calendar day just changed.
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-08-21 22:58:55 +02:00
|
|
|
|
cancelPendingAsyncFetches()
|
2024-03-04 07:51:53 +01:00
|
|
|
|
emptyTheTimeline()
|
|
|
|
|
|
2019-11-29 21:31:15 +01:00
|
|
|
|
guard let timelineFeed = timelineFeed else {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
completion()
|
2019-08-21 22:58:55 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2019-11-29 21:31:15 +01:00
|
|
|
|
var fetchers = [ArticleFetcher]()
|
|
|
|
|
fetchers.append(timelineFeed)
|
|
|
|
|
if exceptionArticleFetcher != nil {
|
|
|
|
|
fetchers.append(exceptionArticleFetcher!)
|
|
|
|
|
exceptionArticleFetcher = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchUnsortedArticlesAsync(for: fetchers) { [weak self] (articles) in
|
2019-11-24 17:27:02 +01:00
|
|
|
|
self?.replaceArticles(with: articles, animated: animated)
|
2019-08-29 22:02:45 +02:00
|
|
|
|
completion()
|
2019-08-21 22:58:55 +02:00
|
|
|
|
}
|
2019-08-21 22:27:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 02:01:34 +01:00
|
|
|
|
func fetchUnsortedArticlesAsync(for representedObjects: [Any], completion: @escaping ArticleSetBlock) {
|
2019-08-21 22:27:53 +02:00
|
|
|
|
// The callback will *not* be called if the fetch is no longer relevant — that is,
|
|
|
|
|
// if it’s been superseded by a newer fetch, or the timeline was emptied, etc., it won’t get called.
|
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
|
cancelPendingAsyncFetches()
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2020-09-13 01:09:42 +02:00
|
|
|
|
let fetchers = representedObjects.compactMap { $0 as? ArticleFetcher }
|
|
|
|
|
let fetchOperation = FetchRequestOperation(id: fetchSerialNumber, readFilterEnabledTable: readFilterEnabledTable, fetchers: fetchers) { [weak self] (articles, operation) in
|
2019-08-21 22:27:53 +02:00
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
|
guard !operation.isCanceled, let strongSelf = self, operation.id == strongSelf.fetchSerialNumber else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-12-15 02:01:34 +01:00
|
|
|
|
completion(articles)
|
2019-08-21 22:27:53 +02:00
|
|
|
|
}
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2019-08-21 22:27:53 +02:00
|
|
|
|
fetchRequestQueue.add(fetchOperation)
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
func timelineFetcherContainsAnyPseudoFeed() -> Bool {
|
2019-11-15 13:19:14 +01:00
|
|
|
|
if timelineFeed is PseudoFeed {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 22:02:45 +02:00
|
|
|
|
func timelineFetcherContainsAnyFolder() -> Bool {
|
2019-11-15 13:19:14 +01:00
|
|
|
|
if timelineFeed is Folder {
|
2019-08-29 22:02:45 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 06:41:18 +01:00
|
|
|
|
func timelineFetcherContainsAnyFeed(_ feeds: Set<Feed>) -> Bool {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
|
|
|
|
|
// Return true if there’s a match or if a folder contains (recursively) one of feeds
|
|
|
|
|
|
2024-02-26 06:41:18 +01:00
|
|
|
|
if let feed = timelineFeed as? Feed {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
for oneFeed in feeds {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if feed.feedID == oneFeed.feedID || feed.url == oneFeed.url {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-15 13:19:14 +01:00
|
|
|
|
} else if let folder = timelineFeed as? Folder {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
for oneFeed in feeds {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if folder.hasFeed(with: oneFeed.feedID) || folder.hasFeed(withURL: oneFeed.url) {
|
2019-06-29 20:35:12 +02:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
}
|
2019-07-26 23:26:22 +02:00
|
|
|
|
|
2019-08-24 21:57:51 +02:00
|
|
|
|
// MARK: NSUserActivity
|
2024-03-04 07:51:53 +01:00
|
|
|
|
|
2019-11-26 23:33:11 +01:00
|
|
|
|
func windowState() -> [AnyHashable: Any] {
|
2019-11-27 18:43:36 +01:00
|
|
|
|
let containerExpandedWindowState = expandedTable.map( { $0.userInfo })
|
|
|
|
|
var readArticlesFilterState = [[AnyHashable: AnyHashable]: Bool]()
|
|
|
|
|
for key in readFilterEnabledTable.keys {
|
|
|
|
|
readArticlesFilterState[key.userInfo] = readFilterEnabledTable[key]
|
|
|
|
|
}
|
2019-11-26 23:33:11 +01:00
|
|
|
|
return [
|
2019-11-27 03:23:12 +01:00
|
|
|
|
UserInfoKey.readFeedsFilterState: isReadFeedsFiltered,
|
2019-11-27 18:43:36 +01:00
|
|
|
|
UserInfoKey.containerExpandedWindowState: containerExpandedWindowState,
|
2024-03-04 07:51:53 +01:00
|
|
|
|
UserInfoKey.readArticlesFilterState: readArticlesFilterState,
|
|
|
|
|
UserInfoKey.isSidebarHidden: rootSplitViewController.displayMode == .secondaryOnly
|
2019-11-26 23:33:11 +01:00
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func handleSelectFeed(_ userInfo: [AnyHashable : Any]?) {
|
2019-11-14 22:06:32 +01:00
|
|
|
|
guard let userInfo = userInfo,
|
2019-11-27 18:43:36 +01:00
|
|
|
|
let feedIdentifierUserInfo = userInfo[UserInfoKey.feedIdentifier] as? [AnyHashable : AnyHashable],
|
2024-02-27 06:47:24 +01:00
|
|
|
|
let sidebarItemID = SidebarItemIdentifier(userInfo: feedIdentifierUserInfo) else {
|
2019-11-14 22:06:32 +01:00
|
|
|
|
return
|
2019-08-28 00:43:15 +02:00
|
|
|
|
}
|
2019-11-14 22:06:32 +01:00
|
|
|
|
|
2024-02-27 06:47:24 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2019-11-28 19:40:33 +01:00
|
|
|
|
|
2024-02-27 06:47:24 +01:00
|
|
|
|
switch sidebarItemID {
|
2019-11-14 22:06:32 +01:00
|
|
|
|
|
2019-11-23 19:30:18 +01:00
|
|
|
|
case .smartFeed:
|
2024-02-27 06:47:24 +01:00
|
|
|
|
guard let smartFeed = SmartFeedsController.shared.find(by: sidebarItemID) else { return }
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(SmartFeedsController.shared)
|
2021-11-18 19:25:43 +01:00
|
|
|
|
rebuildBackingStores(initialLoad: true, completion: {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.treeControllerDelegate.resetFilterExceptions()
|
|
|
|
|
if let indexPath = self.indexPathFor(smartFeed) {
|
|
|
|
|
self.selectFeed(indexPath: indexPath) {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.sidebarViewController.focus()
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
2019-12-09 02:25:22 +01:00
|
|
|
|
}
|
2020-10-25 18:34:02 +01:00
|
|
|
|
})
|
2019-11-14 22:06:32 +01:00
|
|
|
|
|
|
|
|
|
case .script:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
case .folder(let accountID, let folderName):
|
2020-02-18 02:40:40 +01:00
|
|
|
|
guard let accountNode = self.findAccountNode(accountID: accountID),
|
|
|
|
|
let account = accountNode.representedObject as? Account else {
|
2019-11-14 22:06:32 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2020-02-18 18:30:58 +01:00
|
|
|
|
markExpanded(account)
|
2020-02-18 02:40:40 +01:00
|
|
|
|
|
2021-11-18 19:25:43 +01:00
|
|
|
|
rebuildBackingStores(initialLoad: true, completion: {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
self.treeControllerDelegate.resetFilterExceptions()
|
|
|
|
|
|
|
|
|
|
if let folderNode = self.findFolderNode(folderName: folderName, beginningAt: accountNode), let indexPath = self.indexPathFor(folderNode) {
|
|
|
|
|
self.selectFeed(indexPath: indexPath) {
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.sidebarViewController.focus()
|
2020-02-18 02:40:40 +01:00
|
|
|
|
}
|
2019-12-09 02:25:22 +01:00
|
|
|
|
}
|
2020-10-25 18:34:02 +01:00
|
|
|
|
})
|
2019-11-14 22:06:32 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
case .feed(let accountID, let feedID):
|
2020-02-04 20:00:55 +01:00
|
|
|
|
guard let accountNode = findAccountNode(accountID: accountID),
|
|
|
|
|
let account = accountNode.representedObject as? Account,
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let feed = account.existingFeed(withFeedID: feedID) else {
|
2019-11-14 22:06:32 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-04 20:00:55 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
self.discloseFeed(feed, initialLoad: true) {
|
|
|
|
|
self.sidebarViewController.focus()
|
2020-02-04 20:00:55 +01:00
|
|
|
|
}
|
2019-08-28 00:43:15 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-18 02:40:40 +01:00
|
|
|
|
func handleReadArticle(_ userInfo: [AnyHashable : Any]?) {
|
2019-11-16 01:26:52 +01:00
|
|
|
|
guard let userInfo = userInfo else { return }
|
|
|
|
|
|
|
|
|
|
guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable : Any],
|
2021-09-13 08:11:23 +02:00
|
|
|
|
let accountID = articlePathUserInfo[ArticlePathKey.accountID] as? String,
|
|
|
|
|
let accountName = articlePathUserInfo[ArticlePathKey.accountName] as? String,
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let feedID = articlePathUserInfo[ArticlePathKey.feedID] as? String,
|
2021-09-13 08:11:23 +02:00
|
|
|
|
let articleID = articlePathUserInfo[ArticlePathKey.articleID] as? String,
|
|
|
|
|
let accountNode = findAccountNode(accountID: accountID, accountName: accountName),
|
|
|
|
|
let account = accountNode.representedObject as? Account else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-29 21:31:15 +01:00
|
|
|
|
|
|
|
|
|
exceptionArticleFetcher = SingleArticleFetcher(account: account, articleID: articleID)
|
2019-11-14 22:06:32 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if restoreFeedSelection(userInfo, accountID: accountID, feedID: feedID, articleID: articleID) {
|
2019-11-16 01:26:52 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
guard let feed = account.existingFeed(withFeedID: feedID) else {
|
2020-02-18 02:40:40 +01:00
|
|
|
|
return
|
2020-02-04 20:00:55 +01:00
|
|
|
|
}
|
2019-11-28 19:40:33 +01:00
|
|
|
|
|
2024-03-04 07:51:53 +01:00
|
|
|
|
discloseFeed(feed) {
|
2019-11-16 01:26:52 +01:00
|
|
|
|
self.selectArticleInCurrentFeed(articleID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
func restoreFeedSelection(_ userInfo: [AnyHashable : Any], accountID: String, feedID: String, articleID: String) -> Bool {
|
2019-11-27 18:43:36 +01:00
|
|
|
|
guard let feedIdentifierUserInfo = userInfo[UserInfoKey.feedIdentifier] as? [AnyHashable : AnyHashable],
|
2024-02-27 06:47:24 +01:00
|
|
|
|
let sidebarItemID = SidebarItemIdentifier(userInfo: feedIdentifierUserInfo),
|
2021-09-13 08:11:23 +02:00
|
|
|
|
let isShowingExtractedArticle = userInfo[UserInfoKey.isShowingExtractedArticle] as? Bool,
|
|
|
|
|
let articleWindowScrollY = userInfo[UserInfoKey.articleWindowScrollY] as? Int else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-11-16 01:26:52 +01:00
|
|
|
|
|
2024-02-27 06:47:24 +01:00
|
|
|
|
switch sidebarItemID {
|
2019-11-19 18:16:43 +01:00
|
|
|
|
|
2019-11-16 01:26:52 +01:00
|
|
|
|
case .script:
|
|
|
|
|
return false
|
2021-09-15 12:22:18 +02:00
|
|
|
|
|
|
|
|
|
case .smartFeed, .folder:
|
2024-02-27 06:47:24 +01:00
|
|
|
|
let found = selectFeedAndArticle(sidebarItemID: sidebarItemID, articleID: articleID, isShowingExtractedArticle: isShowingExtractedArticle, articleWindowScrollY: articleWindowScrollY)
|
2019-11-28 19:40:33 +01:00
|
|
|
|
if found {
|
2024-02-27 06:47:24 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2019-11-16 01:26:52 +01:00
|
|
|
|
}
|
2019-11-28 19:40:33 +01:00
|
|
|
|
return found
|
2019-11-16 01:26:52 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
case .feed:
|
2024-02-27 06:47:24 +01:00
|
|
|
|
let found = selectFeedAndArticle(sidebarItemID: sidebarItemID, articleID: articleID, isShowingExtractedArticle: isShowingExtractedArticle, articleWindowScrollY: articleWindowScrollY)
|
2019-11-28 19:40:33 +01:00
|
|
|
|
if found {
|
2024-02-27 06:47:24 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(sidebarItemID)
|
2024-03-04 08:01:00 +01:00
|
|
|
|
if let sidebarItemNode = nodeFor(sidebarItemID: sidebarItemID), let folder = sidebarItemNode.parent?.representedObject as? Folder, let folderFeedID = folder.sidebarItemID {
|
2020-02-04 20:00:55 +01:00
|
|
|
|
treeControllerDelegate.addFilterException(folderFeedID)
|
|
|
|
|
}
|
2019-11-28 19:40:33 +01:00
|
|
|
|
}
|
|
|
|
|
return found
|
|
|
|
|
|
2019-08-25 02:31:29 +02:00
|
|
|
|
}
|
2019-11-16 01:26:52 +01:00
|
|
|
|
|
2019-08-25 02:31:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 22:06:32 +01:00
|
|
|
|
func findAccountNode(accountID: String, accountName: String? = nil) -> Node? {
|
2019-08-25 02:31:29 +02:00
|
|
|
|
if let node = treeController.rootNode.descendantNode(where: { ($0.representedObject as? Account)?.accountID == accountID }) {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 22:06:32 +01:00
|
|
|
|
if let accountName = accountName, let node = treeController.rootNode.descendantNode(where: { ($0.representedObject as? Account)?.nameForDisplay == accountName }) {
|
2019-08-25 02:31:29 +02:00
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 22:06:32 +01:00
|
|
|
|
func findFolderNode(folderName: String, beginningAt startingNode: Node) -> Node? {
|
2019-08-28 00:43:15 +02:00
|
|
|
|
if let node = startingNode.descendantNode(where: { ($0.representedObject as? Folder)?.nameForDisplay == folderName }) {
|
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
func findFeedNode(feedID: String, beginningAt startingNode: Node) -> Node? {
|
|
|
|
|
if let node = startingNode.descendantNode(where: { ($0.representedObject as? Feed)?.feedID == feedID }) {
|
2019-08-25 02:31:29 +02:00
|
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-27 06:47:24 +01:00
|
|
|
|
func selectFeedAndArticle(sidebarItemID: SidebarItemIdentifier, articleID: String, isShowingExtractedArticle: Bool, articleWindowScrollY: Int) -> Bool {
|
2024-03-04 08:01:00 +01:00
|
|
|
|
guard let sidebarItemNode = nodeFor(sidebarItemID: sidebarItemID), let indexPath = indexPathFor(sidebarItemNode) else { return false }
|
|
|
|
|
|
|
|
|
|
selectFeed(indexPath: indexPath) {
|
2021-10-21 20:08:18 +02:00
|
|
|
|
self.selectArticleInCurrentFeed(articleID, isShowingExtractedArticle: isShowingExtractedArticle, articleWindowScrollY: articleWindowScrollY)
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
2021-09-15 12:05:58 +02:00
|
|
|
|
|
|
|
|
|
return true
|
2019-11-19 18:16:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:35:12 +02:00
|
|
|
|
}
|