Add createStatusesIfNeeded API to Account, to use with syncing. (Needed for Feedly syncing, and could very well be needed for other systems too.)

This commit is contained in:
Brent Simmons 2020-01-09 22:27:29 -08:00
parent ff31f0114a
commit 4ab5c25844
3 changed files with 40 additions and 0 deletions

View File

@ -791,6 +791,24 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
return updatedArticles
}
/// Make sure statuses exist. Any existing statuses wont be touched.
/// All created statuses will be marked as read and not starred.
/// Sends a .StatusesDidChange notification.
func createStatusesIfNeeded(articleIDs: Set<String>, completion: DatabaseCompletionBlock? = nil) {
guard !articleIDs.isEmpty else {
completion?(nil)
return
}
database.createStatusesIfNeeded(articleIDs: articleIDs) { error in
if let error = error {
completion?(error)
return
}
self.noteStatusesForArticleIDsDidChange(articleIDs)
completion?(nil)
}
}
/// Mark articleIDs statuses based on statusKey and flag.
/// Will create statuses in the database and in memory as needed. Sends a .StatusesDidChange notification.
func mark(articleIDs: Set<String>, statusKey: ArticleStatus.Key, flag: Bool, completion: DatabaseCompletionBlock? = nil) {

View File

@ -189,6 +189,12 @@ public final class ArticlesDatabase {
articlesTable.mark(articleIDs, statusKey, flag, completion)
}
/// Create statuses for specified articleIDs. For existing statuses, dont do anything.
/// For newly-created statuses, mark them as read and not-starred.
public func createStatusesIfNeeded(articleIDs: Set<String>, completion: @escaping DatabaseCompletionBlock) {
articlesTable.createStatusesIfNeeded(articleIDs, completion)
}
// MARK: - Suspend and Resume (for iOS)
/// Close the database and stop running database calls.

View File

@ -421,6 +421,22 @@ final class ArticlesTable: DatabaseTable {
}
}
func createStatusesIfNeeded(_ articleIDs: Set<String>, _ completion: @escaping DatabaseCompletionBlock) {
queue.runInTransaction { databaseResult in
switch databaseResult {
case .success(let database):
let _ = self.statusesTable.ensureStatusesForArticleIDs(articleIDs, true, database)
DispatchQueue.main.async {
completion(nil)
}
case .failure(let databaseError):
DispatchQueue.main.async {
completion(databaseError)
}
}
}
}
// MARK: - Indexing
func indexUnindexedArticles() {