2017-07-03 19:40:48 +02:00
|
|
|
//
|
2018-07-24 03:29:08 +02:00
|
|
|
// ArticlesDatabase.swift
|
2017-07-03 19:40:48 +02:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 7/20/15.
|
|
|
|
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import RSDatabase
|
2017-07-03 20:20:14 +02:00
|
|
|
import RSParser
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-09-16 19:38:54 +02:00
|
|
|
// This file and UnreadCountDictionary are the entirety of the public API for Database.framework.
|
|
|
|
// Everything else is implementation.
|
|
|
|
|
2017-08-23 22:23:12 +02:00
|
|
|
public typealias ArticleResultBlock = (Set<Article>) -> Void
|
2017-09-16 19:21:39 +02:00
|
|
|
public typealias UnreadCountCompletionBlock = (UnreadCountDictionary) -> Void
|
2017-09-16 19:38:54 +02:00
|
|
|
public typealias UpdateArticlesWithFeedCompletionBlock = (Set<Article>?, Set<Article>?) -> Void //newArticles, updatedArticles
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-24 03:29:08 +02:00
|
|
|
public final class ArticlesDatabase {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
private let accountID: String
|
2017-08-21 22:31:14 +02:00
|
|
|
private let articlesTable: ArticlesTable
|
2017-08-27 00:37:15 +02:00
|
|
|
|
2017-09-18 02:03:58 +02:00
|
|
|
public init(databaseFilePath: String, accountID: String) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-09-10 03:46:58 +02:00
|
|
|
self.accountID = accountID
|
2017-09-16 19:38:54 +02:00
|
|
|
|
2017-09-18 02:03:58 +02:00
|
|
|
let queue = RSDatabaseQueue(filepath: databaseFilePath, excludeFromBackup: false)
|
2017-09-10 03:46:58 +02:00
|
|
|
self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue)
|
2017-08-21 00:56:58 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")!
|
2017-07-03 19:40:48 +02:00
|
|
|
let createStatements = try! NSString(contentsOfFile: createStatementsPath, encoding: String.Encoding.utf8.rawValue)
|
|
|
|
queue.createTables(usingStatements: createStatements as String)
|
2017-12-19 03:20:13 +01:00
|
|
|
queue.update { (database) in
|
|
|
|
database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;")
|
|
|
|
}
|
2017-07-03 19:40:48 +02:00
|
|
|
queue.vacuumIfNeeded()
|
|
|
|
}
|
|
|
|
|
2017-08-21 06:23:17 +02:00
|
|
|
// MARK: - Fetching Articles
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchArticles(for feedID: String) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
return articlesTable.fetchArticles(feedID)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchArticlesAsync(for feedID: String, _ resultBlock: @escaping ArticleResultBlock) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
articlesTable.fetchArticlesAsync(feedID, withLimits: true, resultBlock)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchUnreadArticles(for feedIDs: Set<String>) -> Set<Article> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
return articlesTable.fetchUnreadArticles(for: feedIDs)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
2017-08-21 06:23:17 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchTodayArticles(for feedIDs: Set<String>) -> Set<Article> {
|
2018-02-11 02:37:47 +01:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
return articlesTable.fetchTodayArticles(for: feedIDs)
|
2018-02-11 02:37:47 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchStarredArticles(for feedIDs: Set<String>) -> Set<Article> {
|
2018-02-11 21:07:55 +01:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
return articlesTable.fetchStarredArticles(for: feedIDs)
|
2018-02-11 21:07:55 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 06:23:17 +02:00
|
|
|
// MARK: - Unread Counts
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchUnreadCounts(for feedIDs: Set<String>, _ completion: @escaping UnreadCountCompletionBlock) {
|
2017-12-03 20:57:53 +01:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
articlesTable.fetchUnreadCounts(feedIDs, completion)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchUnreadCount(for feedIDs: Set<String>, since: Date, callback: @escaping (Int) -> Void) {
|
2017-11-19 21:44:17 +01:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
articlesTable.fetchUnreadCount(feedIDs, since, callback)
|
2017-11-19 21:44:17 +01:00
|
|
|
}
|
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func fetchStarredAndUnreadCount(for feedIDs: Set<String>, callback: @escaping (Int) -> Void) {
|
2017-11-20 00:40:02 +01:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
articlesTable.fetchStarredAndUnreadCount(feedIDs, callback)
|
2017-11-20 00:40:02 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 20:57:53 +01:00
|
|
|
public func fetchAllNonZeroUnreadCounts(_ completion: @escaping UnreadCountCompletionBlock) {
|
|
|
|
|
|
|
|
articlesTable.fetchAllUnreadCounts(completion)
|
|
|
|
}
|
|
|
|
|
2017-09-16 19:38:54 +02:00
|
|
|
// MARK: - Saving and Updating Articles
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
public func update(feedID: String, parsedFeed: ParsedFeed, completion: @escaping UpdateArticlesWithFeedCompletionBlock) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2018-07-28 21:16:14 +02:00
|
|
|
return articlesTable.update(feedID, parsedFeed, completion)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-08-21 06:23:17 +02:00
|
|
|
// MARK: - Status
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
public func mark(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<ArticleStatus>? {
|
2017-09-01 22:31:27 +02:00
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
return articlesTable.mark(articles, statusKey, flag)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
2017-11-20 01:28:26 +01:00
|
|
|
|
|
|
|
public func markEverywhereAsRead() {
|
|
|
|
|
|
|
|
articlesTable.markEverywhereAsRead()
|
|
|
|
}
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|