2017-07-03 10:40:48 -07:00
|
|
|
//
|
|
|
|
// Database.swift
|
|
|
|
// 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 11:20:14 -07:00
|
|
|
import RSParser
|
2017-07-03 10:40:48 -07:00
|
|
|
import Data
|
|
|
|
|
2017-09-16 10:38:54 -07:00
|
|
|
// This file and UnreadCountDictionary are the entirety of the public API for Database.framework.
|
|
|
|
// Everything else is implementation.
|
|
|
|
|
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 //newArticles, updatedArticles
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-08-23 13:23:12 -07:00
|
|
|
public final class Database {
|
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
|
|
|
|
database.executeStatements("DROP TABLE if EXISTS tags;DROP INDEX if EXISTS tags_tagName_index;")
|
|
|
|
}
|
2017-07-03 10:40:48 -07:00
|
|
|
queue.vacuumIfNeeded()
|
|
|
|
}
|
|
|
|
|
2017-08-20 21:23:17 -07:00
|
|
|
// MARK: - Fetching Articles
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-08-23 13:23:12 -07:00
|
|
|
public func fetchArticles(for feed: Feed) -> Set<Article> {
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-08-23 13:23:12 -07:00
|
|
|
return articlesTable.fetchArticles(feed)
|
2017-07-03 10:40:48 -07:00
|
|
|
}
|
|
|
|
|
2017-08-23 13:23:12 -07:00
|
|
|
public func fetchArticlesAsync(for feed: Feed, _ resultBlock: @escaping ArticleResultBlock) {
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-09-04 17:10:02 -07:00
|
|
|
articlesTable.fetchArticlesAsync(feed, withLimits: true, resultBlock)
|
2017-07-03 10:40:48 -07:00
|
|
|
}
|
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
public func fetchUnreadArticles(for feeds: Set<Feed>) -> Set<Article> {
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-09-16 15:25:38 -07:00
|
|
|
return articlesTable.fetchUnreadArticles(for: feeds)
|
2017-07-03 10:40:48 -07:00
|
|
|
}
|
2017-08-20 21:23:17 -07:00
|
|
|
|
|
|
|
// MARK: - Unread Counts
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-09-01 13:31:27 -07:00
|
|
|
public func fetchUnreadCounts(for feeds: Set<Feed>, _ completion: @escaping UnreadCountCompletionBlock) {
|
2017-12-03 11:57:53 -08:00
|
|
|
|
2017-09-01 13:31:27 -07:00
|
|
|
articlesTable.fetchUnreadCounts(feeds, completion)
|
2017-07-03 10:40:48 -07:00
|
|
|
}
|
|
|
|
|
2017-11-19 12:44:17 -08:00
|
|
|
public func fetchUnreadCount(for feeds: Set<Feed>, since: Date, callback: @escaping (Int) -> Void) {
|
|
|
|
|
|
|
|
articlesTable.fetchUnreadCount(feeds, since, callback)
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:40:02 -08:00
|
|
|
public func fetchStarredAndUnreadCount(for feeds: Set<Feed>, callback: @escaping (Int) -> Void) {
|
|
|
|
|
|
|
|
articlesTable.fetchStarredAndUnreadCount(feeds, callback)
|
|
|
|
}
|
|
|
|
|
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: - Saving and Updating Articles
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-09-09 18:46:58 -07:00
|
|
|
public func update(feed: Feed, parsedFeed: ParsedFeed, completion: @escaping UpdateArticlesWithFeedCompletionBlock) {
|
2017-07-03 10:40:48 -07:00
|
|
|
|
2017-08-23 13:23:12 -07:00
|
|
|
return articlesTable.update(feed, parsedFeed, completion)
|
2017-07-03 10:40:48 -07:00
|
|
|
}
|
|
|
|
|
2017-08-20 21:23:17 -07:00
|
|
|
// MARK: - Status
|
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
|
|
|
}
|
|
|
|
|