2017-11-19 22:57:42 +01:00
|
|
|
//
|
2017-11-20 00:40:02 +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
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2019-12-17 07:45:59 +01:00
|
|
|
import ArticlesDatabase
|
2017-11-19 22:57:42 +01:00
|
|
|
import Account
|
2024-03-11 02:17:04 +01:00
|
|
|
import Database
|
2024-03-21 04:49:15 +01:00
|
|
|
import Core
|
2024-04-16 07:21:17 +02:00
|
|
|
import Images
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
final class SmartFeed: PseudoFeed {
|
|
|
|
|
2021-10-21 02:03:02 +02:00
|
|
|
var account: Account? = nil
|
|
|
|
|
2024-02-26 17:12:34 +01:00
|
|
|
var defaultReadFilterType: ReadFilterType {
|
2019-11-22 02:54:35 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 01:05:57 +01:00
|
|
|
var smallIcon: IconImage? {
|
2019-09-18 03:26:49 +02:00
|
|
|
return delegate.smallIcon
|
|
|
|
}
|
|
|
|
|
2019-04-15 22:03:05 +02:00
|
|
|
#if os(macOS)
|
2018-02-12 07:10:28 +01:00
|
|
|
var pasteboardWriter: NSPasteboardWriting {
|
|
|
|
return SmartFeedPasteboardWriter(smartFeed: self)
|
|
|
|
}
|
2019-04-15 22:03:05 +02:00
|
|
|
#endif
|
2018-02-12 07:10:28 +01:00
|
|
|
|
2024-06-10 07:10:20 +02:00
|
|
|
private lazy var postponingBlock: PostponingBlock = {
|
|
|
|
PostponingBlock(delayInterval: 1.0) {
|
|
|
|
Task {
|
|
|
|
try? await self.fetchUnreadCounts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
private var fetchUnreadCountsTask: Task<Void, Never>?
|
2017-11-20 00:40:02 +01:00
|
|
|
private let delegate: SmartFeedDelegate
|
2019-05-22 22:08:22 +02:00
|
|
|
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) {
|
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
|
|
|
}
|
|
|
|
|
2024-03-25 02:49:39 +01:00
|
|
|
@MainActor @objc func unreadCountDidChange(_ note: Notification) {
|
2019-05-07 00:46:41 +02:00
|
|
|
if note.object is AppDelegate {
|
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
|
|
|
|
2024-06-10 07:10:20 +02:00
|
|
|
@MainActor func fetchUnreadCounts() async throws {
|
2024-03-25 02:49:39 +01:00
|
|
|
|
2019-11-02 01:26:32 +01:00
|
|
|
let activeAccounts = AccountManager.shared.activeAccounts
|
2024-03-25 02:49:39 +01:00
|
|
|
|
2019-11-02 01:26:32 +01:00
|
|
|
// Remove any accounts that are no longer active or have been deleted
|
|
|
|
let activeAccountIDs = activeAccounts.map { $0.accountID }
|
2024-04-08 02:06:39 +02:00
|
|
|
for accountID in unreadCounts.keys {
|
2019-11-02 01:26:32 +01:00
|
|
|
if !activeAccountIDs.contains(accountID) {
|
|
|
|
unreadCounts.removeValue(forKey: accountID)
|
|
|
|
}
|
|
|
|
}
|
2024-03-25 02:49:39 +01:00
|
|
|
|
2019-11-02 01:26:32 +01:00
|
|
|
if activeAccounts.isEmpty {
|
|
|
|
updateUnreadCount()
|
2024-03-25 02:49:39 +01:00
|
|
|
return
|
|
|
|
}
|
2024-06-10 07:10:20 +02:00
|
|
|
|
|
|
|
for account in activeAccounts {
|
|
|
|
await fetchUnreadCount(for: account)
|
2019-11-02 01:26:32 +01:00
|
|
|
}
|
2018-02-18 00:15:26 +01:00
|
|
|
}
|
2018-02-10 22:22:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-20 00:40:02 +01:00
|
|
|
private extension SmartFeed {
|
2017-11-19 22:57:42 +01:00
|
|
|
|
2024-03-25 02:49:39 +01:00
|
|
|
@MainActor func queueFetchUnreadCounts() {
|
2024-06-10 07:10:20 +02:00
|
|
|
|
|
|
|
postponingBlock.runInFuture()
|
2018-02-18 00:15:26 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
2024-06-10 07:10:20 +02:00
|
|
|
|