2020-01-28 04:55:10 +01:00
|
|
|
//
|
|
|
|
// FetchAllUnreadCountsOperation.swift
|
|
|
|
// ArticlesDatabase
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/26/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import RSDatabase
|
|
|
|
|
2020-01-28 08:00:48 +01:00
|
|
|
public final class FetchAllUnreadCountsOperation: MainThreadOperation {
|
2020-01-28 04:55:10 +01:00
|
|
|
|
2020-02-06 07:17:32 +01:00
|
|
|
var result: UnreadCountDictionaryCompletionResult = .failure(.isSuspended)
|
2020-01-28 04:55:10 +01:00
|
|
|
|
|
|
|
// MainThreadOperation
|
2020-01-28 08:00:48 +01:00
|
|
|
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? = "FetchAllUnreadCountsOperation"
|
2020-01-28 08:00:48 +01:00
|
|
|
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
|
2020-01-28 04:55:10 +01:00
|
|
|
|
|
|
|
private let queue: DatabaseQueue
|
|
|
|
|
2020-04-19 05:18:32 +02:00
|
|
|
init(databaseQueue: DatabaseQueue) {
|
2020-01-28 04:55:10 +01:00
|
|
|
self.queue = databaseQueue
|
|
|
|
}
|
|
|
|
|
2020-01-28 08:00:48 +01:00
|
|
|
public func run() {
|
2020-01-28 04:55:10 +01:00
|
|
|
queue.runInDatabase { databaseResult in
|
|
|
|
if self.isCanceled {
|
|
|
|
self.informOperationDelegateOfCompletion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch databaseResult {
|
|
|
|
case .success(let database):
|
|
|
|
self.fetchUnreadCounts(database)
|
|
|
|
case .failure:
|
|
|
|
self.informOperationDelegateOfCompletion()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension FetchAllUnreadCountsOperation {
|
|
|
|
|
|
|
|
func fetchUnreadCounts(_ database: FMDatabase) {
|
2020-04-19 05:18:32 +02:00
|
|
|
let sql = "select distinct feedID, count(*) from articles natural join statuses where read=0 group by feedID;"
|
2020-01-28 04:55:10 +01:00
|
|
|
|
2020-04-19 05:18:32 +02:00
|
|
|
guard let resultSet = database.executeQuery(sql, withArgumentsIn: nil) else {
|
2020-01-28 04:55:10 +01:00
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-06 06:18:29 +01:00
|
|
|
var unreadCountDictionary = UnreadCountDictionary()
|
2020-01-28 04:55:10 +01:00
|
|
|
while resultSet.next() {
|
|
|
|
if isCanceled {
|
2020-01-30 07:45:55 +01:00
|
|
|
resultSet.close()
|
2020-01-28 04:55:10 +01:00
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
let unreadCount = resultSet.long(forColumnIndex: 1)
|
|
|
|
if let webFeedID = resultSet.string(forColumnIndex: 0) {
|
2020-02-06 06:18:29 +01:00
|
|
|
unreadCountDictionary[webFeedID] = unreadCount
|
2020-01-28 04:55:10 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 07:45:55 +01:00
|
|
|
resultSet.close()
|
2020-01-28 04:55:10 +01:00
|
|
|
|
2020-02-06 06:18:29 +01:00
|
|
|
result = .success(unreadCountDictionary)
|
2020-01-28 04:55:10 +01:00
|
|
|
informOperationDelegateOfCompletion()
|
|
|
|
}
|
|
|
|
}
|