Save new and updated articles to the database.

This commit is contained in:
Brent Simmons 2017-10-08 01:54:37 -07:00
parent ca611623aa
commit 1b44591692
3 changed files with 40 additions and 4 deletions

View File

@ -18,6 +18,7 @@ public extension Notification.Name {
public static let AccountRefreshDidBegin = Notification.Name(rawValue: "AccountRefreshDidBegin")
public static let AccountRefreshDidFinish = Notification.Name(rawValue: "AccountRefreshDidFinish")
public static let AccountRefreshProgressDidChange = Notification.Name(rawValue: "AccountRefreshProgressDidChange")
public static let AccountDidDownloadArticles = Notification.Name(rawValue: "AccountDidDownloadArticles")
}
public enum AccountType: Int {
@ -33,6 +34,11 @@ public enum AccountType: Int {
public final class Account: DisplayNameProvider, Container, Hashable {
public struct UserInfoKey { // Used by AccountDidDownloadArticles.
public static let newArticles = "newArticles"
public static let updatedArticles = "updatedArticles"
}
public let accountID: String
public let type: AccountType
public var nameForDisplay = ""
@ -126,9 +132,23 @@ public final class Account: DisplayNameProvider, Container, Hashable {
delegate.refreshAll(for: self)
}
func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: RSVoidCompletionBlock) {
func update(_ feed: Feed, with parsedFeed: ParsedFeed, _ completion: @escaping RSVoidCompletionBlock) {
completion()
database.update(feed: feed, parsedFeed: parsedFeed) { (newArticles, updatedArticles) in
var userInfo = [String: Any]()
if let newArticles = newArticles, !newArticles.isEmpty {
self.updateUnreadCounts(for: Set([feed]))
userInfo[UserInfoKey.newArticles] = newArticles
}
if let updatedArticles = updatedArticles, !updatedArticles.isEmpty {
userInfo[UserInfoKey.updatedArticles] = updatedArticles
}
completion()
NotificationCenter.default.post(name: .AccountDidDownloadArticles, object: self, userInfo: userInfo.isEmpty ? nil : userInfo)
}
}
public func markArticles(_ articles: Set<Article>, statusKey: String, flag: Bool) {
@ -210,6 +230,18 @@ public final class Account: DisplayNameProvider, Container, Hashable {
dirty = true
}
public func updateUnreadCounts(for feeds: Set<Feed>) {
database.fetchUnreadCounts(for: feeds) { (unreadCountDictionary) in
for feed in feeds {
if let unreadCount = unreadCountDictionary[feed] {
feed.unreadCount = unreadCount
}
}
}
}
// MARK: - Notifications
@objc func downloadProgressDidChange(_ note: Notification) {

View File

@ -201,11 +201,11 @@ private final class StatusCache {
subscript(_ articleID: String) -> ArticleStatus? {
get {
assert(!Thread.isMainThread)
return self[articleID]
return dictionary[articleID]
}
set {
assert(!Thread.isMainThread)
self[articleID] = newValue
dictionary[articleID] = newValue
}
}
}

View File

@ -113,6 +113,10 @@ private extension DatabaseLookupTable {
func updateRelationships(for objects: [DatabaseObject], _ database: FMDatabase) {
if objects.isEmpty {
return
}
if let lookupTable = fetchRelatedObjectIDsMap(objects.databaseIDs(), database) {
for object in objects {
syncRelatedObjectsAndLookupTable(object, lookupTable, database)