2019-10-15 09:31:24 +02:00
|
|
|
//
|
|
|
|
// FeedlySendArticleStatusesOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 14/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Articles
|
|
|
|
import SyncDatabase
|
|
|
|
import os.log
|
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
|
|
|
|
/// Take changes to statuses of articles locally and apply them to the corresponding the articles remotely.
|
2019-10-15 09:31:24 +02:00
|
|
|
final class FeedlySendArticleStatusesOperation: FeedlyOperation {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
private let database: SyncDatabase
|
|
|
|
private let log: OSLog
|
2019-10-18 23:21:02 +02:00
|
|
|
private let service: FeedlyMarkArticlesService
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
init(database: SyncDatabase, service: FeedlyMarkArticlesService, log: OSLog) {
|
2019-10-15 09:31:24 +02:00
|
|
|
self.database = database
|
2019-10-18 23:21:02 +02:00
|
|
|
self.service = service
|
2019-10-15 09:31:24 +02:00
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
2019-10-15 09:31:24 +02:00
|
|
|
os_log(.debug, log: log, "Sending article statuses...")
|
2019-12-16 21:49:07 +01:00
|
|
|
|
|
|
|
database.selectForProcessing { result in
|
2020-01-16 06:30:37 +01:00
|
|
|
if self.isCanceled {
|
|
|
|
self.didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:49:07 +01:00
|
|
|
switch result {
|
|
|
|
case .success(let syncStatuses):
|
|
|
|
self.processStatuses(syncStatuses)
|
|
|
|
case .failure:
|
|
|
|
self.didFinish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension FeedlySendArticleStatusesOperation {
|
|
|
|
|
|
|
|
func processStatuses(_ pending: [SyncStatus]) {
|
2019-10-18 23:21:02 +02:00
|
|
|
let statuses: [(status: ArticleStatus.Key, flag: Bool, action: FeedlyMarkAction)] = [
|
2019-10-15 09:31:24 +02:00
|
|
|
(.read, false, .unread),
|
|
|
|
(.read, true, .read),
|
|
|
|
(.starred, true, .saved),
|
|
|
|
(.starred, false, .unsaved),
|
|
|
|
]
|
2019-12-16 21:49:07 +01:00
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
let group = DispatchGroup()
|
2019-12-16 21:49:07 +01:00
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
for pairing in statuses {
|
|
|
|
let articleIds = pending.filter { $0.key == pairing.status && $0.flag == pairing.flag }
|
|
|
|
guard !articleIds.isEmpty else {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-16 21:49:07 +01:00
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
let ids = Set(articleIds.map { $0.articleID })
|
|
|
|
let database = self.database
|
|
|
|
group.enter()
|
2019-10-18 23:21:02 +02:00
|
|
|
service.mark(ids, as: pairing.action) { result in
|
|
|
|
assert(Thread.isMainThread)
|
2019-10-15 09:31:24 +02:00
|
|
|
switch result {
|
|
|
|
case .success:
|
2019-12-16 21:49:07 +01:00
|
|
|
database.deleteSelectedForProcessing(Array(ids)) { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
group.leave()
|
|
|
|
}
|
2019-10-15 09:31:24 +02:00
|
|
|
case .failure:
|
2019-12-16 21:49:07 +01:00
|
|
|
database.resetSelectedForProcessing(Array(ids)) { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
group.leave()
|
|
|
|
}
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 21:49:07 +01:00
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
group.notify(queue: DispatchQueue.main) {
|
|
|
|
os_log(.debug, log: self.log, "Done sending article statuses.")
|
|
|
|
self.didFinish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|