Fix numerous warnings.

This commit is contained in:
Brent Simmons 2024-03-24 18:49:39 -07:00
parent c01e2d1682
commit 9a6314f2d9
10 changed files with 74 additions and 42 deletions

View File

@ -747,10 +747,21 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
return try await unreadArticles(feeds: feeds)
}
public func unreadCountForToday() async throws -> Int {
try await database.unreadCountForToday(feedIDs: allFeedIDs()) ?? 0
}
public func fetchUnreadCountForToday(_ completion: @escaping SingleUnreadCountCompletionBlock) {
database.fetchUnreadCountForToday(for: flattenedFeeds().feedIDs(), completion: completion)
}
public func unreadCountForStarredArticles() async throws -> Int {
try await database.starredAndUnreadCount(feedIDs: allFeedIDs()) ?? 0
}
public func fetchUnreadCountForStarredArticles(_ completion: @escaping SingleUnreadCountCompletionBlock) {
database.fetchStarredAndUnreadCount(for: flattenedFeeds().feedIDs(), completion: completion)
}

View File

@ -27,9 +27,9 @@ extension RSImage {
return maxIconSize * maxScreenScale
}()
static func scaledForIcon(_ data: Data) async -> RSImage? {
static func scaledForIcon(_ data: Data, imageResultBlock: @escaping ImageResultBlock) {
IconScalerQueue.shared.scaledForIcon(data, imageResultBlock)
await IconScalerQueue.shared.scaledForIcon(data)
}
static func scaledForIcon(_ data: Data) -> RSImage? {
@ -60,11 +60,19 @@ private final class IconScalerQueue: @unchecked Sendable {
return q
}()
func scaledForIcon(_ data: Data, _ imageResultBlock: @escaping ImageResultBlock) {
private func scaledForIcon(_ data: Data, _ imageResultBlock: @escaping ImageResultBlock) {
queue.async {
let image = RSImage.scaledForIcon(data)
DispatchQueue.main.async {
imageResultBlock(image)
imageResultBlock(image)
}
}
func scaledForIcon(_ data: Data) async -> RSImage? {
await withCheckedContinuation { continuation in
self.scaledForIcon(data) { image in
continuation.resume(returning: image)
}
}
}

View File

@ -68,8 +68,9 @@ final class AuthorAvatarDownloader {
private extension AuthorAvatarDownloader {
func scaleAndCacheImageData(_ imageData: Data, _ avatarURL: String) {
RSImage.scaledForIcon(imageData) { (image) in
if let image = image {
Task { @MainActor in
if let image = await RSImage.scaledForIcon(imageData) {
self.handleImageDidBecomeAvailable(avatarURL, image)
}
}

View File

@ -180,7 +180,11 @@ private extension FeedIconDownloader {
imageResultBlock(nil)
return
}
RSImage.scaledForIcon(imageData, imageResultBlock: imageResultBlock)
Task {
let image = await RSImage.scaledForIcon(imageData)
imageResultBlock(image)
}
}
func postFeedIconDidBecomeAvailableNotification(_ feed: Feed) {

View File

@ -31,8 +31,9 @@ struct SearchFeedDelegate: SmartFeedDelegate {
self.fetchType = .search(searchString)
}
func fetchUnreadCount(for: Account, completion: @escaping SingleUnreadCountCompletionBlock) {
// TODO: after 5.0
func unreadCount(account: Account) async -> Int {
return 0
}
}

View File

@ -31,7 +31,8 @@ struct SearchTimelineFeedDelegate: SmartFeedDelegate {
self.fetchType = .searchWithArticleIDs(searchString, articleIDs)
}
func fetchUnreadCount(for: Account, completion: @escaping SingleUnreadCountCompletionBlock) {
// TODO: after 5.0
func unreadCount(account: Account) async -> Int {
return 0
}
}

View File

@ -50,21 +50,22 @@ final class SmartFeed: PseudoFeed {
private let delegate: SmartFeedDelegate
private var unreadCounts = [String: Int]()
init(delegate: SmartFeedDelegate) {
@MainActor init(delegate: SmartFeedDelegate) {
self.delegate = delegate
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
queueFetchUnreadCounts() // Fetch unread count at startup
}
@objc func unreadCountDidChange(_ note: Notification) {
@MainActor @objc func unreadCountDidChange(_ note: Notification) {
if note.object is AppDelegate {
queueFetchUnreadCounts()
}
}
@objc @MainActor func fetchUnreadCounts() {
@MainActor @objc func fetchUnreadCounts() {
let activeAccounts = AccountManager.shared.activeAccounts
// Remove any accounts that are no longer active or have been deleted
let activeAccountIDs = activeAccounts.map { $0.accountID }
unreadCounts.keys.forEach { accountID in
@ -72,14 +73,18 @@ final class SmartFeed: PseudoFeed {
unreadCounts.removeValue(forKey: accountID)
}
}
if activeAccounts.isEmpty {
updateUnreadCount()
} else {
activeAccounts.forEach { self.fetchUnreadCount(for: $0) }
return
}
Task { @MainActor in
for account in activeAccounts {
await fetchUnreadCount(for: account)
}
}
}
}
extension SmartFeed: ArticleFetcher {
@ -107,31 +112,25 @@ extension SmartFeed: ArticleFetcher {
private extension SmartFeed {
func queueFetchUnreadCounts() {
Task { @MainActor in
CoalescingQueue.standard.add(self, #selector(fetchUnreadCounts))
}
@MainActor func queueFetchUnreadCounts() {
CoalescingQueue.standard.add(self, #selector(fetchUnreadCounts))
}
func fetchUnreadCount(for account: Account) {
delegate.fetchUnreadCount(for: account) { singleUnreadCountResult in
@MainActor func fetchUnreadCount(for account: Account) async {
MainActor.assumeIsolated {
guard let accountUnreadCount = try? singleUnreadCountResult.get() else {
return
}
self.unreadCounts[account.accountID] = accountUnreadCount
self.updateUnreadCount()
}
}
let unreadCount = await delegate.unreadCount(account: account)
unreadCounts[account.accountID] = unreadCount
updateUnreadCount()
}
@MainActor func updateUnreadCount() {
unreadCount = AccountManager.shared.activeAccounts.reduce(0) { (result, account) -> Int in
if let oneUnreadCount = unreadCounts[account.accountID] {
return result + oneUnreadCount
}
return result
var unread = 0
for account in AccountManager.shared.activeAccounts {
unread = unread + (unreadCounts[account.accountID] ?? 0)
}
unreadCount = unread
}
}

View File

@ -17,7 +17,8 @@ protocol SmartFeedDelegate: SidebarItemIdentifiable, DisplayNameProvider, Articl
var fetchType: FetchType { get }
func fetchUnreadCount(for: Account, completion: @escaping SingleUnreadCountCompletionBlock)
func unreadCount(account: Account) async -> Int
// func fetchUnreadCount(for: Account, completion: @escaping SingleUnreadCountCompletionBlock)
}
extension SmartFeedDelegate {

View File

@ -25,6 +25,11 @@ struct StarredFeedDelegate: SmartFeedDelegate {
return AppAssets.starredFeedImage
}
func unreadCount(account: Account) async -> Int {
(try? await account.unreadCountForStarredArticles()) ?? 0
}
func fetchUnreadCount(for account: Account, completion: @escaping SingleUnreadCountCompletionBlock) {
account.fetchUnreadCountForStarredArticles(completion)
}

View File

@ -23,8 +23,9 @@ struct TodayFeedDelegate: SmartFeedDelegate {
return AppAssets.todayFeedImage
}
func fetchUnreadCount(for account: Account, completion: @escaping SingleUnreadCountCompletionBlock) {
account.fetchUnreadCountForToday(completion)
func unreadCount(account: Account) async -> Int {
(try? await account.unreadCountForToday()) ?? 0
}
}