NetNewsWire/Frameworks/Database/Database.swift

99 lines
2.9 KiB
Swift
Raw Normal View History

//
// 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
import RSParser
import Data
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.
public typealias ArticleResultBlock = (Set<Article>) -> Void
public typealias UnreadCountCompletionBlock = (UnreadCountDictionary) -> Void
2017-09-16 19:38:54 +02:00
public typealias UpdateArticlesWithFeedCompletionBlock = (Set<Article>?, Set<Article>?) -> Void //newArticles, updatedArticles
public final class Database {
private let accountID: String
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) {
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)
self.articlesTable = ArticlesTable(name: DatabaseTableName.articles, accountID: accountID, queue: queue)
2017-08-21 00:56:58 +02:00
let createStatementsPath = Bundle(for: type(of: self)).path(forResource: "CreateStatements", ofType: "sql")!
let createStatements = try! NSString(contentsOfFile: createStatementsPath, encoding: String.Encoding.utf8.rawValue)
queue.createTables(usingStatements: createStatements as String)
queue.vacuumIfNeeded()
}
// MARK: - Fetching Articles
public func fetchArticles(for feed: Feed) -> Set<Article> {
return articlesTable.fetchArticles(feed)
}
public func fetchArticlesAsync(for feed: Feed, _ resultBlock: @escaping ArticleResultBlock) {
articlesTable.fetchArticlesAsync(feed, withLimits: true, resultBlock)
}
public func fetchUnreadArticles(for feeds: Set<Feed>) -> Set<Article> {
return articlesTable.fetchUnreadArticles(for: feeds)
}
// MARK: - Unread Counts
2017-09-01 22:31:27 +02:00
public func fetchUnreadCounts(for feeds: Set<Feed>, _ completion: @escaping UnreadCountCompletionBlock) {
2017-09-01 22:31:27 +02:00
articlesTable.fetchUnreadCounts(feeds, completion)
}
public func fetchUnreadCount(for feeds: Set<Feed>, since: Date, callback: @escaping (Int) -> Void) {
articlesTable.fetchUnreadCount(feeds, since, callback)
}
public func fetchStarredAndUnreadCount(for feeds: Set<Feed>, callback: @escaping (Int) -> Void) {
articlesTable.fetchStarredAndUnreadCount(feeds, callback)
}
public func fetchAllNonZeroUnreadCounts(_ completion: @escaping UnreadCountCompletionBlock) {
articlesTable.fetchAllUnreadCounts(completion)
}
2017-09-16 19:38:54 +02:00
// MARK: - Saving and Updating Articles
public func update(feed: Feed, parsedFeed: ParsedFeed, completion: @escaping UpdateArticlesWithFeedCompletionBlock) {
return articlesTable.update(feed, parsedFeed, completion)
}
// MARK: - Status
public func mark(_ articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) -> Set<ArticleStatus>? {
2017-09-01 22:31:27 +02:00
return articlesTable.mark(articles, statusKey, flag)
}
2017-11-20 01:28:26 +01:00
public func markEverywhereAsRead() {
articlesTable.markEverywhereAsRead()
}
}