Add external web feed to status so that it can prompt receiving system to pull down new articles.

This commit is contained in:
Maurice Parker 2020-04-10 18:10:54 -05:00
parent a8dcf3eeee
commit 6ec11119f8
3 changed files with 46 additions and 29 deletions

View File

@ -98,12 +98,12 @@ final class CloudKitAccountDelegate: AccountDelegate {
return return
} }
let starredArticleIDs = syncStatuses.filter({ $0.key == .starred && $0.flag == true }).map({ $0.articleID }) let articleIDs = syncStatuses.map({ $0.articleID })
account.fetchArticlesAsync(.articleIDs(Set(starredArticleIDs))) { result in account.fetchArticlesAsync(.articleIDs(Set(articleIDs))) { result in
func processWithArticles(_ starredArticles: Set<Article>) { func processWithArticles(_ articles: Set<Article>) {
self.articlesZone.sendArticleStatus(syncStatuses, starredArticles: starredArticles) { result in self.articlesZone.sendArticleStatus(syncStatuses, articles: articles) { result in
switch result { switch result {
case .success: case .success:
self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID }) ) self.database.deleteSelectedForProcessing(syncStatuses.map({ $0.articleID }) )
@ -119,8 +119,8 @@ final class CloudKitAccountDelegate: AccountDelegate {
} }
switch result { switch result {
case .success(let starredArticles): case .success(let articles):
processWithArticles(starredArticles) processWithArticles(articles)
case .failure(let databaseError): case .failure(let databaseError):
completion(.failure(databaseError)) completion(.failure(databaseError))
} }

View File

@ -48,6 +48,7 @@ final class CloudKitArticlesZone: CloudKitZone {
struct CloudKitArticleStatus { struct CloudKitArticleStatus {
static let recordType = "ArticleStatus" static let recordType = "ArticleStatus"
struct Fields { struct Fields {
static let webFeedExternalID = "webFeedExternalID"
static let read = "read" static let read = "read"
static let starred = "starred" static let starred = "starred"
static let userDeleted = "userDeleted" static let userDeleted = "userDeleted"
@ -81,8 +82,11 @@ final class CloudKitArticlesZone: CloudKitZone {
} }
} }
func sendArticleStatus(_ syncStatuses: [SyncStatus], starredArticles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) { func sendArticleStatus(_ syncStatuses: [SyncStatus], articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
var records = makeStatusRecords(syncStatuses)
var records = makeStatusRecords(syncStatuses, articles)
let starredArticles = articles.filter({ $0.status.starred == true })
makeArticleRecordsIfNecessary(starredArticles) { result in makeArticleRecordsIfNecessary(starredArticles) { result in
switch result { switch result {
case .success(let articleRecords): case .success(let articleRecords):
@ -92,11 +96,11 @@ final class CloudKitArticlesZone: CloudKitZone {
case .success: case .success:
completion(.success(())) completion(.success(()))
case .failure(let error): case .failure(let error):
self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: starredArticles, completion: completion) self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: articles, completion: completion)
} }
} }
case .failure(let error): case .failure(let error):
self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: starredArticles, completion: completion) self.handleSendArticleStatusError(error, syncStatuses: syncStatuses, starredArticles: articles, completion: completion)
} }
} }
} }
@ -106,7 +110,7 @@ final class CloudKitArticlesZone: CloudKitZone {
self.createZoneRecord() { result in self.createZoneRecord() { result in
switch result { switch result {
case .success: case .success:
self.sendArticleStatus(syncStatuses, starredArticles: starredArticles, completion: completion) self.sendArticleStatus(syncStatuses, articles: starredArticles, completion: completion)
case .failure(let error): case .failure(let error):
completion(.failure(error)) completion(.failure(error))
} }
@ -120,7 +124,13 @@ final class CloudKitArticlesZone: CloudKitZone {
private extension CloudKitArticlesZone { private extension CloudKitArticlesZone {
func makeStatusRecords(_ syncStatuses: [SyncStatus]) -> [CKRecord] { func makeStatusRecords(_ syncStatuses: [SyncStatus], _ articles: Set<Article>) -> [CKRecord] {
var articleDict = [String: Article]()
for article in articles {
articleDict[article.articleID] = article
}
var records = [String: CKRecord]() var records = [String: CKRecord]()
for status in syncStatuses { for status in syncStatuses {
@ -132,6 +142,10 @@ private extension CloudKitArticlesZone {
records[status.articleID] = record records[status.articleID] = record
} }
if let webFeedExternalID = articleDict[status.articleID]?.webFeed?.externalID {
record![CloudKitArticleStatus.Fields.webFeedExternalID] = webFeedExternalID
}
switch status.key { switch status.key {
case .read: case .read:
record![CloudKitArticleStatus.Fields.read] = status.flag ? "1" : "0" record![CloudKitArticleStatus.Fields.read] = status.flag ? "1" : "0"

View File

@ -94,25 +94,28 @@ private extension CloudKitArticlesZoneDelegate {
if newArticleStatusIDs.isEmpty { if newArticleStatusIDs.isEmpty {
group.leave() group.leave()
} else { } else {
self.account?.fetchArticlesAsync(FetchType.articleIDs(newArticleStatusIDs)) { result in
switch result { var webFeedExternalIDDict = [String: String]()
case .success(let articles): for record in records {
if let webFeedExternalID = record[CloudKitArticlesZone.CloudKitArticleStatus.Fields.webFeedExternalID] as? String {
if articles.isEmpty { webFeedExternalIDDict[record.externalID] = webFeedExternalID
group.leave()
} else {
let webFeeds = Set(articles.compactMap({ $0.webFeed }))
webFeeds.forEach { $0.dropConditionalGetInfo() }
self.refreshProgress?.addToNumberOfTasksAndRemaining(webFeeds.count)
self.refresher?.refreshFeeds(webFeeds) {
group.leave()
}
}
case .failure:
group.leave()
} }
} }
var webFeeds = Set<WebFeed>()
for newArticleStatusID in newArticleStatusIDs {
if let webFeedExternalID = webFeedExternalIDDict[newArticleStatusID],
let webFeed = self.account?.existingWebFeed(withExternalID: webFeedExternalID) {
webFeeds.insert(webFeed)
}
}
webFeeds.forEach { $0.dropConditionalGetInfo() }
self.refreshProgress?.addToNumberOfTasksAndRemaining(webFeeds.count)
self.refresher?.refreshFeeds(webFeeds) {
group.leave()
}
} }
case .failure: case .failure: