2020-02-02 00:00:59 +01:00
//
// F e t c h U n r e a d C o u n t s F o r F e e d s O p e r a t i o n . s w i f t
// A r t i c l e s D a t a b a s e
//
// C r e a t e d b y B r e n t S i m m o n s o n 2 / 1 / 2 0 .
// C o p y r i g h t © 2 0 2 0 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import Foundation
import RSCore
import RSDatabase
// / F e t c h t h e u n r e a d c o u n t s f o r a n u m b e r o f f e e d s .
public final class FetchUnreadCountsForFeedsOperation : MainThreadOperation {
2020-02-06 07:17:32 +01:00
var result : UnreadCountDictionaryCompletionResult = . failure ( . isSuspended )
2020-02-02 00:00:59 +01:00
// M a i n T h r e a d O p e r a t i o n
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 ? = " FetchUnreadCountsForFeedsOperation "
2020-02-02 00:00:59 +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 webFeedIDs : Set < String >
2020-02-02 00:00:59 +01:00
2020-02-06 07:17:32 +01:00
init ( webFeedIDs : Set < String > , databaseQueue : DatabaseQueue , cutoffDate : Date ) {
self . webFeedIDs = webFeedIDs
2020-02-02 00:00:59 +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 . fetchUnreadCounts ( database )
case . failure :
self . informOperationDelegateOfCompletion ( )
}
}
}
}
private extension FetchUnreadCountsForFeedsOperation {
func fetchUnreadCounts ( _ database : FMDatabase ) {
2020-02-06 07:17:32 +01:00
let placeholders = NSString . rs_SQLValueList ( withPlaceholders : UInt ( webFeedIDs . count ) ) !
2020-02-02 00:00:59 +01:00
let sql = " select distinct feedID, count(*) from articles natural join statuses where feedID in \( placeholders ) and read=0 and userDeleted=0 and (starred=1 or dateArrived>?) group by feedID; "
var parameters = [ Any ] ( )
2020-02-06 07:17:32 +01:00
parameters += Array ( webFeedIDs ) as [ Any ]
2020-02-02 00:00:59 +01:00
parameters += [ cutoffDate ] as [ Any ]
guard let resultSet = database . executeQuery ( sql , withArgumentsIn : parameters ) else {
informOperationDelegateOfCompletion ( )
return
}
if isCanceled {
2020-02-06 07:17:32 +01:00
resultSet . close ( )
2020-02-02 00:00:59 +01:00
informOperationDelegateOfCompletion ( )
return
}
2020-02-06 07:17:32 +01:00
var unreadCountDictionary = UnreadCountDictionary ( )
2020-02-02 00:00:59 +01:00
while resultSet . next ( ) {
if isCanceled {
resultSet . close ( )
informOperationDelegateOfCompletion ( )
return
}
let unreadCount = resultSet . long ( forColumnIndex : 1 )
if let webFeedID = resultSet . string ( forColumnIndex : 0 ) {
2020-02-06 07:17:32 +01:00
unreadCountDictionary [ webFeedID ] = unreadCount
2020-02-02 00:00:59 +01:00
}
}
resultSet . close ( )
2020-02-06 07:17:32 +01:00
result = . success ( unreadCountDictionary )
2020-02-02 00:00:59 +01:00
informOperationDelegateOfCompletion ( )
}
}