2020-06-30 11:03:33 -05:00
|
|
|
|
//
|
|
|
|
|
// TimelineModel.swift
|
|
|
|
|
// NetNewsWire
|
|
|
|
|
//
|
|
|
|
|
// Created by Maurice Parker on 6/30/20.
|
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2020-07-11 18:22:47 -05:00
|
|
|
|
import Combine
|
2020-06-30 11:03:33 -05:00
|
|
|
|
import RSCore
|
|
|
|
|
import Account
|
2020-07-01 12:30:55 -05:00
|
|
|
|
import Articles
|
2020-06-30 11:03:33 -05:00
|
|
|
|
|
|
|
|
|
protocol TimelineModelDelegate: class {
|
|
|
|
|
func timelineRequestedWebFeedSelection(_: TimelineModel, webFeed: WebFeed)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 18:22:47 -05:00
|
|
|
|
class TimelineModel: ObservableObject, UndoableCommandRunner {
|
2020-06-30 11:03:33 -05:00
|
|
|
|
|
|
|
|
|
weak var delegate: TimelineModelDelegate?
|
|
|
|
|
|
2020-07-11 12:47:13 -05:00
|
|
|
|
@Published var nameForDisplay = ""
|
2020-06-30 11:03:33 -05:00
|
|
|
|
@Published var timelineItems = [TimelineItem]()
|
2020-07-12 14:43:52 -05:00
|
|
|
|
@Published var selectedArticleIDs = Set<String>() // Don't use directly. Use selectedArticles
|
|
|
|
|
@Published var selectedArticleID: String? = .none // Don't use directly. Use selectedArticles
|
2020-07-11 18:22:47 -05:00
|
|
|
|
@Published var selectedArticles = [Article]()
|
2020-07-12 16:48:39 -05:00
|
|
|
|
@Published var readFilterEnabledTable = [FeedIdentifier: Bool]()
|
|
|
|
|
@Published var isReadFiltered: Bool? = nil
|
2020-07-12 10:52:42 -05:00
|
|
|
|
|
2020-07-11 18:22:47 -05:00
|
|
|
|
var undoManager: UndoManager?
|
|
|
|
|
var undoableCommands = [UndoableCommand]()
|
|
|
|
|
|
|
|
|
|
private var selectedArticleIDsCancellable: AnyCancellable?
|
|
|
|
|
private var selectedArticleIDCancellable: AnyCancellable?
|
2020-07-11 19:52:28 -05:00
|
|
|
|
private var selectedArticlesCancellable: AnyCancellable?
|
2020-07-11 18:22:47 -05:00
|
|
|
|
|
2020-07-12 16:48:39 -05:00
|
|
|
|
private var feeds = [Feed]()
|
2020-07-01 12:30:55 -05:00
|
|
|
|
private var fetchSerialNumber = 0
|
|
|
|
|
private let fetchRequestQueue = FetchRequestQueue()
|
|
|
|
|
private var exceptionArticleFetcher: ArticleFetcher?
|
|
|
|
|
|
2020-07-06 21:14:05 -05:00
|
|
|
|
private var articles = [Article]() {
|
|
|
|
|
didSet {
|
|
|
|
|
articleDictionaryNeedsUpdate = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-01 12:30:55 -05:00
|
|
|
|
|
2020-07-06 21:14:05 -05:00
|
|
|
|
private var articleDictionaryNeedsUpdate = true
|
|
|
|
|
private var _idToArticleDictionary = [String: Article]()
|
2020-07-09 20:10:52 -05:00
|
|
|
|
private var idToArticleDictionary: [String: Article] {
|
2020-07-06 21:14:05 -05:00
|
|
|
|
if articleDictionaryNeedsUpdate {
|
|
|
|
|
rebuildArticleDictionaries()
|
|
|
|
|
}
|
|
|
|
|
return _idToArticleDictionary
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 19:43:25 -05:00
|
|
|
|
private var sortDirection: Bool {
|
|
|
|
|
AppDefaults.shared.timelineSortDirection
|
2020-07-01 12:30:55 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 19:43:25 -05:00
|
|
|
|
private var groupByFeed: Bool {
|
|
|
|
|
AppDefaults.shared.timelineGroupByFeed
|
2020-07-01 12:30:55 -05:00
|
|
|
|
}
|
2020-07-01 11:13:11 -05:00
|
|
|
|
|
2020-06-30 11:03:33 -05:00
|
|
|
|
init() {
|
2020-07-09 20:10:52 -05:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(statusesDidChange(_:)), name: .StatusesDidChange, object: nil)
|
2020-07-12 19:43:25 -05:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil)
|
|
|
|
|
|
2020-07-11 18:22:47 -05:00
|
|
|
|
// TODO: This should be rewritten to use Combine correctly
|
|
|
|
|
selectedArticleIDsCancellable = $selectedArticleIDs.sink { [weak self] articleIDs in
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
self.selectedArticles = articleIDs.compactMap { self.idToArticleDictionary[$0] }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: This should be rewritten to use Combine correctly
|
|
|
|
|
selectedArticleIDCancellable = $selectedArticleID.sink { [weak self] articleID in
|
|
|
|
|
guard let self = self else { return }
|
|
|
|
|
if let articleID = articleID, let article = self.idToArticleDictionary[articleID] {
|
|
|
|
|
self.selectedArticles = [article]
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-11 19:52:28 -05:00
|
|
|
|
|
|
|
|
|
// TODO: This should be rewritten to use Combine correctly
|
|
|
|
|
selectedArticlesCancellable = $selectedArticles.sink { articles in
|
|
|
|
|
if articles.count == 1 {
|
|
|
|
|
let article = articles.first!
|
|
|
|
|
if !article.status.read {
|
|
|
|
|
markArticles(Set([article]), statusKey: .read, flag: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-12 14:43:52 -05:00
|
|
|
|
|
2020-06-30 11:03:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: API
|
|
|
|
|
|
2020-07-12 14:43:52 -05:00
|
|
|
|
func fetchArticles(feeds: [Feed]) {
|
2020-07-12 16:48:39 -05:00
|
|
|
|
self.feeds = feeds
|
|
|
|
|
|
2020-07-11 12:47:13 -05:00
|
|
|
|
if feeds.count == 1 {
|
|
|
|
|
nameForDisplay = feeds.first!.nameForDisplay
|
|
|
|
|
} else {
|
|
|
|
|
nameForDisplay = NSLocalizedString("Multiple", comment: "Multiple Feeds")
|
|
|
|
|
}
|
2020-07-12 16:48:39 -05:00
|
|
|
|
|
|
|
|
|
resetReadFilter()
|
2020-07-12 17:30:35 -05:00
|
|
|
|
fetchAndReplaceArticlesAsync()
|
2020-06-30 11:03:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 16:48:39 -05:00
|
|
|
|
func toggleReadFilter() {
|
|
|
|
|
guard let filter = isReadFiltered, let feedID = feeds.first?.feedID else { return }
|
|
|
|
|
readFilterEnabledTable[feedID] = !filter
|
|
|
|
|
isReadFiltered = !filter
|
2020-07-12 17:30:35 -05:00
|
|
|
|
rebuildTimelineItems()
|
2020-07-12 16:48:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 19:52:28 -05:00
|
|
|
|
func toggleReadStatusForSelectedArticles() {
|
|
|
|
|
guard !selectedArticles.isEmpty else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if selectedArticles.anyArticleIsUnread() {
|
|
|
|
|
markSelectedArticlesAsRead()
|
|
|
|
|
} else {
|
|
|
|
|
markSelectedArticlesAsUnread()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markSelectedArticlesAsRead() {
|
|
|
|
|
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: true, undoManager: undoManager) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runCommand(markReadCommand)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markSelectedArticlesAsUnread() {
|
|
|
|
|
guard let undoManager = undoManager, let markUnreadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingRead: false, undoManager: undoManager) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runCommand(markUnreadCommand)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toggleStarredStatusForSelectedArticles() {
|
|
|
|
|
guard !selectedArticles.isEmpty else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if selectedArticles.anyArticleIsUnstarred() {
|
|
|
|
|
markSelectedArticlesAsStarred()
|
|
|
|
|
} else {
|
|
|
|
|
markSelectedArticlesAsUnstarred()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markSelectedArticlesAsStarred() {
|
|
|
|
|
guard let undoManager = undoManager, let markReadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingStarred: true, undoManager: undoManager) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runCommand(markReadCommand)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func markSelectedArticlesAsUnstarred() {
|
|
|
|
|
guard let undoManager = undoManager, let markUnreadCommand = MarkStatusCommand(initialArticles: selectedArticles, markingStarred: false, undoManager: undoManager) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
runCommand(markUnreadCommand)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 21:14:05 -05:00
|
|
|
|
func articleFor(_ articleID: String) -> Article? {
|
2020-07-09 20:10:52 -05:00
|
|
|
|
return idToArticleDictionary[articleID]
|
2020-07-06 21:14:05 -05: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]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func selectArticle(_ article: Article) {
|
|
|
|
|
// TODO: Implement me!
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 11:03:33 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Private
|
2020-07-01 12:30:55 -05:00
|
|
|
|
|
2020-06-30 11:03:33 -05:00
|
|
|
|
private extension TimelineModel {
|
|
|
|
|
|
2020-07-09 20:10:52 -05:00
|
|
|
|
// MARK: Notifications
|
|
|
|
|
|
|
|
|
|
@objc func statusesDidChange(_ note: Notification) {
|
|
|
|
|
guard let articleIDs = note.userInfo?[Account.UserInfoKey.articleIDs] as? Set<String> else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
for i in 0..<timelineItems.count {
|
|
|
|
|
if articleIDs.contains(timelineItems[i].article.articleID) {
|
|
|
|
|
timelineItems[i].updateStatus()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 19:43:25 -05:00
|
|
|
|
@objc func userDefaultsDidChange(_ note: Notification) {
|
|
|
|
|
performBlockAndRestoreSelection {
|
|
|
|
|
articles = articles.sortedByDate(sortDirection ? .orderedDescending : .orderedAscending, groupByFeed: groupByFeed)
|
|
|
|
|
rebuildTimelineItems()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 19:52:28 -05:00
|
|
|
|
// MARK: Timeline Management
|
2020-07-09 20:10:52 -05:00
|
|
|
|
|
2020-07-12 16:48:39 -05:00
|
|
|
|
func resetReadFilter() {
|
|
|
|
|
guard feeds.count == 1, let timelineFeed = feeds.first else {
|
|
|
|
|
isReadFiltered = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
guard timelineFeed.defaultReadFilterType != .alwaysRead else {
|
|
|
|
|
isReadFiltered = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let feedID = timelineFeed.feedID, let readFilterEnabled = readFilterEnabledTable[feedID] {
|
|
|
|
|
isReadFiltered = readFilterEnabled
|
|
|
|
|
} else {
|
|
|
|
|
isReadFiltered = timelineFeed.defaultReadFilterType == .read
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 12:30:55 -05:00
|
|
|
|
func performBlockAndRestoreSelection(_ block: (() -> Void)) {
|
|
|
|
|
// let savedSelection = selectedArticleIDs()
|
|
|
|
|
block()
|
|
|
|
|
// restoreSelection(savedSelection)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 21:14:05 -05:00
|
|
|
|
func rebuildArticleDictionaries() {
|
|
|
|
|
var idDictionary = [String: Article]()
|
|
|
|
|
articles.forEach { article in
|
|
|
|
|
idDictionary[article.articleID] = article
|
|
|
|
|
}
|
|
|
|
|
_idToArticleDictionary = idDictionary
|
|
|
|
|
articleDictionaryNeedsUpdate = false
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-01 12:30:55 -05:00
|
|
|
|
// MARK: Article Fetching
|
|
|
|
|
|
2020-07-12 17:30:35 -05:00
|
|
|
|
func fetchAndReplaceArticlesAsync() {
|
2020-07-01 12:30:55 -05:00
|
|
|
|
var fetchers = feeds as [ArticleFetcher]
|
|
|
|
|
if let fetcher = exceptionArticleFetcher {
|
|
|
|
|
fetchers.append(fetcher)
|
|
|
|
|
exceptionArticleFetcher = nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchUnsortedArticlesAsync(for: fetchers) { [weak self] (articles) in
|
|
|
|
|
self?.replaceArticles(with: articles)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cancelPendingAsyncFetches() {
|
|
|
|
|
fetchSerialNumber += 1
|
|
|
|
|
fetchRequestQueue.cancelAllRequests()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func fetchUnsortedArticlesAsync(for representedObjects: [Any], completion: @escaping ArticleSetBlock) {
|
|
|
|
|
// 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()
|
2020-07-12 16:48:39 -05:00
|
|
|
|
|
2020-07-12 17:30:35 -05:00
|
|
|
|
// Right now we are pulling all the records and filtering them in the UI. This is because the async
|
|
|
|
|
// change of the timeline times doesn't trigger an animation because it isn't in a withAnimation block.
|
|
|
|
|
// Ideally this would be done in the database tier with a query, but if you look, we always pull everything
|
|
|
|
|
// from SQLite and filter it programmatically at that level currently. So no big deal.
|
|
|
|
|
// We should change this as soon as we figure out how to trigger animations on Lists with async tasks.
|
|
|
|
|
let fetchOperation = FetchRequestOperation(id: fetchSerialNumber, readFilter: false, representedObjects: representedObjects) { [weak self] (articles, operation) in
|
2020-07-01 12:30:55 -05:00
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
|
guard !operation.isCanceled, let strongSelf = self, operation.id == strongSelf.fetchSerialNumber else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
completion(articles)
|
|
|
|
|
}
|
|
|
|
|
fetchRequestQueue.add(fetchOperation)
|
|
|
|
|
}
|
2020-06-30 11:03:33 -05:00
|
|
|
|
|
2020-07-01 12:30:55 -05:00
|
|
|
|
func replaceArticles(with unsortedArticles: Set<Article>) {
|
2020-07-02 05:46:56 -05:00
|
|
|
|
articles = Array(unsortedArticles).sortedByDate(sortDirection ? .orderedDescending : .orderedAscending, groupByFeed: groupByFeed)
|
2020-07-12 17:30:35 -05:00
|
|
|
|
rebuildTimelineItems()
|
2020-07-01 12:30:55 -05:00
|
|
|
|
// TODO: Update unread counts and other item done in didSet on AppKit
|
|
|
|
|
}
|
2020-07-02 17:36:50 -05:00
|
|
|
|
|
2020-07-12 17:30:35 -05:00
|
|
|
|
func rebuildTimelineItems() {
|
2020-07-12 16:48:39 -05:00
|
|
|
|
let filtered = isReadFiltered ?? false
|
2020-07-12 14:43:52 -05:00
|
|
|
|
let selectedArticleIDs = selectedArticles.map { $0.articleID }
|
|
|
|
|
|
|
|
|
|
timelineItems = articles.compactMap { article in
|
2020-07-12 16:48:39 -05:00
|
|
|
|
if filtered && article.status.read && !selectedArticleIDs.contains(article.articleID) {
|
2020-07-12 14:43:52 -05:00
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
return TimelineItem(article: article)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-30 11:03:33 -05:00
|
|
|
|
}
|