2017-07-03 20:20:14 +02:00
|
|
|
//
|
2017-07-29 21:08:10 +02:00
|
|
|
// StatusesTable.swift
|
2017-07-03 19:40:48 +02:00
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 5/8/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSCore
|
|
|
|
import RSDatabase
|
2017-07-03 20:20:14 +02:00
|
|
|
import RSParser
|
2017-07-03 19:40:48 +02:00
|
|
|
import Data
|
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
final class StatusesTable: DatabaseTable {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var cachedStatuses = [String: ArticleStatus]()
|
2017-07-29 21:08:10 +02:00
|
|
|
let name: String
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
init(name: String) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-29 21:08:10 +02:00
|
|
|
self.name = name
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
func markArticles(_ articles: Set<Article>, statusKey: String, flag: Bool) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
assertNoMissingStatuses(articles)
|
2017-07-03 20:20:14 +02:00
|
|
|
let statuses = Set(articles.flatMap { $0.status })
|
2017-07-03 19:40:48 +02:00
|
|
|
markArticleStatuses(statuses, statusKey: statusKey, flag: flag)
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func attachCachedStatuses(_ articles: Set<Article>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
articles.forEach { (oneArticle) in
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
if let cachedStatus = cachedStatusForArticleID(oneArticle.articleID) {
|
|
|
|
oneArticle.status = cachedStatus
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
2017-07-04 00:04:31 +02:00
|
|
|
else if let oneArticleStatus = oneArticle.status {
|
2017-07-03 20:20:14 +02:00
|
|
|
cacheStatus(oneArticleStatus)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ensureStatusesForParsedArticles(_ parsedArticles: [ParsedItem], _ callback: @escaping RSVoidCompletionBlock) {
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var articleIDs = Set(parsedArticles.map { $0.articleID })
|
2017-07-03 19:40:48 +02:00
|
|
|
articleIDs = articleIDsMissingStatuses(articleIDs)
|
|
|
|
if articleIDs.isEmpty {
|
|
|
|
callback()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
queue.fetch { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
let statuses = self.fetchStatusesForArticleIDs(articleIDs, database: database)
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
|
|
|
self.cacheStatuses(statuses)
|
|
|
|
|
|
|
|
let newArticleIDs = self.articleIDsMissingStatuses(articleIDs)
|
|
|
|
self.createStatusForNewArticleIDs(newArticleIDs)
|
|
|
|
callback()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func assertNoMissingStatuses(_ articles: Set<Article>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
for oneArticle in articles {
|
|
|
|
if oneArticle.status == nil {
|
|
|
|
assertionFailure("All articles must have a status at this point.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private let statusesTableName = "statuses"
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
private extension StatusesManager {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
// MARK: Marking
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
func markArticleStatuses(_ statuses: Set<ArticleStatus>, statusKey: String, flag: Bool) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
// Ignore the statuses where status.[statusKey] == flag. Update the remainder and save in database.
|
|
|
|
|
|
|
|
var articleIDs = Set<String>()
|
|
|
|
|
|
|
|
statuses.forEach { (oneStatus) in
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
if oneStatus.boolStatus(forKey: statusKey) != flag {
|
|
|
|
oneStatus.setBoolStatus(flag, forKey: statusKey)
|
2017-07-03 19:40:48 +02:00
|
|
|
articleIDs.insert(oneStatus.articleID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !articleIDs.isEmpty {
|
|
|
|
updateArticleStatusesInDatabase(articleIDs, statusKey: statusKey, flag: flag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Fetching
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func fetchStatusesForArticleIDs(_ articleIDs: Set<String>, database: FMDatabase) -> Set<ArticleStatus> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
guard !articleIDs.isEmpty else {
|
2017-07-03 20:20:14 +02:00
|
|
|
return Set<ArticleStatus>()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
guard let resultSet = database.rs_selectRowsWhereKey(DatabaseKey.articleID, inValues: Array(articleIDs), tableName: statusesTableName) else {
|
2017-07-03 20:20:14 +02:00
|
|
|
return Set<ArticleStatus>()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
return articleStatusesWithResultSet(resultSet)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func articleStatusesWithResultSet(_ resultSet: FMResultSet) -> Set<ArticleStatus> {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
var statuses = Set<ArticleStatus>()
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
while(resultSet.next()) {
|
2017-07-03 20:20:14 +02:00
|
|
|
if let oneArticleStatus = ArticleStatus(row: resultSet) {
|
2017-07-03 19:40:48 +02:00
|
|
|
statuses.insert(oneArticleStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statuses
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Saving
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func saveStatuses(_ statuses: Set<ArticleStatus>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
let statusArray = statuses.map { (oneStatus) -> NSDictionary in
|
2017-07-04 00:04:31 +02:00
|
|
|
return oneStatus.databaseDictionary()
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
queue.update { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
statusArray.forEach { (oneStatusDictionary) in
|
|
|
|
|
|
|
|
let _ = database.rs_insertRow(with: oneStatusDictionary as [NSObject: AnyObject], insertType: RSDatabaseInsertOrIgnore, tableName: "statuses")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func updateArticleStatusesInDatabase(_ articleIDs: Set<String>, statusKey: ArticleStatusKey, flag: Bool) {
|
|
|
|
|
|
|
|
queue.update { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
let _ = database.rs_updateRows(withValue: NSNumber(value: flag), valueKey: statusKey.rawValue, whereKey: articleIDKey, inValues: Array(articleIDs), tableName: statusesTableName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Creating
|
|
|
|
|
|
|
|
func createStatusForNewArticleIDs(_ articleIDs: Set<String>) {
|
|
|
|
|
|
|
|
let now = Date()
|
|
|
|
let statuses = articleIDs.map { (oneArticleID) -> LocalArticleStatus in
|
2017-07-03 20:20:14 +02:00
|
|
|
return ArticleStatus(articleID: oneArticleID, read: false, starred: false, userDeleted: false, dateArrived: now)
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
cacheStatuses(Set(statuses))
|
|
|
|
|
|
|
|
queue.update { (database: FMDatabase!) -> Void in
|
|
|
|
|
|
|
|
let falseValue = NSNumber(value: false)
|
|
|
|
|
|
|
|
articleIDs.forEach { (oneArticleID) in
|
|
|
|
|
|
|
|
let _ = database.executeUpdate("insert or ignore into statuses (read, articleID, starred, userDeleted, dateArrived) values (?, ?, ?, ?, ?)", withArgumentsIn:[falseValue, oneArticleID as NSString, falseValue, falseValue, now])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Cache
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func cachedStatusForArticleID(_ articleID: String) -> ArticleStatus? {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
return cachedStatuses[articleID]
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func cacheStatus(_ status: ArticleStatus) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
cacheStatuses(Set([status]))
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:20:14 +02:00
|
|
|
func cacheStatuses(_ statuses: Set<ArticleStatus>) {
|
2017-07-03 19:40:48 +02:00
|
|
|
|
|
|
|
statuses.forEach { (oneStatus) in
|
|
|
|
if let _ = cachedStatuses[oneStatus.articleID] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cachedStatuses[oneStatus.articleID] = oneStatus
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Utilities
|
|
|
|
|
|
|
|
func articleIDsMissingStatuses(_ articleIDs: Set<String>) -> Set<String> {
|
|
|
|
|
|
|
|
return Set(articleIDs.filter { cachedStatusForArticleID($0) == nil })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ParsedItem {
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
var articleID: String {
|
2017-07-03 19:40:48 +02:00
|
|
|
get {
|
2017-07-04 00:04:31 +02:00
|
|
|
return "\(feedURL) \(uniqueID)" //Must be same as Article.articleID
|
2017-07-03 19:40:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|