mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Continue converting ArticlesDatabase to async/await.
This commit is contained in:
@@ -181,14 +181,8 @@ public typealias ArticleStatusesResultBlock = (ArticleStatusesResult) -> Void
|
||||
operationQueue.add(operation)
|
||||
}
|
||||
|
||||
/// Fetch unread count for a single feed.
|
||||
public func fetchUnreadCount(_ feedID: String, _ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
let operation = FetchFeedUnreadCountOperation(feedID: feedID, databaseQueue: queue, cutoffDate: articlesTable.articleCutoffDate)
|
||||
operation.completionBlock = { operation in
|
||||
let fetchOperation = operation as! FetchFeedUnreadCountOperation
|
||||
completion(fetchOperation.result)
|
||||
}
|
||||
operationQueue.add(operation)
|
||||
public func unreadCountForFeed(_ feedID: String) async throws -> Int {
|
||||
try await articlesTable.unreadCountForFeedID(feedID)
|
||||
}
|
||||
|
||||
/// Fetch non-zero unread counts for given feedIDs.
|
||||
|
||||
@@ -396,6 +396,30 @@ final class ArticlesTable: DatabaseTable {
|
||||
|
||||
// MARK: - Unread Counts
|
||||
|
||||
func r async throws -> Int {
|
||||
|
||||
try await withCheckedThrowingContinuation { continuation in
|
||||
queue.runInDatabase { databaseResult in
|
||||
|
||||
func readUnreadCount(_ database: FMDatabase) -> Int {
|
||||
let sql = "select count(*) from articles natural join statuses where feedID=? and read=0;"
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: [feedID]) else {
|
||||
return 0
|
||||
}
|
||||
return self.numberWithCountResultSet(resultSet)
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let unreadCount = readUnreadCount(database)
|
||||
continuation.resume(returning: unreadCount)
|
||||
case .failure(let databaseError):
|
||||
continuation.resume(throwing: databaseError)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fetchUnreadCount(_ feedIDs: Set<String>, _ since: Date, _ completion: @escaping SingleUnreadCountCompletionBlock) {
|
||||
// Get unread count for today, for instance.
|
||||
if feedIDs.isEmpty {
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
//
|
||||
// FetchFeedUnreadCountOperation.swift
|
||||
// ArticlesDatabase
|
||||
//
|
||||
// Created by Brent Simmons on 1/27/20.
|
||||
// Copyright © 2020 Ranchero Software. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import RSCore
|
||||
import RSDatabase
|
||||
import RSDatabaseObjC
|
||||
|
||||
/// Fetch the unread count for a single feed.
|
||||
public final class FetchFeedUnreadCountOperation: MainThreadOperation {
|
||||
|
||||
var result: SingleUnreadCountResult = .failure(.isSuspended)
|
||||
|
||||
// MainThreadOperation
|
||||
public var isCanceled = false
|
||||
public var id: Int?
|
||||
public weak var operationDelegate: MainThreadOperationDelegate?
|
||||
public var name: String? = "FetchFeedUnreadCountOperation"
|
||||
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
||||
|
||||
private let queue: DatabaseQueue
|
||||
private let cutoffDate: Date
|
||||
private let feedID: String
|
||||
|
||||
init(feedID: String, databaseQueue: DatabaseQueue, cutoffDate: Date) {
|
||||
self.feedID = feedID
|
||||
self.queue = databaseQueue
|
||||
self.cutoffDate = cutoffDate
|
||||
}
|
||||
|
||||
public func run() {
|
||||
queue.runInDatabase { databaseResult in
|
||||
if self.isCanceled {
|
||||
self.informOperationDelegateOfCompletion()
|
||||
return
|
||||
}
|
||||
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
self.fetchUnreadCount(database)
|
||||
case .failure:
|
||||
self.informOperationDelegateOfCompletion()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension FetchFeedUnreadCountOperation {
|
||||
|
||||
func fetchUnreadCount(_ database: FMDatabase) {
|
||||
let sql = "select count(*) from articles natural join statuses where feedID=? and read=0;"
|
||||
|
||||
guard let resultSet = database.executeQuery(sql, withArgumentsIn: [feedID]) else {
|
||||
informOperationDelegateOfCompletion()
|
||||
return
|
||||
}
|
||||
if isCanceled {
|
||||
informOperationDelegateOfCompletion()
|
||||
return
|
||||
}
|
||||
|
||||
if resultSet.next() {
|
||||
let unreadCount = resultSet.long(forColumnIndex: 0)
|
||||
result = .success(unreadCount)
|
||||
}
|
||||
resultSet.close()
|
||||
|
||||
informOperationDelegateOfCompletion()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user