Refactor LocalAccountRefresherDelegate to be much simpler

This commit is contained in:
Maurice Parker 2020-05-08 09:28:11 -05:00
parent 9027fca2a4
commit ffdeda4a74
3 changed files with 22 additions and 38 deletions

View File

@ -786,15 +786,11 @@ private extension CloudKitAccountDelegate {
extension CloudKitAccountDelegate: LocalAccountRefresherDelegate { extension CloudKitAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess articleChanges: ArticleChanges, completion: @escaping () -> Void) { func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed, articleChanges: ArticleChanges?) {
self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles)
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
refreshProgress.completeTask() refreshProgress.completeTask()
} if let articleChanges = articleChanges {
self.storeArticleChanges(new: articleChanges.newArticles, updated: articleChanges.updatedArticles, deleted: articleChanges.deletedArticles)
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) { }
} }
} }

View File

@ -235,17 +235,10 @@ final class LocalAccountDelegate: AccountDelegate {
extension LocalAccountDelegate: LocalAccountRefresherDelegate { extension LocalAccountDelegate: LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void) { func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed, articleChanges: ArticleChanges?) {
completion()
}
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed) {
refreshProgress.completeTask() refreshProgress.completeTask()
} }
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher) {
}
} }
private extension LocalAccountDelegate { private extension LocalAccountDelegate {

View File

@ -14,9 +14,7 @@ import Articles
import ArticlesDatabase import ArticlesDatabase
protocol LocalAccountRefresherDelegate { protocol LocalAccountRefresherDelegate {
func localAccountRefresher(_ refresher: LocalAccountRefresher, didProcess: ArticleChanges, completion: @escaping () -> Void) func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed, articleChanges: ArticleChanges?)
func localAccountRefresher(_ refresher: LocalAccountRefresher, requestCompletedFor: WebFeed)
func localAccountRefresherDidFinish(_ refresher: LocalAccountRefresher)
} }
final class LocalAccountRefresher { final class LocalAccountRefresher {
@ -74,21 +72,21 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
guard !data.isEmpty, !isSuspended else { guard !data.isEmpty, !isSuspended else {
completion() completion()
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return return
} }
if let error = error { if let error = error {
print("Error downloading \(feed.url) - \(error)") print("Error downloading \(feed.url) - \(error)")
completion() completion()
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return return
} }
let dataHash = data.md5String let dataHash = data.md5String
if dataHash == feed.contentHash { if dataHash == feed.contentHash {
completion() completion()
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return return
} }
@ -97,23 +95,21 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
guard let account = feed.account, let parsedFeed = parsedFeed, error == nil else { guard let account = feed.account, let parsedFeed = parsedFeed, error == nil else {
completion() completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) self.delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return return
} }
account.update(feed, with: parsedFeed) { result in account.update(feed, with: parsedFeed) { result in
if case .success(let articleChanges) = result { if case .success(let articleChanges) = result {
self.delegate?.localAccountRefresher(self, didProcess: articleChanges) { if let httpResponse = response as? HTTPURLResponse {
if let httpResponse = response as? HTTPURLResponse { feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
feed.conditionalGetInfo = HTTPConditionalGetInfo(urlResponse: httpResponse)
}
feed.contentHash = dataHash
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed)
} }
feed.contentHash = dataHash
completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: articleChanges)
} else { } else {
completion() completion()
self.delegate?.localAccountRefresher(self, requestCompletedFor: feed) self.delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
} }
} }
@ -123,7 +119,7 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool { func downloadSession(_ downloadSession: DownloadSession, shouldContinueAfterReceivingData data: Data, representedObject: AnyObject) -> Bool {
let feed = representedObject as! WebFeed let feed = representedObject as! WebFeed
guard !isSuspended else { guard !isSuspended else {
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return false return false
} }
@ -132,7 +128,7 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
} }
if data.isDefinitelyNotFeed() { if data.isDefinitelyNotFeed() {
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return false return false
} }
@ -141,7 +137,7 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
if FeedParser.mightBeAbleToParseBasedOnPartialData(parserData) { if FeedParser.mightBeAbleToParseBasedOnPartialData(parserData) {
return true return true
} else { } else {
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
return false return false
} }
} }
@ -151,23 +147,22 @@ extension LocalAccountRefresher: DownloadSessionDelegate {
func downloadSession(_ downloadSession: DownloadSession, didReceiveUnexpectedResponse response: URLResponse, representedObject: AnyObject) { func downloadSession(_ downloadSession: DownloadSession, didReceiveUnexpectedResponse response: URLResponse, representedObject: AnyObject) {
let feed = representedObject as! WebFeed let feed = representedObject as! WebFeed
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
} }
func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) { func downloadSession(_ downloadSession: DownloadSession, didReceiveNotModifiedResponse: URLResponse, representedObject: AnyObject) {
let feed = representedObject as! WebFeed let feed = representedObject as! WebFeed
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
} }
func downloadSession(_ downloadSession: DownloadSession, didDiscardDuplicateRepresentedObject representedObject: AnyObject) { func downloadSession(_ downloadSession: DownloadSession, didDiscardDuplicateRepresentedObject representedObject: AnyObject) {
let feed = representedObject as! WebFeed let feed = representedObject as! WebFeed
delegate?.localAccountRefresher(self, requestCompletedFor: feed) delegate?.localAccountRefresher(self, requestCompletedFor: feed, articleChanges: nil)
} }
func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) { func downloadSessionDidCompleteDownloadObjects(_ downloadSession: DownloadSession) {
completion?() completion?()
completion = nil completion = nil
delegate?.localAccountRefresherDidFinish(self)
} }
} }