2017-11-19 22:57:42 +01:00
|
|
|
//
|
2017-11-20 00:40:02 +01:00
|
|
|
// SmartFeed.swift
|
2017-11-19 22:57:42 +01:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/19/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2017-11-20 00:40:02 +01:00
|
|
|
import RSCore
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2017-11-19 22:57:42 +01:00
|
|
|
import Account
|
|
|
|
|
2018-02-10 22:22:02 +01:00
|
|
|
protocol SmartFeedDelegate: DisplayNameProvider, ArticleFetcher {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
func fetchUnreadCount(for: Account, callback: @escaping (Int) -> Void)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class SmartFeed: PseudoFeed {
|
|
|
|
|
|
|
|
var nameForDisplay: String {
|
2018-02-14 22:14:25 +01:00
|
|
|
return delegate.nameForDisplay
|
2017-11-20 00:40:02 +01:00
|
|
|
}
|
2017-11-19 22:57:42 +01:00
|
|
|
|
|
|
|
var unreadCount = 0 {
|
|
|
|
didSet {
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 07:10:28 +01:00
|
|
|
var pasteboardWriter: NSPasteboardWriting {
|
|
|
|
return SmartFeedPasteboardWriter(smartFeed: self)
|
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
private let delegate: SmartFeedDelegate
|
2017-11-19 22:57:42 +01:00
|
|
|
private var unreadCounts = [Account: Int]()
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
init(delegate: SmartFeedDelegate) {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
self.delegate = delegate
|
2017-11-19 22:57:42 +01:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2018-02-18 00:15:26 +01:00
|
|
|
queueFetchUnreadCounts() // Fetch unread count at startup
|
2017-11-19 22:57:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func unreadCountDidChange(_ note: Notification) {
|
|
|
|
|
|
|
|
if note.object is Account {
|
2018-02-18 00:15:26 +01:00
|
|
|
queueFetchUnreadCounts()
|
2017-11-19 22:57:42 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 00:15:26 +01:00
|
|
|
|
|
|
|
@objc func fetchUnreadCounts() {
|
|
|
|
|
|
|
|
AccountManager.shared.accounts.forEach { self.fetchUnreadCount(for: $0) }
|
|
|
|
}
|
2018-02-10 22:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SmartFeed: ArticleFetcher {
|
|
|
|
|
|
|
|
func fetchArticles() -> Set<Article> {
|
|
|
|
|
|
|
|
return delegate.fetchArticles()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchUnreadArticles() -> Set<Article> {
|
|
|
|
|
|
|
|
return delegate.fetchUnreadArticles()
|
|
|
|
}
|
2017-11-19 22:57:42 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
private extension SmartFeed {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2018-02-18 00:15:26 +01:00
|
|
|
func queueFetchUnreadCounts() {
|
|
|
|
|
2018-02-18 02:45:05 +01:00
|
|
|
CoalescingQueue.standard.add(self, #selector(fetchUnreadCounts))
|
2018-02-18 00:15:26 +01:00
|
|
|
}
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2018-02-18 00:15:26 +01:00
|
|
|
func fetchUnreadCount(for account: Account) {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
delegate.fetchUnreadCount(for: account) { (accountUnreadCount) in
|
2017-11-19 22:57:42 +01:00
|
|
|
self.unreadCounts[account] = accountUnreadCount
|
|
|
|
self.updateUnreadCount()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-18 00:15:26 +01:00
|
|
|
func updateUnreadCount() {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
|
|
|
unreadCount = AccountManager.shared.accounts.reduce(0) { (result, account) -> Int in
|
|
|
|
if let oneUnreadCount = unreadCounts[account] {
|
|
|
|
return result + oneUnreadCount
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|