2017-07-03 10:40:48 -07:00
//
2018-07-23 18:29:08 -07:00
// A r t i c l e s D a t a b a s e . s w i f t
2018-08-28 22:18:24 -07:00
// N e t N e w s W i r e
2017-07-03 10:40:48 -07:00
//
// C r e a t e d b y B r e n t S i m m o n s o n 7 / 2 0 / 1 5 .
// C o p y r i g h t © 2 0 1 5 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import Foundation
import RSCore
import RSDatabase
2017-07-03 11:20:14 -07:00
import RSParser
2018-07-23 18:29:08 -07:00
import Articles
2017-07-03 10:40:48 -07:00
2017-09-16 10:38:54 -07:00
// T h i s f i l e a n d U n r e a d C o u n t D i c t i o n a r y a r e t h e e n t i r e t y o f t h e p u b l i c A P I f o r D a t a b a s e . f r a m e w o r k .
// E v e r y t h i n g e l s e i s i m p l e m e n t a t i o n .
2017-08-23 13:23:12 -07:00
public typealias ArticleResultBlock = ( Set < Article > ) -> Void
2017-09-16 10:21:39 -07:00
public typealias UnreadCountCompletionBlock = ( UnreadCountDictionary ) -> Void
2017-09-16 10:38:54 -07:00
public typealias UpdateArticlesWithFeedCompletionBlock = ( Set < Article > ? , Set < Article > ? ) -> Void // n e w A r t i c l e s , u p d a t e d A r t i c l e s
2017-07-03 10:40:48 -07:00
2018-07-23 18:29:08 -07:00
public final class ArticlesDatabase {
2017-07-03 10:40:48 -07:00
2017-09-09 18:46:58 -07:00
private let accountID : String
2017-08-21 13:31:14 -07:00
private let articlesTable : ArticlesTable
2017-08-26 15:37:15 -07:00
2017-09-17 17:03:58 -07:00
public init ( databaseFilePath : String , accountID : String ) {
2017-07-03 10:40:48 -07:00
2017-09-09 18:46:58 -07:00
self . accountID = accountID
2017-09-16 10:38:54 -07:00
2017-09-17 17:03:58 -07:00
let queue = RSDatabaseQueue ( filepath : databaseFilePath , excludeFromBackup : false )
2017-09-09 18:46:58 -07:00
self . articlesTable = ArticlesTable ( name : DatabaseTableName . articles , accountID : accountID , queue : queue )
2017-08-20 15:56:58 -07:00
2017-07-03 11:20:14 -07:00
let createStatementsPath = Bundle ( for : type ( of : self ) ) . path ( forResource : " CreateStatements " , ofType : " sql " ) !
2017-07-03 10:40:48 -07:00
let createStatements = try ! NSString ( contentsOfFile : createStatementsPath , encoding : String . Encoding . utf8 . rawValue )
queue . createTables ( usingStatements : createStatements as String )
2017-12-18 18:20:13 -08:00
queue . update { ( database ) in
2018-09-25 22:48:47 -07:00
database . executeStatements ( " DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;DROP INDEX if EXISTS articles_feedID_index;DROP INDEX if EXISTS statuses_read_index; " )
2017-12-18 18:20:13 -08:00
}
2017-07-03 10:40:48 -07:00
queue . vacuumIfNeeded ( )
}
2017-08-20 21:23:17 -07:00
// MARK: - F e t c h i n g A r t i c l e s
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
public func fetchArticles ( for feedID : String ) -> Set < Article > {
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
return articlesTable . fetchArticles ( feedID )
2017-07-03 10:40:48 -07:00
}
2018-07-28 12:16:14 -07:00
public func fetchArticlesAsync ( for feedID : String , _ resultBlock : @ escaping ArticleResultBlock ) {
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
articlesTable . fetchArticlesAsync ( feedID , withLimits : true , resultBlock )
2017-07-03 10:40:48 -07:00
}
2018-07-28 12:16:14 -07:00
public func fetchUnreadArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
return articlesTable . fetchUnreadArticles ( for : feedIDs )
2017-07-03 10:40:48 -07:00
}
2017-08-20 21:23:17 -07:00
2018-07-28 12:16:14 -07:00
public func fetchTodayArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2018-02-10 17:37:47 -08:00
2018-07-28 12:16:14 -07:00
return articlesTable . fetchTodayArticles ( for : feedIDs )
2018-02-10 17:37:47 -08:00
}
2018-07-28 12:16:14 -07:00
public func fetchStarredArticles ( for feedIDs : Set < String > ) -> Set < Article > {
2018-02-11 12:07:55 -08:00
2018-07-28 12:16:14 -07:00
return articlesTable . fetchStarredArticles ( for : feedIDs )
2018-02-11 12:07:55 -08:00
}
2017-08-20 21:23:17 -07:00
// MARK: - U n r e a d C o u n t s
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
public func fetchUnreadCounts ( for feedIDs : Set < String > , _ completion : @ escaping UnreadCountCompletionBlock ) {
2017-12-03 11:57:53 -08:00
2018-07-28 12:16:14 -07:00
articlesTable . fetchUnreadCounts ( feedIDs , completion )
2017-07-03 10:40:48 -07:00
}
2018-07-28 12:16:14 -07:00
public func fetchUnreadCount ( for feedIDs : Set < String > , since : Date , callback : @ escaping ( Int ) -> Void ) {
2017-11-19 12:44:17 -08:00
2018-07-28 12:16:14 -07:00
articlesTable . fetchUnreadCount ( feedIDs , since , callback )
2017-11-19 12:44:17 -08:00
}
2018-07-28 12:16:14 -07:00
public func fetchStarredAndUnreadCount ( for feedIDs : Set < String > , callback : @ escaping ( Int ) -> Void ) {
2017-11-19 15:40:02 -08:00
2018-07-28 12:16:14 -07:00
articlesTable . fetchStarredAndUnreadCount ( feedIDs , callback )
2017-11-19 15:40:02 -08:00
}
2017-12-03 11:57:53 -08:00
public func fetchAllNonZeroUnreadCounts ( _ completion : @ escaping UnreadCountCompletionBlock ) {
articlesTable . fetchAllUnreadCounts ( completion )
}
2017-09-16 10:38:54 -07:00
// MARK: - S a v i n g a n d U p d a t i n g A r t i c l e s
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
public func update ( feedID : String , parsedFeed : ParsedFeed , completion : @ escaping UpdateArticlesWithFeedCompletionBlock ) {
2017-07-03 10:40:48 -07:00
2018-07-28 12:16:14 -07:00
return articlesTable . update ( feedID , parsedFeed , completion )
2017-07-03 10:40:48 -07:00
}
2017-08-20 21:23:17 -07:00
// MARK: - S t a t u s
2017-07-03 10:40:48 -07:00
2017-10-08 21:06:25 -07:00
public func mark ( _ articles : Set < Article > , statusKey : ArticleStatus . Key , flag : Bool ) -> Set < ArticleStatus > ? {
2017-09-01 13:31:27 -07:00
2017-10-08 21:06:25 -07:00
return articlesTable . mark ( articles , statusKey , flag )
2017-07-03 10:40:48 -07:00
}
2017-11-19 16:28:26 -08:00
public func markEverywhereAsRead ( ) {
articlesTable . markEverywhereAsRead ( )
}
2017-07-03 10:40:48 -07:00
}