Resolve issue where CloudKit articles were coming back as unread.

This commit is contained in:
Maurice Parker 2020-04-14 01:25:35 -05:00
parent 2ae511b818
commit 85d4cc352f
5 changed files with 44 additions and 27 deletions

View File

@ -594,18 +594,7 @@ private extension CloudKitAccountDelegate {
self.refreshProgress.completeTask()
self.refresher.refreshFeeds(webFeeds) {
account.metadata.lastArticleFetchEndTime = Date()
self.sendArticleStatus(for: account) { result in
switch result {
case .success:
completion(.success(()))
case .failure(let error):
fail(error)
}
}
}
case .failure(let error):
@ -638,12 +627,13 @@ private extension CloudKitAccountDelegate {
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) {
if let newArticles = newAndUpdatedArticles.newArticles {
let syncStatuses = newArticles.map { article in
return SyncStatus(articleID: article.articleID, key: .read, flag: false)
articlesZone.sendNewArticles(newArticles) { _ in
completion()
}
database.insertStatuses(syncStatuses)
} else {
completion()
}
}

View File

@ -51,7 +51,6 @@ final class CloudKitArticlesZone: CloudKitZone {
static let webFeedExternalID = "webFeedExternalID"
static let read = "read"
static let starred = "starred"
static let userDeleted = "userDeleted"
}
}
@ -82,6 +81,11 @@ final class CloudKitArticlesZone: CloudKitZone {
}
}
func sendNewArticles(_ articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
let records = makeNewStatusRecords(articles)
saveIfNew(records, completion: completion)
}
func sendArticleStatus(_ syncStatuses: [SyncStatus], articles: Set<Article>, completion: @escaping ((Result<Void, Error>) -> Void)) {
var records = makeStatusRecords(syncStatuses, articles)
@ -124,6 +128,23 @@ final class CloudKitArticlesZone: CloudKitZone {
private extension CloudKitArticlesZone {
func makeNewStatusRecords(_ articles: Set<Article>) -> [CKRecord] {
var records = [CKRecord]()
for article in articles {
let recordID = CKRecord.ID(recordName: article.articleID, zoneID: Self.zoneID)
let record = CKRecord(recordType: CloudKitArticleStatus.recordType, recordID: recordID)
if let webFeedExternalID = article.webFeed?.externalID {
record[CloudKitArticleStatus.Fields.webFeedExternalID] = webFeedExternalID
}
record[CloudKitArticleStatus.Fields.read] = "0"
records.append(record)
}
return records
}
func makeStatusRecords(_ syncStatuses: [SyncStatus], _ articles: Set<Article>) -> [CKRecord] {
var articleDict = [String: Article]()
@ -151,8 +172,8 @@ private extension CloudKitArticlesZone {
record![CloudKitArticleStatus.Fields.read] = status.flag ? "1" : "0"
case .starred:
record![CloudKitArticleStatus.Fields.starred] = status.flag ? "1" : "0"
case .userDeleted:
record![CloudKitArticleStatus.Fields.userDeleted] = status.flag ? "1" : "0"
default:
break
}
}

View File

@ -202,7 +202,8 @@ private extension CloudKitArticlesZoneDelegate {
extension CloudKitArticlesZoneDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) {
completion()
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {

View File

@ -234,7 +234,8 @@ final class LocalAccountDelegate: AccountDelegate {
extension LocalAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles) {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess newAndUpdatedArticles: NewAndUpdatedArticles, completion: @escaping () -> Void) {
completion()
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {

View File

@ -14,7 +14,7 @@ import Articles
import ArticlesDatabase
protocol LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: NewAndUpdatedArticles)
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: NewAndUpdatedArticles, completion: @escaping () -> Void)
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed)
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher)
}
@ -101,14 +101,18 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
account.update(feed, with: parsedFeed) { result in
if case .success(let newAndUpdatedArticles) = result {
self.delegate?.localAccountRefresher(self, didProcess: newAndUpdatedArticles)
if let httpResponse = response as? HTTPURLResponse {
feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
self.delegate?.localAccountRefresher(self, didProcess: newAndUpdatedArticles) {
if let httpResponse = response as? HTTPURLResponse {
feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
}
feed.contentHash = dataHash
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
}
feed.contentHash = dataHash
} else {
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
}
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
}
}