2019-11-30 00:13:52 +01:00
|
|
|
//
|
|
|
|
// FeedlyAddExistingFeedOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 27/11/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import os.log
|
|
|
|
import RSWeb
|
2020-01-16 06:30:37 +01:00
|
|
|
import RSCore
|
2019-11-30 00:13:52 +01:00
|
|
|
|
|
|
|
class FeedlyAddExistingFeedOperation: FeedlyOperation, FeedlyOperationDelegate, FeedlyCheckpointOperationDelegate {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
|
|
|
private let operationQueue = MainThreadOperationQueue()
|
2019-11-30 00:13:52 +01:00
|
|
|
var addCompletionHandler: ((Result<Void, Error>) -> ())?
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
init(account: Account, credentials: Credentials, resource: FeedlyFeedResourceId, service: FeedlyAddFeedToCollectionService, container: Container, progress: DownloadProgress, log: OSLog) throws {
|
2019-11-30 00:13:52 +01:00
|
|
|
|
2020-01-17 05:25:57 +01:00
|
|
|
let validator = FeedlyFeedContainerValidator(container: container)
|
2019-11-30 00:13:52 +01:00
|
|
|
let (folder, collectionId) = try validator.getValidContainer()
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
self.operationQueue.suspend()
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-11-30 00:13:52 +01:00
|
|
|
super.init()
|
|
|
|
|
|
|
|
self.downloadProgress = progress
|
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
let addRequest = FeedlyAddFeedToCollectionOperation(account: account, folder: folder, feedResource: resource, feedName: nil, collectionId: collectionId, service: service)
|
2019-11-30 00:13:52 +01:00
|
|
|
addRequest.delegate = self
|
|
|
|
addRequest.downloadProgress = progress
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(addRequest)
|
2019-11-30 00:13:52 +01:00
|
|
|
|
|
|
|
let createFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: addRequest, log: log)
|
|
|
|
createFeeds.downloadProgress = progress
|
2020-01-19 23:19:06 +01:00
|
|
|
createFeeds.addDependency(addRequest)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(createFeeds)
|
2019-11-30 00:13:52 +01:00
|
|
|
|
|
|
|
let finishOperation = FeedlyCheckpointOperation()
|
|
|
|
finishOperation.checkpointDelegate = self
|
|
|
|
finishOperation.downloadProgress = progress
|
2020-01-19 23:19:06 +01:00
|
|
|
finishOperation.addDependency(createFeeds)
|
2020-02-06 06:18:29 +01:00
|
|
|
self.operationQueue.add(finishOperation)
|
2019-11-30 00:13:52 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
|
|
|
operationQueue.resume()
|
2019-11-30 00:13:52 +01:00
|
|
|
}
|
2020-01-19 23:19:06 +01:00
|
|
|
|
|
|
|
override func didCancel() {
|
|
|
|
operationQueue.cancelAllOperations()
|
|
|
|
addCompletionHandler = nil
|
2020-01-20 01:55:39 +01:00
|
|
|
super.didCancel()
|
2020-01-19 23:19:06 +01:00
|
|
|
}
|
2019-11-30 00:13:52 +01:00
|
|
|
|
|
|
|
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
|
|
|
addCompletionHandler?(.failure(error))
|
|
|
|
addCompletionHandler = nil
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func feedlyCheckpointOperationDidReachCheckpoint(_ operation: FeedlyCheckpointOperation) {
|
2020-01-16 06:30:37 +01:00
|
|
|
guard !isCanceled else {
|
2019-11-30 00:13:52 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
addCompletionHandler?(.success(()))
|
|
|
|
addCompletionHandler = nil
|
|
|
|
|
|
|
|
didFinish()
|
|
|
|
}
|
|
|
|
}
|