2017-11-19 13:57:42 -08:00
|
|
|
//
|
2017-11-19 15:40:02 -08:00
|
|
|
// SmartFeed.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
// NetNewsWire
|
2017-11-19 13:57:42 -08:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 11/19/17.
|
|
|
|
// Copyright © 2017 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2018-07-23 18:29:08 -07:00
|
|
|
import Articles
|
2019-12-16 22:45:59 -08:00
|
|
|
import ArticlesDatabase
|
2017-11-19 13:57:42 -08:00
|
|
|
import Account
|
2024-03-10 18:17:04 -07:00
|
|
|
import Database
|
2024-03-20 20:49:15 -07:00
|
|
|
import Core
|
2017-11-19 13:57:42 -08:00
|
|
|
|
2017-11-19 15:40:02 -08:00
|
|
|
final class SmartFeed: PseudoFeed {
|
|
|
|
|
2021-10-20 19:03:02 -05:00
|
|
|
var account: Account? = nil
|
|
|
|
|
2024-02-26 08:12:34 -08:00
|
|
|
var defaultReadFilterType: ReadFilterType {
|
2019-11-21 19:54:35 -06:00
|
|
|
return .none
|
2019-11-21 18:22:43 -06:00
|
|
|
}
|
|
|
|
|
2024-02-25 21:34:22 -08:00
|
|
|
var sidebarItemID: SidebarItemIdentifier? {
|
|
|
|
delegate.sidebarItemID
|
2019-11-15 06:19:14 -06:00
|
|
|
}
|
|
|
|
|
2017-11-19 15:40:02 -08:00
|
|
|
var nameForDisplay: String {
|
2018-02-14 13:14:25 -08:00
|
|
|
return delegate.nameForDisplay
|
2017-11-19 15:40:02 -08:00
|
|
|
}
|
2017-11-19 13:57:42 -08:00
|
|
|
|
|
|
|
var unreadCount = 0 {
|
|
|
|
didSet {
|
|
|
|
if unreadCount != oldValue {
|
|
|
|
postUnreadCountDidChangeNotification()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 18:05:57 -06:00
|
|
|
var smallIcon: IconImage? {
|
2019-09-17 20:26:49 -05:00
|
|
|
return delegate.smallIcon
|
|
|
|
}
|
|
|
|
|
2019-04-15 15:03:05 -05:00
|
|
|
#if os(macOS)
|
2018-02-11 22:10:28 -08:00
|
|
|
var pasteboardWriter: NSPasteboardWriting {
|
|
|
|
return SmartFeedPasteboardWriter(smartFeed: self)
|
|
|
|
}
|
2019-04-15 15:03:05 -05:00
|
|
|
#endif
|
2018-02-11 22:10:28 -08:00
|
|
|
|
2017-11-19 15:40:02 -08:00
|
|
|
private let delegate: SmartFeedDelegate
|
2019-05-22 15:08:22 -05:00
|
|
|
private var unreadCounts = [String: Int]()
|
2017-11-19 13:57:42 -08:00
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
@MainActor init(delegate: SmartFeedDelegate) {
|
2017-11-19 15:40:02 -08:00
|
|
|
self.delegate = delegate
|
2017-11-19 13:57:42 -08:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
2018-02-17 15:15:26 -08:00
|
|
|
queueFetchUnreadCounts() // Fetch unread count at startup
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
@MainActor @objc func unreadCountDidChange(_ note: Notification) {
|
2019-05-06 17:46:41 -05:00
|
|
|
if note.object is AppDelegate {
|
2018-02-17 15:15:26 -08:00
|
|
|
queueFetchUnreadCounts()
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-17 15:15:26 -08:00
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
@MainActor @objc func fetchUnreadCounts() {
|
|
|
|
|
2019-11-01 19:26:32 -05:00
|
|
|
let activeAccounts = AccountManager.shared.activeAccounts
|
2024-03-24 18:49:39 -07:00
|
|
|
|
2019-11-01 19:26:32 -05:00
|
|
|
// Remove any accounts that are no longer active or have been deleted
|
|
|
|
let activeAccountIDs = activeAccounts.map { $0.accountID }
|
|
|
|
unreadCounts.keys.forEach { accountID in
|
|
|
|
if !activeAccountIDs.contains(accountID) {
|
|
|
|
unreadCounts.removeValue(forKey: accountID)
|
|
|
|
}
|
|
|
|
}
|
2024-03-24 18:49:39 -07:00
|
|
|
|
2019-11-01 19:26:32 -05:00
|
|
|
if activeAccounts.isEmpty {
|
|
|
|
updateUnreadCount()
|
2024-03-24 18:49:39 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Task { @MainActor in
|
|
|
|
for account in activeAccounts {
|
|
|
|
await fetchUnreadCount(for: account)
|
|
|
|
}
|
2019-11-01 19:26:32 -05:00
|
|
|
}
|
2018-02-17 15:15:26 -08:00
|
|
|
}
|
2018-02-10 13:22:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SmartFeed: ArticleFetcher {
|
|
|
|
|
2024-03-18 21:08:37 -07:00
|
|
|
func fetchArticles() async throws -> Set<Article> {
|
|
|
|
|
|
|
|
try await delegate.fetchArticles()
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchUnreadArticles() async throws -> Set<Article> {
|
|
|
|
|
|
|
|
try await delegate.fetchUnreadArticles()
|
|
|
|
}
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
|
|
|
|
2017-11-19 15:40:02 -08:00
|
|
|
private extension SmartFeed {
|
2017-11-19 13:57:42 -08:00
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
@MainActor func queueFetchUnreadCounts() {
|
|
|
|
CoalescingQueue.standard.add(self, #selector(fetchUnreadCounts))
|
2018-02-17 15:15:26 -08:00
|
|
|
}
|
2017-11-19 13:57:42 -08:00
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
@MainActor func fetchUnreadCount(for account: Account) async {
|
2024-03-19 23:05:30 -07:00
|
|
|
|
2024-03-24 18:49:39 -07:00
|
|
|
let unreadCount = await delegate.unreadCount(account: account)
|
|
|
|
unreadCounts[account.accountID] = unreadCount
|
|
|
|
|
|
|
|
updateUnreadCount()
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
|
|
|
|
2024-03-19 23:05:30 -07:00
|
|
|
@MainActor func updateUnreadCount() {
|
2024-03-24 18:49:39 -07:00
|
|
|
|
|
|
|
var unread = 0
|
|
|
|
for account in AccountManager.shared.activeAccounts {
|
|
|
|
unread = unread + (unreadCounts[account.accountID] ?? 0)
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
2024-03-24 18:49:39 -07:00
|
|
|
|
|
|
|
unreadCount = unread
|
2017-11-19 13:57:42 -08:00
|
|
|
}
|
|
|
|
}
|