2020-01-30 07:47:01 +01:00
|
|
|
//
|
|
|
|
// FetchFeedUnreadCountOperation.swift
|
|
|
|
// ArticlesDatabase
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/27/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import RSDatabase
|
|
|
|
|
|
|
|
/// Fetch the unread count for a single feed.
|
|
|
|
public final class FetchFeedUnreadCountOperation: MainThreadOperation {
|
|
|
|
|
2020-02-06 07:17:32 +01:00
|
|
|
var result: SingleUnreadCountResult = .failure(.isSuspended)
|
2020-01-30 07:47:01 +01:00
|
|
|
|
|
|
|
// MainThreadOperation
|
|
|
|
public var isCanceled = false
|
|
|
|
public var id: Int?
|
|
|
|
public weak var operationDelegate: MainThreadOperationDelegate?
|
2020-02-06 06:18:29 +01:00
|
|
|
public var name: String? = "FetchFeedUnreadCountOperation"
|
2020-01-30 07:47:01 +01:00
|
|
|
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
|
|
|
|
|
|
|
private let queue: DatabaseQueue
|
|
|
|
private let cutoffDate: Date
|
2020-02-06 07:17:32 +01:00
|
|
|
private let webFeedID: String
|
2020-01-30 07:47:01 +01:00
|
|
|
|
2020-02-06 07:17:32 +01:00
|
|
|
init(webFeedID: String, databaseQueue: DatabaseQueue, cutoffDate: Date) {
|
|
|
|
self.webFeedID = webFeedID
|
2020-01-30 07:47:01 +01:00
|
|
|
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 and userDeleted=0 and (starred=1 or dateArrived>?);"
|
|
|
|
|
2020-02-06 07:17:32 +01:00
|
|
|
guard let resultSet = database.executeQuery(sql, withArgumentsIn: [webFeedID, cutoffDate]) else {
|
2020-01-30 07:47:01 +01:00
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if isCanceled {
|
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resultSet.next() {
|
2020-02-06 07:17:32 +01:00
|
|
|
let unreadCount = resultSet.long(forColumnIndex: 0)
|
|
|
|
result = .success(unreadCount)
|
2020-01-30 07:47:01 +01:00
|
|
|
}
|
|
|
|
resultSet.close()
|
|
|
|
|
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
}
|
|
|
|
}
|