NetNewsWire/Shared/SmartFeeds/SmartFeed.swift

127 lines
2.6 KiB
Swift
Raw Normal View History

2017-11-19 22:57:42 +01:00
//
// SmartFeed.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-11-19 22:57:42 +01:00
//
// Created by Brent Simmons on 11/19/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import Articles
import ArticlesDatabase
2017-11-19 22:57:42 +01:00
import Account
2024-03-11 02:17:04 +01:00
import Database
import Core
2017-11-19 22:57:42 +01:00
final class SmartFeed: PseudoFeed {
var account: Account? = nil
var defaultReadFilterType: ReadFilterType {
return .none
2019-11-22 01:22:43 +01:00
}
2024-02-26 06:34:22 +01:00
var sidebarItemID: SidebarItemIdentifier? {
delegate.sidebarItemID
2019-11-15 13:19:14 +01:00
}
var nameForDisplay: String {
return delegate.nameForDisplay
}
2017-11-19 22:57:42 +01:00
var unreadCount = 0 {
didSet {
if unreadCount != oldValue {
postUnreadCountDidChangeNotification()
}
}
}
var smallIcon: IconImage? {
return delegate.smallIcon
}
#if os(macOS)
var pasteboardWriter: NSPasteboardWriting {
return SmartFeedPasteboardWriter(smartFeed: self)
}
#endif
private let delegate: SmartFeedDelegate
private var unreadCounts = [String: Int]()
2017-11-19 22:57:42 +01:00
2024-03-25 02:49:39 +01:00
@MainActor init(delegate: SmartFeedDelegate) {
self.delegate = delegate
2017-11-19 22:57:42 +01:00
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
queueFetchUnreadCounts() // Fetch unread count at startup
2017-11-19 22:57:42 +01:00
}
2024-03-25 02:49:39 +01:00
@MainActor @objc func unreadCountDidChange(_ note: Notification) {
if note.object is AppDelegate {
queueFetchUnreadCounts()
2017-11-19 22:57:42 +01:00
}
}
2024-03-25 02:49:39 +01:00
@MainActor @objc func fetchUnreadCounts() {
let activeAccounts = AccountManager.shared.activeAccounts
2024-03-25 02:49:39 +01: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-25 02:49:39 +01:00
if activeAccounts.isEmpty {
updateUnreadCount()
2024-03-25 02:49:39 +01:00
return
}
Task { @MainActor in
for account in activeAccounts {
await fetchUnreadCount(for: account)
}
}
}
}
extension SmartFeed: ArticleFetcher {
2024-03-19 05:08:37 +01:00
func fetchArticles() async throws -> Set<Article> {
try await delegate.fetchArticles()
}
func fetchUnreadArticles() async throws -> Set<Article> {
try await delegate.fetchUnreadArticles()
}
2017-11-19 22:57:42 +01:00
}
private extension SmartFeed {
2017-11-19 22:57:42 +01:00
2024-03-25 02:49:39 +01:00
@MainActor func queueFetchUnreadCounts() {
CoalescingQueue.standard.add(self, #selector(fetchUnreadCounts))
}
2017-11-19 22:57:42 +01:00
2024-03-25 02:49:39 +01:00
@MainActor func fetchUnreadCount(for account: Account) async {
2024-03-20 07:05:30 +01:00
2024-03-25 02:49:39 +01:00
let unreadCount = await delegate.unreadCount(account: account)
unreadCounts[account.accountID] = unreadCount
updateUnreadCount()
2017-11-19 22:57:42 +01:00
}
2024-03-20 07:05:30 +01:00
@MainActor func updateUnreadCount() {
2024-03-25 02:49:39 +01:00
var unread = 0
for account in AccountManager.shared.activeAccounts {
unread = unread + (unreadCounts[account.accountID] ?? 0)
2017-11-19 22:57:42 +01:00
}
2024-03-25 02:49:39 +01:00
unreadCount = unread
2017-11-19 22:57:42 +01:00
}
}