2017-07-03 19:29:44 +02:00
|
|
|
|
//
|
|
|
|
|
// Account.swift
|
|
|
|
|
// DataModel
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2017-09-17 21:08:50 +02:00
|
|
|
|
import RSParser
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import ArticlesDatabase
|
2017-10-08 02:20:19 +02:00
|
|
|
|
import RSWeb
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-10-07 23:40:14 +02:00
|
|
|
|
public extension Notification.Name {
|
2019-02-12 16:04:18 +01:00
|
|
|
|
static let AccountRefreshDidBegin = Notification.Name(rawValue: "AccountRefreshDidBegin")
|
|
|
|
|
static let AccountRefreshDidFinish = Notification.Name(rawValue: "AccountRefreshDidFinish")
|
|
|
|
|
static let AccountRefreshProgressDidChange = Notification.Name(rawValue: "AccountRefreshProgressDidChange")
|
|
|
|
|
static let AccountDidDownloadArticles = Notification.Name(rawValue: "AccountDidDownloadArticles")
|
2017-10-09 06:06:25 +02:00
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
|
static let StatusesDidChange = Notification.Name(rawValue: "StatusesDidChange")
|
2017-10-07 23:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
|
public enum AccountType: Int {
|
|
|
|
|
// Raw values should not change since they’re stored on disk.
|
|
|
|
|
case onMyMac = 1
|
|
|
|
|
case feedly = 16
|
|
|
|
|
case feedbin = 17
|
|
|
|
|
case feedWrangler = 18
|
|
|
|
|
case newsBlur = 19
|
|
|
|
|
// TODO: more
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 22:23:12 +02:00
|
|
|
|
public final class Account: DisplayNameProvider, UnreadCountProvider, Container, Hashable {
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-11-05 06:51:14 +01:00
|
|
|
|
public struct UserInfoKey {
|
2017-10-09 06:06:25 +02:00
|
|
|
|
public static let newArticles = "newArticles" // AccountDidDownloadArticles
|
|
|
|
|
public static let updatedArticles = "updatedArticles" // AccountDidDownloadArticles
|
2017-10-09 07:25:33 +02:00
|
|
|
|
public static let statuses = "statuses" // StatusesDidChange
|
|
|
|
|
public static let articles = "articles" // StatusesDidChange
|
2018-01-18 02:28:09 +01:00
|
|
|
|
public static let feeds = "feeds" // AccountDidDownloadArticles, StatusesDidChange
|
2017-10-08 10:54:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 21:08:50 +02:00
|
|
|
|
public let accountID: String
|
2017-07-03 19:29:44 +02:00
|
|
|
|
public let type: AccountType
|
2019-03-28 06:10:14 +01:00
|
|
|
|
public var nameForDisplay: String {
|
|
|
|
|
guard let name = name, !name.isEmpty else {
|
|
|
|
|
return defaultName
|
|
|
|
|
}
|
|
|
|
|
return name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public var name: String? {
|
|
|
|
|
get {
|
|
|
|
|
return settings.name
|
|
|
|
|
}
|
|
|
|
|
set {
|
|
|
|
|
if newValue != settings.name {
|
|
|
|
|
settings.name = newValue
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public let defaultName: String
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
public var topLevelFeeds = Set<Feed>()
|
|
|
|
|
public var folders: Set<Folder>? = Set<Folder>()
|
|
|
|
|
private var feedDictionaryNeedsUpdate = true
|
|
|
|
|
private var _idToFeedDictionary = [String: Feed]()
|
|
|
|
|
var idToFeedDictionary: [String: Feed] {
|
|
|
|
|
if feedDictionaryNeedsUpdate {
|
|
|
|
|
rebuildFeedDictionaries()
|
|
|
|
|
}
|
|
|
|
|
return _idToFeedDictionary
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private var fetchingAllUnreadCounts = false
|
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
|
let dataFolder: String
|
2018-07-24 03:29:08 +02:00
|
|
|
|
let database: ArticlesDatabase
|
2017-10-08 02:43:10 +02:00
|
|
|
|
let delegate: AccountDelegate
|
2017-07-03 19:29:44 +02:00
|
|
|
|
var username: String?
|
2018-02-18 00:38:54 +01:00
|
|
|
|
static let saveQueue = CoalescingQueue(name: "Account Save Queue", interval: 1.0)
|
2017-10-08 03:15:42 +02:00
|
|
|
|
|
2018-09-15 07:06:03 +02:00
|
|
|
|
private var unreadCounts = [String: Int]() // [feedID: Int]
|
2018-09-14 22:25:38 +02:00
|
|
|
|
private let opmlFilePath: String
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
private var _flattenedFeeds = Set<Feed>()
|
|
|
|
|
private var flattenedFeedsNeedUpdate = true
|
|
|
|
|
|
2019-03-21 06:10:22 +01:00
|
|
|
|
private let settingsPath: String
|
|
|
|
|
private var settings = AccountSettings()
|
2019-03-28 06:10:14 +01:00
|
|
|
|
private var settingsDirty = false {
|
|
|
|
|
didSet {
|
|
|
|
|
queueSaveSettingsIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-21 06:10:22 +01:00
|
|
|
|
|
2019-03-14 07:41:43 +01:00
|
|
|
|
private let feedMetadataPath: String
|
|
|
|
|
private typealias FeedMetadataDictionary = [String: FeedMetadata]
|
|
|
|
|
private var feedMetadata = FeedMetadataDictionary()
|
|
|
|
|
private var feedMetadataDirty = false {
|
|
|
|
|
didSet {
|
|
|
|
|
queueSaveMetadataIfNeeded()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 05:02:24 +02:00
|
|
|
|
private var startingUp = true
|
|
|
|
|
|
2017-10-08 06:41:21 +02:00
|
|
|
|
public var dirty = false {
|
2017-10-08 03:15:42 +02:00
|
|
|
|
didSet {
|
2019-02-08 06:29:05 +01:00
|
|
|
|
queueSaveToDiskIfNeeded()
|
2017-10-08 03:15:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-07 23:40:14 +02:00
|
|
|
|
|
2017-10-10 22:23:12 +02:00
|
|
|
|
public var unreadCount = 0 {
|
|
|
|
|
didSet {
|
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-07 23:40:14 +02:00
|
|
|
|
var refreshInProgress = false {
|
|
|
|
|
didSet {
|
|
|
|
|
if refreshInProgress != oldValue {
|
|
|
|
|
if refreshInProgress {
|
|
|
|
|
NotificationCenter.default.post(name: .AccountRefreshDidBegin, object: self)
|
|
|
|
|
}
|
|
|
|
|
else {
|
2017-10-08 03:31:34 +02:00
|
|
|
|
NotificationCenter.default.post(name: .AccountRefreshDidFinish, object: self)
|
2018-02-18 03:02:40 +01:00
|
|
|
|
queueSaveToDiskIfNeeded()
|
2017-10-07 23:40:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-28 22:16:47 +02:00
|
|
|
|
|
2017-10-08 02:20:19 +02:00
|
|
|
|
var refreshProgress: DownloadProgress {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return delegate.refreshProgress
|
2017-10-08 02:20:19 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2017-09-28 22:16:47 +02:00
|
|
|
|
var supportsSubFolders: Bool {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
return delegate.supportsSubFolders
|
2017-09-28 22:16:47 +02:00
|
|
|
|
}
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2019-03-15 21:17:37 +01:00
|
|
|
|
init?(dataFolder: String, type: AccountType, accountID: String) {
|
2018-02-14 22:14:25 +01:00
|
|
|
|
|
2017-10-07 23:40:14 +02:00
|
|
|
|
// TODO: support various syncing systems.
|
2017-10-08 02:43:10 +02:00
|
|
|
|
precondition(type == .onMyMac)
|
|
|
|
|
self.delegate = LocalAccountDelegate()
|
2017-09-18 02:03:58 +02:00
|
|
|
|
|
|
|
|
|
self.accountID = accountID
|
|
|
|
|
self.type = type
|
|
|
|
|
self.dataFolder = dataFolder
|
2017-12-20 02:48:30 +01:00
|
|
|
|
|
2018-09-14 22:25:38 +02:00
|
|
|
|
self.opmlFilePath = (dataFolder as NSString).appendingPathComponent("Subscriptions.opml")
|
|
|
|
|
|
2017-09-18 02:03:58 +02:00
|
|
|
|
let databaseFilePath = (dataFolder as NSString).appendingPathComponent("DB.sqlite3")
|
2018-07-24 03:29:08 +02:00
|
|
|
|
self.database = ArticlesDatabase(databaseFilePath: databaseFilePath, accountID: accountID)
|
2017-09-27 22:29:05 +02:00
|
|
|
|
|
2019-03-14 07:41:43 +01:00
|
|
|
|
self.feedMetadataPath = (dataFolder as NSString).appendingPathComponent("FeedMetadata.plist")
|
2019-03-21 06:10:22 +01:00
|
|
|
|
self.settingsPath = (dataFolder as NSString).appendingPathComponent("Settings.plist")
|
2018-09-13 07:28:21 +02:00
|
|
|
|
|
2019-03-28 06:10:14 +01:00
|
|
|
|
switch type {
|
|
|
|
|
case .onMyMac:
|
|
|
|
|
defaultName = NSLocalizedString("On My Mac", comment: "Account name")
|
|
|
|
|
case .feedly:
|
|
|
|
|
defaultName = "Feedly"
|
|
|
|
|
case .feedbin:
|
|
|
|
|
defaultName = "Feedbin"
|
|
|
|
|
case .feedWrangler:
|
|
|
|
|
defaultName = "FeedWrangler"
|
|
|
|
|
case .newsBlur:
|
|
|
|
|
defaultName = "NewsBlur"
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 02:43:10 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(downloadProgressDidChange(_:)), name: .DownloadProgressDidChange, object: nil)
|
2017-10-13 06:02:27 +02:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2017-11-15 22:26:10 +01:00
|
|
|
|
|
2018-01-24 06:49:33 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidPerform(_:)), name: .BatchUpdateDidPerform, object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(displayNameDidChange(_:)), name: .DisplayNameDidChange, object: nil)
|
2018-02-25 00:54:32 +01:00
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(childrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
|
2017-11-25 22:48:14 +01:00
|
|
|
|
|
2017-09-27 22:29:05 +02:00
|
|
|
|
pullObjectsFromDisk()
|
2017-10-19 03:37:45 +02:00
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
2017-12-03 20:57:53 +01:00
|
|
|
|
self.fetchAllUnreadCounts()
|
2017-10-19 03:37:45 +02:00
|
|
|
|
}
|
2017-12-20 02:48:30 +01:00
|
|
|
|
|
|
|
|
|
self.delegate.accountDidInitialize(self)
|
2018-09-17 05:02:24 +02:00
|
|
|
|
startingUp = false
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 20:32:58 +02:00
|
|
|
|
// MARK: - API
|
|
|
|
|
|
|
|
|
|
public func refreshAll() {
|
|
|
|
|
|
2017-10-08 02:43:10 +02:00
|
|
|
|
delegate.refreshAll(for: self)
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
2017-09-17 00:30:26 +02:00
|
|
|
|
|
2017-12-03 02:47:08 +01:00
|
|
|
|
public func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: @escaping RSVoidCompletionBlock) {
|
2017-10-08 10:54:37 +02:00
|
|
|
|
|
2017-11-25 22:48:14 +01:00
|
|
|
|
feed.takeSettings(from: parsedFeed)
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
database.update(feedID: feed.feedID, parsedFeed: parsedFeed) { (newArticles, updatedArticles) in
|
2017-10-08 10:54:37 +02:00
|
|
|
|
|
|
|
|
|
var userInfo = [String: Any]()
|
|
|
|
|
if let newArticles = newArticles, !newArticles.isEmpty {
|
|
|
|
|
self.updateUnreadCounts(for: Set([feed]))
|
|
|
|
|
userInfo[UserInfoKey.newArticles] = newArticles
|
|
|
|
|
}
|
|
|
|
|
if let updatedArticles = updatedArticles, !updatedArticles.isEmpty {
|
|
|
|
|
userInfo[UserInfoKey.updatedArticles] = updatedArticles
|
|
|
|
|
}
|
2018-01-18 02:28:09 +01:00
|
|
|
|
userInfo[UserInfoKey.feeds] = Set([feed])
|
2017-09-17 21:08:50 +02:00
|
|
|
|
|
2017-10-08 10:54:37 +02:00
|
|
|
|
completion()
|
|
|
|
|
|
2018-01-18 02:28:09 +01:00
|
|
|
|
NotificationCenter.default.post(name: .AccountDidDownloadArticles, object: self, userInfo: userInfo)
|
2017-10-08 10:54:37 +02:00
|
|
|
|
}
|
2017-09-17 21:08:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 19:14:10 +01:00
|
|
|
|
public func markArticles(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<Article>? {
|
|
|
|
|
|
|
|
|
|
// Returns set of Articles whose statuses did change.
|
|
|
|
|
|
2017-10-10 06:54:08 +02:00
|
|
|
|
guard let updatedStatuses = database.mark(articles, statusKey: statusKey, flag: flag) else {
|
2017-10-29 19:14:10 +01:00
|
|
|
|
return nil
|
2017-10-09 06:06:25 +02:00
|
|
|
|
}
|
2017-10-10 06:54:08 +02:00
|
|
|
|
|
|
|
|
|
let updatedArticleIDs = updatedStatuses.articleIDs()
|
|
|
|
|
let updatedArticles = Set(articles.filter{ updatedArticleIDs.contains($0.articleID) })
|
2017-10-10 22:23:12 +02:00
|
|
|
|
|
|
|
|
|
noteStatusesForArticlesDidChange(updatedArticles)
|
2017-10-29 19:14:10 +01:00
|
|
|
|
return updatedArticles
|
2017-09-18 01:30:45 +02:00
|
|
|
|
}
|
2017-10-19 04:46:35 +02:00
|
|
|
|
|
|
|
|
|
@discardableResult
|
2017-09-17 22:07:55 +02:00
|
|
|
|
public func ensureFolder(with name: String) -> Folder? {
|
2017-10-19 04:46:35 +02:00
|
|
|
|
|
|
|
|
|
// TODO: support subfolders, maybe, some day
|
|
|
|
|
|
|
|
|
|
if name.isEmpty {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let folder = existingFolder(with: name) {
|
|
|
|
|
return folder
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let folder = Folder(account: self, name: name)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
folders!.insert(folder)
|
2018-09-17 05:02:24 +02:00
|
|
|
|
structureDidChange()
|
2017-10-19 04:46:35 +02:00
|
|
|
|
|
2017-10-19 22:27:59 +02:00
|
|
|
|
postChildrenDidChangeNotification()
|
2017-10-19 04:46:35 +02:00
|
|
|
|
return folder
|
2017-09-17 22:07:55 +02:00
|
|
|
|
}
|
2017-09-25 22:31:36 +02:00
|
|
|
|
|
2017-11-05 03:03:47 +01:00
|
|
|
|
public func ensureFolder(withFolderNames folderNames: [String]) -> Folder? {
|
|
|
|
|
|
|
|
|
|
// TODO: support subfolders, maybe, some day.
|
|
|
|
|
// Since we don’t, just take the last name and make sure there’s a Folder.
|
|
|
|
|
|
|
|
|
|
guard let folderName = folderNames.last else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return ensureFolder(with: folderName)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-25 22:31:36 +02:00
|
|
|
|
public func canAddFeed(_ feed: Feed, to folder: Folder?) -> Bool {
|
|
|
|
|
|
|
|
|
|
// If folder is nil, then it should go at the top level.
|
|
|
|
|
// The same feed in multiple folders is allowed.
|
|
|
|
|
// But the same feed can’t appear twice in the same folder
|
|
|
|
|
// (or at the top level).
|
|
|
|
|
|
|
|
|
|
return true // TODO
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 06:48:32 +01:00
|
|
|
|
public func addFeed(_ feed: Feed, to folder: Folder?) {
|
2017-10-01 19:59:35 +02:00
|
|
|
|
if let folder = folder {
|
2019-02-06 06:48:32 +01:00
|
|
|
|
folder.addFeed(feed)
|
2017-10-01 19:59:35 +02:00
|
|
|
|
}
|
|
|
|
|
else {
|
2019-02-06 06:48:32 +01:00
|
|
|
|
addFeed(feed)
|
2017-10-01 19:59:35 +02:00
|
|
|
|
}
|
2017-09-25 22:31:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
public func addFeeds(_ feeds: Set<Feed>, to folder: Folder?) {
|
|
|
|
|
if let folder = folder {
|
|
|
|
|
folder.addFeeds(feeds)
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-02-06 06:48:32 +01:00
|
|
|
|
addFeeds(feeds)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 20:28:44 +02:00
|
|
|
|
public func createFeed(with name: String?, editedName: String?, url: String) -> Feed? {
|
2017-10-01 01:56:48 +02:00
|
|
|
|
|
|
|
|
|
// For syncing, this may need to be an async method with a callback,
|
|
|
|
|
// since it will likely need to call the server.
|
2019-03-16 20:08:31 +01:00
|
|
|
|
|
|
|
|
|
let feedMetadata = metadata(feedID: url)
|
|
|
|
|
let feed = Feed(account: self, url: url, feedID: url, metadata: feedMetadata)
|
2018-09-16 22:04:42 +02:00
|
|
|
|
if let name = name, feed.name == nil {
|
|
|
|
|
feed.name = name
|
|
|
|
|
}
|
|
|
|
|
if let editedName = editedName, feed.editedName == nil {
|
|
|
|
|
feed.editedName = editedName
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 01:56:48 +02:00
|
|
|
|
return feed
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 06:43:40 +02:00
|
|
|
|
public func canAddFolder(_ folder: Folder, to containingFolder: Folder?) -> Bool {
|
2017-09-25 22:31:36 +02:00
|
|
|
|
|
|
|
|
|
return false // TODO
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
|
@discardableResult
|
|
|
|
|
public func addFolder(_ folder: Folder, to parentFolder: Folder?) -> Bool {
|
2017-09-25 22:31:36 +02:00
|
|
|
|
|
2017-11-05 21:14:36 +01:00
|
|
|
|
// TODO: support subfolders, maybe, some day, if one of the sync systems
|
|
|
|
|
// supports subfolders. But, for now, parentFolder is ignored.
|
2018-09-17 02:54:42 +02:00
|
|
|
|
if folders!.contains(folder) {
|
2017-11-05 21:14:36 +01:00
|
|
|
|
return true
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
folders!.insert(folder)
|
2017-11-05 21:14:36 +01:00
|
|
|
|
postChildrenDidChangeNotification()
|
2018-09-17 02:54:42 +02:00
|
|
|
|
structureDidChange()
|
2017-11-05 21:14:36 +01:00
|
|
|
|
return true
|
2017-09-25 22:31:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-05 06:51:14 +01:00
|
|
|
|
public func importOPML(_ opmlDocument: RSOPMLDocument) {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
|
|
|
|
guard let children = opmlDocument.children else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-10-22 06:00:21 +02:00
|
|
|
|
importOPMLItems(children, parentFolder: nil)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
structureDidChange()
|
2017-12-03 21:54:51 +01:00
|
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.refreshAll()
|
|
|
|
|
}
|
2017-09-19 07:00:35 +02:00
|
|
|
|
}
|
2017-10-08 02:20:19 +02:00
|
|
|
|
|
2017-10-08 10:54:37 +02:00
|
|
|
|
public func updateUnreadCounts(for feeds: Set<Feed>) {
|
|
|
|
|
|
2017-10-19 06:53:45 +02:00
|
|
|
|
if feeds.isEmpty {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
database.fetchUnreadCounts(for: feeds.feedIDs()) { (unreadCountDictionary) in
|
2017-10-08 10:54:37 +02:00
|
|
|
|
|
|
|
|
|
for feed in feeds {
|
2018-07-28 21:16:14 +02:00
|
|
|
|
if let unreadCount = unreadCountDictionary[feed.feedID] {
|
2017-10-08 10:54:37 +02:00
|
|
|
|
feed.unreadCount = unreadCount
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 03:58:15 +02:00
|
|
|
|
public func fetchArticles(for feed: Feed) -> Set<Article> {
|
2017-12-26 20:27:55 +01:00
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
let articles = database.fetchArticles(for: feed.feedID)
|
2017-12-26 20:27:55 +01:00
|
|
|
|
validateUnreadCount(feed, articles)
|
|
|
|
|
return articles
|
2017-10-09 03:58:15 +02:00
|
|
|
|
}
|
2018-02-04 19:57:41 +01:00
|
|
|
|
|
|
|
|
|
public func fetchUnreadArticles(for feed: Feed) -> Set<Article> {
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
let articles = database.fetchUnreadArticles(for: Set([feed.feedID]))
|
2018-02-04 19:57:41 +01:00
|
|
|
|
validateUnreadCount(feed, articles)
|
|
|
|
|
return articles
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 22:00:16 +01:00
|
|
|
|
public func fetchUnreadArticles() -> Set<Article> {
|
|
|
|
|
|
|
|
|
|
return fetchUnreadArticles(forContainer: self)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 03:58:15 +02:00
|
|
|
|
public func fetchArticles(folder: Folder) -> Set<Article> {
|
2017-12-26 20:27:55 +01:00
|
|
|
|
|
2018-02-10 22:00:16 +01:00
|
|
|
|
return fetchUnreadArticles(forContainer: folder)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func fetchUnreadArticles(forContainer container: Container) -> Set<Article> {
|
|
|
|
|
|
|
|
|
|
let feeds = container.flattenedFeeds()
|
2018-07-28 21:16:14 +02:00
|
|
|
|
let articles = database.fetchUnreadArticles(for: feeds.feedIDs())
|
2018-09-11 07:08:38 +02:00
|
|
|
|
|
|
|
|
|
// Validate unread counts. This was the site of a performance slowdown:
|
|
|
|
|
// it was calling going through the entire list of articles once per feed:
|
|
|
|
|
// feeds.forEach { validateUnreadCount($0, articles) }
|
|
|
|
|
// Now we loop through articles exactly once. This makes a huge difference.
|
|
|
|
|
|
|
|
|
|
var unreadCountStorage = [String: Int]() // [FeedID: Int]
|
|
|
|
|
articles.forEach { (article) in
|
|
|
|
|
precondition(!article.status.read)
|
|
|
|
|
unreadCountStorage[article.feedID, default: 0] += 1
|
|
|
|
|
}
|
|
|
|
|
feeds.forEach { (feed) in
|
|
|
|
|
let unreadCount = unreadCountStorage[feed.feedID, default: 0]
|
|
|
|
|
feed.unreadCount = unreadCount
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 20:27:55 +01:00
|
|
|
|
return articles
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 02:37:47 +01:00
|
|
|
|
public func fetchTodayArticles() -> Set<Article> {
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
return database.fetchTodayArticles(for: flattenedFeeds().feedIDs())
|
2018-02-11 02:37:47 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 21:07:55 +01:00
|
|
|
|
public func fetchStarredArticles() -> Set<Article> {
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
return database.fetchStarredArticles(for: flattenedFeeds().feedIDs())
|
2018-02-11 21:07:55 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-25 04:22:16 +01:00
|
|
|
|
public func fetchArticlesMatching(_ searchString: String) -> Set<Article> {
|
|
|
|
|
return database.fetchArticlesMatching(searchString, for: flattenedFeeds().feedIDs())
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 20:27:55 +01:00
|
|
|
|
private func validateUnreadCount(_ feed: Feed, _ articles: Set<Article>) {
|
|
|
|
|
|
|
|
|
|
// articles must contain all the unread articles for the feed.
|
|
|
|
|
// The unread number should match the feed’s unread count.
|
|
|
|
|
|
|
|
|
|
let feedUnreadCount = articles.reduce(0) { (result, article) -> Int in
|
|
|
|
|
if article.feed == feed && !article.status.read {
|
|
|
|
|
return result + 1
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-29 19:13:11 +01:00
|
|
|
|
feed.unreadCount = feedUnreadCount
|
2017-10-09 03:58:15 +02:00
|
|
|
|
}
|
2017-11-19 21:12:43 +01:00
|
|
|
|
|
2017-11-19 21:45:35 +01:00
|
|
|
|
public func fetchUnreadCountForToday(_ callback: @escaping (Int) -> Void) {
|
2017-11-19 21:12:43 +01:00
|
|
|
|
|
|
|
|
|
let startOfToday = NSCalendar.startOfToday()
|
2018-07-28 21:16:14 +02:00
|
|
|
|
database.fetchUnreadCount(for: flattenedFeeds().feedIDs(), since: startOfToday, callback: callback)
|
2017-11-19 21:12:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
|
public func fetchUnreadCountForStarredArticles(_ callback: @escaping (Int) -> Void) {
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
database.fetchStarredAndUnreadCount(for: flattenedFeeds().feedIDs(), callback: callback)
|
2017-11-20 00:40:02 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-14 22:25:38 +02:00
|
|
|
|
public func opmlDocument() -> String {
|
|
|
|
|
let escapedTitle = nameForDisplay.rs_stringByEscapingSpecialXMLCharacters()
|
|
|
|
|
let openingText =
|
|
|
|
|
"""
|
|
|
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<!-- OPML generated by NetNewsWire -->
|
|
|
|
|
<opml version="1.1">
|
|
|
|
|
<head>
|
|
|
|
|
<title>\(escapedTitle)</title>
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
let middleText = OPMLString(indentLevel: 0)
|
|
|
|
|
|
|
|
|
|
let closingText =
|
|
|
|
|
"""
|
|
|
|
|
</body>
|
|
|
|
|
</opml>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
let opml = openingText + middleText + closingText
|
|
|
|
|
return opml
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-15 07:06:03 +02:00
|
|
|
|
public func unreadCount(for feed: Feed) -> Int {
|
|
|
|
|
return unreadCounts[feed.feedID] ?? 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func setUnreadCount(_ unreadCount: Int, for feed: Feed) {
|
|
|
|
|
unreadCounts[feed.feedID] = unreadCount
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
public func structureDidChange() {
|
|
|
|
|
// Feeds were added or deleted. Or folders added or deleted.
|
|
|
|
|
// Or feeds inside folders were added or deleted.
|
2018-09-17 05:02:24 +02:00
|
|
|
|
if !startingUp {
|
|
|
|
|
dirty = true
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
flattenedFeedsNeedUpdate = true
|
|
|
|
|
feedDictionaryNeedsUpdate = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Container
|
|
|
|
|
|
|
|
|
|
public func flattenedFeeds() -> Set<Feed> {
|
|
|
|
|
if flattenedFeedsNeedUpdate {
|
|
|
|
|
updateFlattenedFeeds()
|
|
|
|
|
}
|
|
|
|
|
return _flattenedFeeds
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func deleteFeed(_ feed: Feed) {
|
|
|
|
|
topLevelFeeds.remove(feed)
|
|
|
|
|
structureDidChange()
|
|
|
|
|
postChildrenDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func deleteFolder(_ folder: Folder) {
|
|
|
|
|
folders?.remove(folder)
|
|
|
|
|
structureDidChange()
|
|
|
|
|
postChildrenDidChangeNotification()
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:13:15 +01:00
|
|
|
|
// MARK: - Debug
|
|
|
|
|
|
|
|
|
|
public func debugDropConditionalGetInfo() {
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
flattenedFeeds().forEach{ $0.debugDropConditionalGetInfo() }
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 07:29:43 +01:00
|
|
|
|
public func debugRunSearch() {
|
|
|
|
|
#if DEBUG
|
2019-02-25 04:22:16 +01:00
|
|
|
|
let t1 = Date()
|
|
|
|
|
let articles = fetchArticlesMatching("Brent NetNewsWire")
|
|
|
|
|
let t2 = Date()
|
|
|
|
|
print(t2.timeIntervalSince(t1))
|
|
|
|
|
print(articles.count)
|
2019-02-19 07:29:43 +01:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 02:43:10 +02:00
|
|
|
|
// MARK: - Notifications
|
|
|
|
|
|
|
|
|
|
@objc func downloadProgressDidChange(_ note: Notification) {
|
2017-10-08 02:20:19 +02:00
|
|
|
|
|
2017-10-08 02:43:10 +02:00
|
|
|
|
guard let noteObject = note.object as? DownloadProgress, noteObject === refreshProgress else {
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-10-08 02:20:19 +02:00
|
|
|
|
|
|
|
|
|
refreshInProgress = refreshProgress.numberRemaining > 0
|
|
|
|
|
NotificationCenter.default.post(name: .AccountRefreshProgressDidChange, object: self)
|
|
|
|
|
}
|
2017-10-13 06:02:27 +02:00
|
|
|
|
|
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
2018-09-17 04:07:07 +02:00
|
|
|
|
if let feed = note.object as? Feed, feed.account === self {
|
2018-09-14 07:25:10 +02:00
|
|
|
|
updateUnreadCount()
|
2017-10-13 15:50:33 +02:00
|
|
|
|
}
|
2017-10-13 06:02:27 +02:00
|
|
|
|
}
|
2017-11-15 22:26:10 +01:00
|
|
|
|
|
|
|
|
|
@objc func batchUpdateDidPerform(_ note: Notification) {
|
2017-11-26 03:05:20 +01:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
flattenedFeedsNeedUpdate = true
|
2017-11-26 03:05:20 +01:00
|
|
|
|
rebuildFeedDictionaries()
|
2017-11-15 22:26:10 +01:00
|
|
|
|
updateUnreadCount()
|
|
|
|
|
}
|
2017-10-08 02:20:19 +02:00
|
|
|
|
|
2018-02-25 00:54:32 +01:00
|
|
|
|
@objc func childrenDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
|
|
guard let object = note.object else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if let account = object as? Account, account === self {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
structureDidChange()
|
2018-02-25 00:54:32 +01:00
|
|
|
|
}
|
|
|
|
|
if let folder = object as? Folder, folder.account === self {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
structureDidChange()
|
2018-02-25 00:54:32 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-24 06:49:33 +01:00
|
|
|
|
@objc func displayNameDidChange(_ note: Notification) {
|
|
|
|
|
|
2018-02-25 00:54:32 +01:00
|
|
|
|
if let folder = note.object as? Folder, folder.account === self {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
structureDidChange()
|
2018-01-24 06:49:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 00:38:54 +01:00
|
|
|
|
@objc func saveToDiskIfNeeded() {
|
|
|
|
|
|
2018-02-25 00:54:32 +01:00
|
|
|
|
if dirty {
|
|
|
|
|
saveToDisk()
|
2018-02-18 00:38:54 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 07:41:43 +01:00
|
|
|
|
@objc func saveMetadataIfNeeded() {
|
|
|
|
|
if feedMetadataDirty {
|
|
|
|
|
saveFeedMetadata()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-28 06:10:14 +01:00
|
|
|
|
@objc func saveSettingsIfNeeded() {
|
|
|
|
|
if settingsDirty {
|
|
|
|
|
saveSettings()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-25 20:54:58 +02:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(accountID)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-17 20:32:58 +02:00
|
|
|
|
// MARK: - Equatable
|
2017-09-17 00:30:26 +02:00
|
|
|
|
|
2017-09-17 20:32:58 +02:00
|
|
|
|
public class func ==(lhs: Account, rhs: Account) -> Bool {
|
2017-09-17 00:30:26 +02:00
|
|
|
|
|
2017-09-17 20:32:58 +02:00
|
|
|
|
return lhs === rhs
|
2017-09-17 00:30:26 +02:00
|
|
|
|
}
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-14 07:41:43 +01:00
|
|
|
|
// MARK: - FeedMetadataDelegate
|
|
|
|
|
|
|
|
|
|
extension Account: FeedMetadataDelegate {
|
|
|
|
|
|
|
|
|
|
func valueDidChange(_ feedMetadata: FeedMetadata, key: FeedMetadata.CodingKeys) {
|
|
|
|
|
feedMetadataDirty = true
|
2019-03-17 20:47:04 +01:00
|
|
|
|
guard let feed = existingFeed(with: feedMetadata.feedID) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
feed.postFeedSettingDidChangeNotification(key)
|
2019-03-14 07:41:43 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 05:11:17 +02:00
|
|
|
|
// MARK: - Disk (Private)
|
2017-09-27 22:29:05 +02:00
|
|
|
|
|
2017-10-08 05:11:17 +02:00
|
|
|
|
private extension Account {
|
|
|
|
|
|
2018-02-18 03:02:40 +01:00
|
|
|
|
func queueSaveToDiskIfNeeded() {
|
|
|
|
|
Account.saveQueue.add(self, #selector(saveToDiskIfNeeded))
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 05:11:17 +02:00
|
|
|
|
func pullObjectsFromDisk() {
|
2019-03-21 06:10:22 +01:00
|
|
|
|
importSettings()
|
2019-03-14 07:41:43 +01:00
|
|
|
|
importFeedMetadata()
|
2018-09-16 21:42:46 +02:00
|
|
|
|
importOPMLFile(path: opmlFilePath)
|
2017-10-08 03:15:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 06:10:22 +01:00
|
|
|
|
func importSettings() {
|
|
|
|
|
let url = URL(fileURLWithPath: settingsPath)
|
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
|
settings = (try? decoder.decode(AccountSettings.self, from: data)) ?? AccountSettings()
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 07:41:43 +01:00
|
|
|
|
func importFeedMetadata() {
|
|
|
|
|
let url = URL(fileURLWithPath: feedMetadataPath)
|
|
|
|
|
guard let data = try? Data(contentsOf: url) else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
let decoder = PropertyListDecoder()
|
|
|
|
|
feedMetadata = (try? decoder.decode(FeedMetadataDictionary.self, from: data)) ?? FeedMetadataDictionary()
|
|
|
|
|
feedMetadata.values.forEach { $0.delegate = self }
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 21:42:46 +02:00
|
|
|
|
func importOPMLFile(path: String) {
|
|
|
|
|
let opmlFileURL = URL(fileURLWithPath: path)
|
|
|
|
|
var fileData: Data?
|
|
|
|
|
do {
|
|
|
|
|
fileData = try Data(contentsOf: opmlFileURL)
|
|
|
|
|
} catch {
|
2018-09-27 07:05:35 +02:00
|
|
|
|
// Commented out because it’s not an error on first run.
|
|
|
|
|
// TODO: make it so we know if it’s first run or not.
|
|
|
|
|
//NSApplication.shared.presentError(error)
|
2018-09-16 21:42:46 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
guard let opmlData = fileData else {
|
|
|
|
|
return
|
2017-10-08 03:15:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-16 21:42:46 +02:00
|
|
|
|
let parserData = ParserData(url: opmlFileURL.absoluteString, data: opmlData)
|
|
|
|
|
var opmlDocument: RSOPMLDocument?
|
2017-10-19 04:14:40 +02:00
|
|
|
|
|
2018-09-16 21:42:46 +02:00
|
|
|
|
do {
|
|
|
|
|
opmlDocument = try RSOPMLParser.parseOPML(with: parserData)
|
|
|
|
|
} catch {
|
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
guard let parsedOPML = opmlDocument, let children = parsedOPML.children else {
|
2018-09-16 21:42:46 +02:00
|
|
|
|
return
|
2017-10-19 04:14:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
BatchUpdate.shared.perform {
|
|
|
|
|
importOPMLItems(children, parentFolder: nil)
|
|
|
|
|
}
|
2017-10-08 03:15:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveToDisk() {
|
|
|
|
|
|
2018-09-14 22:25:38 +02:00
|
|
|
|
dirty = false
|
|
|
|
|
|
|
|
|
|
let opmlDocumentString = opmlDocument()
|
|
|
|
|
do {
|
|
|
|
|
let url = URL(fileURLWithPath: opmlFilePath)
|
|
|
|
|
try opmlDocumentString.write(to: url, atomically: true, encoding: .utf8)
|
|
|
|
|
}
|
|
|
|
|
catch let error as NSError {
|
|
|
|
|
NSApplication.shared.presentError(error)
|
|
|
|
|
}
|
2017-10-08 05:11:17 +02:00
|
|
|
|
}
|
2019-03-14 07:41:43 +01:00
|
|
|
|
|
|
|
|
|
func queueSaveMetadataIfNeeded() {
|
|
|
|
|
Account.saveQueue.add(self, #selector(saveMetadataIfNeeded))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func metadataForOnlySubscribedToFeeds() -> FeedMetadataDictionary {
|
|
|
|
|
let feedIDs = idToFeedDictionary.keys
|
|
|
|
|
return feedMetadata.filter { (feedID: String, metadata: FeedMetadata) -> Bool in
|
|
|
|
|
return feedIDs.contains(feedID)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveFeedMetadata() {
|
|
|
|
|
feedMetadataDirty = false
|
|
|
|
|
|
|
|
|
|
let d = metadataForOnlySubscribedToFeeds()
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
|
let url = URL(fileURLWithPath: feedMetadataPath)
|
|
|
|
|
do {
|
|
|
|
|
let data = try encoder.encode(d)
|
|
|
|
|
try data.write(to: url)
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-28 06:10:14 +01:00
|
|
|
|
|
|
|
|
|
func queueSaveSettingsIfNeeded() {
|
|
|
|
|
Account.saveQueue.add(self, #selector(saveSettingsIfNeeded))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func saveSettings() {
|
|
|
|
|
settingsDirty = false
|
|
|
|
|
|
|
|
|
|
let encoder = PropertyListEncoder()
|
|
|
|
|
encoder.outputFormat = .binary
|
|
|
|
|
let url = URL(fileURLWithPath: settingsPath)
|
|
|
|
|
do {
|
|
|
|
|
let data = try encoder.encode(settings)
|
|
|
|
|
try data.write(to: url)
|
|
|
|
|
}
|
|
|
|
|
catch {
|
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-27 22:29:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 19:59:35 +02:00
|
|
|
|
// MARK: - Private
|
2017-09-27 22:29:05 +02:00
|
|
|
|
|
|
|
|
|
private extension Account {
|
|
|
|
|
|
2019-03-17 01:30:30 +01:00
|
|
|
|
func metadata(feedID: String) -> FeedMetadata {
|
|
|
|
|
if let d = feedMetadata[feedID] {
|
|
|
|
|
assert(d.delegate === self)
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
let d = FeedMetadata(feedID: feedID)
|
|
|
|
|
d.delegate = self
|
|
|
|
|
feedMetadata[feedID] = d
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
func updateFlattenedFeeds() {
|
|
|
|
|
var feeds = Set<Feed>()
|
|
|
|
|
feeds.formUnion(topLevelFeeds)
|
|
|
|
|
for folder in folders! {
|
|
|
|
|
feeds.formUnion(folder.flattenedFeeds())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_flattenedFeeds = feeds
|
|
|
|
|
flattenedFeedsNeedUpdate = false
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 20:08:51 +02:00
|
|
|
|
func rebuildFeedDictionaries() {
|
|
|
|
|
var idDictionary = [String: Feed]()
|
|
|
|
|
|
|
|
|
|
flattenedFeeds().forEach { (feed) in
|
|
|
|
|
idDictionary[feed.feedID] = feed
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
_idToFeedDictionary = idDictionary
|
|
|
|
|
feedDictionaryNeedsUpdate = false
|
2017-10-01 19:59:35 +02:00
|
|
|
|
}
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
|
func createFeed(with opmlFeedSpecifier: RSOPMLFeedSpecifier) -> Feed {
|
2019-03-16 20:08:31 +01:00
|
|
|
|
let feedID = opmlFeedSpecifier.feedURL
|
|
|
|
|
let feedMetadata = metadata(feedID: feedID)
|
|
|
|
|
let feed = Feed(account: self, url: opmlFeedSpecifier.feedURL, feedID: feedID, metadata: feedMetadata)
|
2019-02-03 03:08:01 +01:00
|
|
|
|
if let feedTitle = opmlFeedSpecifier.title {
|
|
|
|
|
if feed.name == nil {
|
|
|
|
|
feed.name = feedTitle
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
}
|
2017-10-22 06:00:21 +02:00
|
|
|
|
return feed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func importOPMLItems(_ items: [RSOPMLItem], parentFolder: Folder?) {
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
var feedsToAdd = Set<Feed>()
|
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
|
items.forEach { (item) in
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
|
|
|
|
if let feedSpecifier = item.feedSpecifier {
|
2017-10-22 06:00:21 +02:00
|
|
|
|
let feed = createFeed(with: feedSpecifier)
|
2018-09-17 02:54:42 +02:00
|
|
|
|
feedsToAdd.insert(feed)
|
2017-10-22 06:00:21 +02:00
|
|
|
|
return
|
|
|
|
|
}
|
2017-10-08 06:41:21 +02:00
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
|
guard let folderName = item.titleFromAttributes else {
|
|
|
|
|
// Folder doesn’t have a name, so it won’t be created, and its items will go one level up.
|
2019-01-28 06:42:58 +01:00
|
|
|
|
if let itemChildren = item.children {
|
|
|
|
|
importOPMLItems(itemChildren, parentFolder: parentFolder)
|
|
|
|
|
}
|
2017-10-22 06:00:21 +02:00
|
|
|
|
return
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 06:00:21 +02:00
|
|
|
|
if let folder = ensureFolder(with: folderName) {
|
2019-01-28 06:42:58 +01:00
|
|
|
|
if let itemChildren = item.children {
|
|
|
|
|
importOPMLItems(itemChildren, parentFolder: folder)
|
|
|
|
|
}
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
|
|
|
|
|
if !feedsToAdd.isEmpty {
|
|
|
|
|
addFeeds(feedsToAdd, to: parentFolder)
|
|
|
|
|
}
|
2017-10-08 06:41:21 +02:00
|
|
|
|
}
|
2017-10-10 22:23:12 +02:00
|
|
|
|
|
|
|
|
|
func updateUnreadCount() {
|
2018-09-17 02:54:42 +02:00
|
|
|
|
if fetchingAllUnreadCounts {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var updatedUnreadCount = 0
|
|
|
|
|
for feed in flattenedFeeds() {
|
|
|
|
|
updatedUnreadCount += feed.unreadCount
|
|
|
|
|
}
|
|
|
|
|
unreadCount = updatedUnreadCount
|
2017-10-10 22:23:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-13 06:02:27 +02:00
|
|
|
|
func noteStatusesForArticlesDidChange(_ articles: Set<Article>) {
|
2017-10-10 22:23:12 +02:00
|
|
|
|
|
2018-01-28 03:50:48 +01:00
|
|
|
|
let feeds = Set(articles.compactMap { $0.feed })
|
2017-10-13 06:02:27 +02:00
|
|
|
|
let statuses = Set(articles.map { $0.status })
|
2017-10-10 22:23:12 +02:00
|
|
|
|
|
|
|
|
|
// .UnreadCountDidChange notification will get sent to Folder and Account objects,
|
|
|
|
|
// which will update their own unread counts.
|
|
|
|
|
updateUnreadCounts(for: feeds)
|
|
|
|
|
|
|
|
|
|
NotificationCenter.default.post(name: .StatusesDidChange, object: self, userInfo: [UserInfoKey.statuses: statuses, UserInfoKey.articles: articles, UserInfoKey.feeds: feeds])
|
|
|
|
|
}
|
2017-12-03 20:57:53 +01:00
|
|
|
|
|
|
|
|
|
func fetchAllUnreadCounts() {
|
|
|
|
|
|
2018-09-17 02:54:42 +02:00
|
|
|
|
fetchingAllUnreadCounts = true
|
2017-12-03 20:57:53 +01:00
|
|
|
|
database.fetchAllNonZeroUnreadCounts { (unreadCountDictionary) in
|
|
|
|
|
|
|
|
|
|
if unreadCountDictionary.isEmpty {
|
2018-09-27 07:05:35 +02:00
|
|
|
|
self.fetchingAllUnreadCounts = false
|
|
|
|
|
self.updateUnreadCount()
|
2017-12-03 20:57:53 +01:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.flattenedFeeds().forEach{ (feed) in
|
|
|
|
|
|
|
|
|
|
// When the unread count is zero, it won’t appear in unreadCountDictionary.
|
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
|
if let unreadCount = unreadCountDictionary[feed.feedID] {
|
2017-12-03 20:57:53 +01:00
|
|
|
|
feed.unreadCount = unreadCount
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
feed.unreadCount = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
self.fetchingAllUnreadCounts = false
|
2018-09-14 07:25:10 +02:00
|
|
|
|
self.updateUnreadCount()
|
2017-12-03 20:57:53 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-22 20:08:51 +02:00
|
|
|
|
// MARK: - Container Overrides
|
|
|
|
|
|
|
|
|
|
extension Account {
|
|
|
|
|
|
|
|
|
|
public func existingFeed(with feedID: String) -> Feed? {
|
|
|
|
|
|
|
|
|
|
return idToFeedDictionary[feedID]
|
|
|
|
|
}
|
2018-09-17 02:54:42 +02:00
|
|
|
|
|
2017-10-22 20:08:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-27 22:29:05 +02:00
|
|
|
|
// MARK: - OPMLRepresentable
|
|
|
|
|
|
2017-09-17 00:25:38 +02:00
|
|
|
|
extension Account: OPMLRepresentable {
|
|
|
|
|
|
|
|
|
|
public func OPMLString(indentLevel: Int) -> String {
|
|
|
|
|
|
|
|
|
|
var s = ""
|
2018-09-17 02:54:42 +02:00
|
|
|
|
for feed in topLevelFeeds {
|
|
|
|
|
s += feed.OPMLString(indentLevel: indentLevel + 1)
|
|
|
|
|
}
|
|
|
|
|
for folder in folders! {
|
|
|
|
|
s += folder.OPMLString(indentLevel: indentLevel + 1)
|
2017-09-17 00:25:38 +02:00
|
|
|
|
}
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
}
|