Save article statuses from articles we have not received yet

This commit is contained in:
Maurice Parker 2019-05-14 10:06:29 -05:00
parent 62d154d0f2
commit e31e1a4d60
4 changed files with 40 additions and 0 deletions

View File

@ -617,6 +617,10 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
} }
func ensureStatuses(_ articleIDs: Set<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool) {
database.ensureStatuses(articleIDs, statusKey, flag)
}
// MARK: - Container // MARK: - Container
public func flattenedFeeds() -> Set<Feed> { public func flattenedFeeds() -> Set<Feed> {

View File

@ -1020,17 +1020,28 @@ private extension FeedbinAccountDelegate {
let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } ) let feedbinUnreadArticleIDs = Set(articleIDs.map { String($0) } )
let currentUnreadArticleIDs = account.fetchUnreadArticleIDs() let currentUnreadArticleIDs = account.fetchUnreadArticleIDs()
// Mark articles as unread
let deltaUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs) let deltaUnreadArticleIDs = feedbinUnreadArticleIDs.subtracting(currentUnreadArticleIDs)
let markUnreadArticles = account.fetchArticles(forArticleIDs: deltaUnreadArticleIDs) let markUnreadArticles = account.fetchArticles(forArticleIDs: deltaUnreadArticleIDs)
DispatchQueue.main.async { DispatchQueue.main.async {
_ = account.markArticles(markUnreadArticles, statusKey: .read, flag: false) _ = account.markArticles(markUnreadArticles, statusKey: .read, flag: false)
} }
// Mark articles as read
let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(feedbinUnreadArticleIDs) let deltaReadArticleIDs = currentUnreadArticleIDs.subtracting(feedbinUnreadArticleIDs)
let markReadArticles = account.fetchArticles(forArticleIDs: deltaReadArticleIDs) let markReadArticles = account.fetchArticles(forArticleIDs: deltaReadArticleIDs)
DispatchQueue.main.async { DispatchQueue.main.async {
_ = account.markArticles(markReadArticles, statusKey: .read, flag: true) _ = account.markArticles(markReadArticles, statusKey: .read, flag: true)
} }
// Save any unread statuses for articles we haven't yet received
let markUnreadArticleIDs = Set(markUnreadArticles.map { $0.articleID })
let missingUnreadArticleIDs = deltaUnreadArticleIDs.subtracting(markUnreadArticleIDs)
if !missingUnreadArticleIDs.isEmpty {
DispatchQueue.main.async {
account.ensureStatuses(missingUnreadArticleIDs, .read, false)
}
}
} }
@ -1043,18 +1054,29 @@ private extension FeedbinAccountDelegate {
let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } ) let feedbinStarredArticleIDs = Set(articleIDs.map { String($0) } )
let currentStarredArticleIDs = account.fetchStarredArticleIDs() let currentStarredArticleIDs = account.fetchStarredArticleIDs()
// Mark articles as starred
let deltaStarredArticleIDs = feedbinStarredArticleIDs.subtracting(currentStarredArticleIDs) let deltaStarredArticleIDs = feedbinStarredArticleIDs.subtracting(currentStarredArticleIDs)
let markStarredArticles = account.fetchArticles(forArticleIDs: deltaStarredArticleIDs) let markStarredArticles = account.fetchArticles(forArticleIDs: deltaStarredArticleIDs)
DispatchQueue.main.async { DispatchQueue.main.async {
_ = account.markArticles(markStarredArticles, statusKey: .starred, flag: true) _ = account.markArticles(markStarredArticles, statusKey: .starred, flag: true)
} }
// Mark articles as unstarred
let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(feedbinStarredArticleIDs) let deltaUnstarredArticleIDs = currentStarredArticleIDs.subtracting(feedbinStarredArticleIDs)
let markUnstarredArticles = account.fetchArticles(forArticleIDs: deltaUnstarredArticleIDs) let markUnstarredArticles = account.fetchArticles(forArticleIDs: deltaUnstarredArticleIDs)
DispatchQueue.main.async { DispatchQueue.main.async {
_ = account.markArticles(markUnstarredArticles, statusKey: .starred, flag: false) _ = account.markArticles(markUnstarredArticles, statusKey: .starred, flag: false)
} }
// Save any starred statuses for articles we haven't yet received
let markStarredArticleIDs = Set(markStarredArticles.map { $0.articleID })
let missingStarredArticleIDs = deltaStarredArticleIDs.subtracting(markStarredArticleIDs)
if !missingStarredArticleIDs.isEmpty {
DispatchQueue.main.async {
account.ensureStatuses(missingStarredArticleIDs, .starred, true)
}
}
} }
} }

View File

@ -98,6 +98,10 @@ public final class ArticlesDatabase {
return articlesTable.update(feedID, parsedItems, defaultRead, completion) return articlesTable.update(feedID, parsedItems, defaultRead, completion)
} }
public func ensureStatuses(_ articleIDs: Set<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool) {
articlesTable.ensureStatuses(articleIDs, statusKey, flag)
}
// MARK: - Status // MARK: - Status
public func fetchUnreadArticleIDs() -> Set<String> { public func fetchUnreadArticleIDs() -> Set<String> {

View File

@ -185,6 +185,16 @@ final class ArticlesTable: DatabaseTable {
} }
} }
func ensureStatuses(_ articleIDs: Set<String>, _ statusKey: ArticleStatus.Key, _ flag: Bool) {
self.queue.update { (database) in
let statusesDictionary = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, false, database)
let statuses = Set(statusesDictionary.values)
_ = self.statusesTable.mark(statuses, statusKey, flag)
}
}
// MARK: Unread Counts // MARK: Unread Counts
func fetchUnreadCounts(_ feedIDs: Set<String>, _ completion: @escaping UnreadCountCompletionBlock) { func fetchUnreadCounts(_ feedIDs: Set<String>, _ completion: @escaping UnreadCountCompletionBlock) {