Merge pull request #1125 from kielgillard/master
Refactor add and create feeds
This commit is contained in:
commit
5b448d9a20
|
@ -91,8 +91,10 @@
|
|||
9E1D15572334355900F4944C /* FeedlyRequestStreamsOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E1D15562334355900F4944C /* FeedlyRequestStreamsOperation.swift */; };
|
||||
9E1D155B2334423300F4944C /* FeedlyOrganiseParsedItemsByFeedOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E1D155A2334423300F4944C /* FeedlyOrganiseParsedItemsByFeedOperation.swift */; };
|
||||
9E1D155D233447F000F4944C /* FeedlyUpdateAccountFeedsWithItemsOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E1D155C233447F000F4944C /* FeedlyUpdateAccountFeedsWithItemsOperation.swift */; };
|
||||
9E510D6E234F16A8002E6F1A /* FeedlyCreateFeedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E510D6D234F16A8002E6F1A /* FeedlyCreateFeedRequest.swift */; };
|
||||
9E510D6E234F16A8002E6F1A /* FeedlyAddFeedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E510D6D234F16A8002E6F1A /* FeedlyAddFeedRequest.swift */; };
|
||||
9E713653233AD63E00765C84 /* FeedlyRefreshStreamEntriesStatusOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E713652233AD63E00765C84 /* FeedlyRefreshStreamEntriesStatusOperation.swift */; };
|
||||
9E7299D723505E9600DAEFB7 /* FeedlyAddFeedOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7299D623505E9600DAEFB7 /* FeedlyAddFeedOperation.swift */; };
|
||||
9E7299D9235062A200DAEFB7 /* FeedlyResourceProviding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7299D8235062A200DAEFB7 /* FeedlyResourceProviding.swift */; };
|
||||
9E7F15072341E96700F860D1 /* AccountFeedlySyncTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E7F15062341E96700F860D1 /* AccountFeedlySyncTest.swift */; };
|
||||
9E7F150A2341EF5A00F860D1 /* feedly_collections_initial.json in Resources */ = {isa = PBXBuildFile; fileRef = 9E7F15092341EF5A00F860D1 /* feedly_collections_initial.json */; };
|
||||
9E7F150D2341F32000F860D1 /* macintosh_initial.json in Resources */ = {isa = PBXBuildFile; fileRef = 9E7F150C2341F32000F860D1 /* macintosh_initial.json */; };
|
||||
|
@ -118,7 +120,6 @@
|
|||
9ECC9A85234DC16E009B5144 /* FeedlyAccountDelegateError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9ECC9A84234DC16E009B5144 /* FeedlyAccountDelegateError.swift */; };
|
||||
9EE4CCFA234F106600FBAE4B /* FeedlyFeedContainerValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE4CCF9234F106600FBAE4B /* FeedlyFeedContainerValidator.swift */; };
|
||||
9EF35F7A234E830E003AE2AE /* FeedlyCompoundOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF35F79234E830E003AE2AE /* FeedlyCompoundOperation.swift */; };
|
||||
9EF35F7C234E9383003AE2AE /* FeedlySubscribeToFeedOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EF35F7B234E9383003AE2AE /* FeedlySubscribeToFeedOperation.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -267,8 +268,10 @@
|
|||
9E1D15562334355900F4944C /* FeedlyRequestStreamsOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyRequestStreamsOperation.swift; sourceTree = "<group>"; };
|
||||
9E1D155A2334423300F4944C /* FeedlyOrganiseParsedItemsByFeedOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyOrganiseParsedItemsByFeedOperation.swift; sourceTree = "<group>"; };
|
||||
9E1D155C233447F000F4944C /* FeedlyUpdateAccountFeedsWithItemsOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyUpdateAccountFeedsWithItemsOperation.swift; sourceTree = "<group>"; };
|
||||
9E510D6D234F16A8002E6F1A /* FeedlyCreateFeedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyCreateFeedRequest.swift; sourceTree = "<group>"; };
|
||||
9E510D6D234F16A8002E6F1A /* FeedlyAddFeedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyAddFeedRequest.swift; sourceTree = "<group>"; };
|
||||
9E713652233AD63E00765C84 /* FeedlyRefreshStreamEntriesStatusOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyRefreshStreamEntriesStatusOperation.swift; sourceTree = "<group>"; };
|
||||
9E7299D623505E9600DAEFB7 /* FeedlyAddFeedOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyAddFeedOperation.swift; sourceTree = "<group>"; };
|
||||
9E7299D8235062A200DAEFB7 /* FeedlyResourceProviding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyResourceProviding.swift; sourceTree = "<group>"; };
|
||||
9E7F15062341E96700F860D1 /* AccountFeedlySyncTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountFeedlySyncTest.swift; sourceTree = "<group>"; };
|
||||
9E7F15092341EF5A00F860D1 /* feedly_collections_initial.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = feedly_collections_initial.json; sourceTree = "<group>"; };
|
||||
9E7F150C2341F32000F860D1 /* macintosh_initial.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = macintosh_initial.json; sourceTree = "<group>"; };
|
||||
|
@ -294,7 +297,6 @@
|
|||
9ECC9A84234DC16E009B5144 /* FeedlyAccountDelegateError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyAccountDelegateError.swift; sourceTree = "<group>"; };
|
||||
9EE4CCF9234F106600FBAE4B /* FeedlyFeedContainerValidator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyFeedContainerValidator.swift; sourceTree = "<group>"; };
|
||||
9EF35F79234E830E003AE2AE /* FeedlyCompoundOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlyCompoundOperation.swift; sourceTree = "<group>"; };
|
||||
9EF35F7B234E9383003AE2AE /* FeedlySubscribeToFeedOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedlySubscribeToFeedOperation.swift; sourceTree = "<group>"; };
|
||||
D511EEB5202422BB00712EC3 /* Account_project_debug.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Account_project_debug.xcconfig; sourceTree = "<group>"; };
|
||||
D511EEB6202422BB00712EC3 /* Account_target.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Account_target.xcconfig; sourceTree = "<group>"; };
|
||||
D511EEB7202422BB00712EC3 /* Account_project_release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Account_project_release.xcconfig; sourceTree = "<group>"; };
|
||||
|
@ -553,10 +555,11 @@
|
|||
9EC688EB232C583300A8D0A2 /* FeedlyAccountDelegate+OAuth.swift */,
|
||||
9EC688ED232C58E800A8D0A2 /* OAuthAuthorizationCodeGranting.swift */,
|
||||
9EC688E9232B973C00A8D0A2 /* FeedlyAPICaller.swift */,
|
||||
9E510D6D234F16A8002E6F1A /* FeedlyCreateFeedRequest.swift */,
|
||||
9E510D6D234F16A8002E6F1A /* FeedlyAddFeedRequest.swift */,
|
||||
9E1D1554233431A600F4944C /* FeedlyOperation.swift */,
|
||||
9EF35F79234E830E003AE2AE /* FeedlyCompoundOperation.swift */,
|
||||
9EF35F7B234E9383003AE2AE /* FeedlySubscribeToFeedOperation.swift */,
|
||||
9E7299D8235062A200DAEFB7 /* FeedlyResourceProviding.swift */,
|
||||
9E7299D623505E9600DAEFB7 /* FeedlyAddFeedOperation.swift */,
|
||||
9EBC31B6233987C1002A567B /* FeedlyArticleStatusCoordinator.swift */,
|
||||
9EBC31B32338AC2E002A567B /* Models */,
|
||||
9EBC31B22338AC0F002A567B /* Refresh */,
|
||||
|
@ -814,6 +817,7 @@
|
|||
552032F9229D5D5A009559E0 /* ReaderAPISubscription.swift in Sources */,
|
||||
84C3654A1F899F3B001EC85C /* CombinedRefreshProgress.swift in Sources */,
|
||||
9EC688EE232C58E800A8D0A2 /* OAuthAuthorizationCodeGranting.swift in Sources */,
|
||||
9E7299D9235062A200DAEFB7 /* FeedlyResourceProviding.swift in Sources */,
|
||||
9EC688EC232C583300A8D0A2 /* FeedlyAccountDelegate+OAuth.swift in Sources */,
|
||||
8469F81C1F6DD15E0084783E /* Account.swift in Sources */,
|
||||
9EAEC60E2332FEC20085D7C9 /* FeedlyFeed.swift in Sources */,
|
||||
|
@ -841,13 +845,13 @@
|
|||
9E1D15512334282100F4944C /* FeedlyMirrorCollectionsAsFoldersOperation.swift in Sources */,
|
||||
9E1773D7234575AB0056A5A8 /* FeedlyTag.swift in Sources */,
|
||||
515E4EB62324FF8C0057B0E7 /* URLRequest+RSWeb.swift in Sources */,
|
||||
9E7299D723505E9600DAEFB7 /* FeedlyAddFeedOperation.swift in Sources */,
|
||||
5154367B228EEB28005E1CDF /* FeedbinImportResult.swift in Sources */,
|
||||
84B2D4D02238CD8A00498ADA /* FeedMetadata.swift in Sources */,
|
||||
9EF35F7C234E9383003AE2AE /* FeedlySubscribeToFeedOperation.swift in Sources */,
|
||||
9EAEC624233315F60085D7C9 /* FeedlyEntry.swift in Sources */,
|
||||
5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */,
|
||||
84B99C9F1FAE8D3200ECDEDB /* ContainerPath.swift in Sources */,
|
||||
9E510D6E234F16A8002E6F1A /* FeedlyCreateFeedRequest.swift in Sources */,
|
||||
9E510D6E234F16A8002E6F1A /* FeedlyAddFeedRequest.swift in Sources */,
|
||||
5133231122810EB200C30F19 /* FeedbinIcon.swift in Sources */,
|
||||
846E77501F6EF9C400A165E2 /* LocalAccountRefresher.swift in Sources */,
|
||||
55203300229D5D5A009559E0 /* ReaderAPICaller.swift in Sources */,
|
||||
|
|
|
@ -238,18 +238,18 @@ final class FeedlyAccountDelegate: AccountDelegate {
|
|||
return (folder, collectionId)
|
||||
}
|
||||
|
||||
var createFeedRequest: FeedlyCreateFeedRequest?
|
||||
var createFeedRequest: FeedlyAddFeedRequest?
|
||||
|
||||
func createFeed(for account: Account, url: String, name: String?, container: Container, completion: @escaping (Result<Feed, Error>) -> Void) {
|
||||
|
||||
let progress = refreshProgress
|
||||
progress.addToNumberOfTasksAndRemaining(1)
|
||||
|
||||
let createFeedRequest = FeedlyCreateFeedRequest(account: account, caller: caller, container: container, log: log)
|
||||
let request = FeedlyAddFeedRequest(account: account, caller: caller, container: container, log: log)
|
||||
|
||||
self.createFeedRequest = createFeedRequest
|
||||
self.createFeedRequest = request
|
||||
|
||||
createFeedRequest.start(url: url, name: name) { [weak self] result in
|
||||
request.addNewFeed(at: url, name: name) { [weak self] result in
|
||||
progress.completeTask()
|
||||
self?.createFeedRequest = nil
|
||||
completion(result)
|
||||
|
@ -259,7 +259,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
|
|||
func renameFeed(for account: Account, with feed: Feed, to name: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
let folderCollectionIds = account.folders?.filter { $0.has(feed) }.compactMap { $0.externalID }
|
||||
guard let collectionIds = folderCollectionIds, let collectionId = collectionIds.first else {
|
||||
completion(.failure(FeedbinAccountDelegateError.invalidParameter))
|
||||
completion(.failure(FeedlyAccountDelegateError.unableToRenameFeed(feed.nameForDisplay, name)))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -283,41 +283,25 @@ final class FeedlyAccountDelegate: AccountDelegate {
|
|||
feed.editedName = name
|
||||
}
|
||||
|
||||
func addFeed(for account: Account, with: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
let (folder, collectionId): (Folder, String)
|
||||
do {
|
||||
(folder, collectionId) = try isValidContainer(for: account, container: container)
|
||||
} catch {
|
||||
return DispatchQueue.main.async {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
var addFeedRequest: FeedlyAddFeedRequest?
|
||||
|
||||
func addFeed(for account: Account, with feed: Feed, to container: Container, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
|
||||
let progress = refreshProgress
|
||||
progress.addToNumberOfTasksAndRemaining(1)
|
||||
|
||||
let feedId = FeedlyFeedResourceId(id: with.feedID)
|
||||
let request = FeedlyAddFeedRequest(account: account, caller: caller, container: container, log: log)
|
||||
|
||||
caller.addFeed(with: feedId, toCollectionWith: collectionId) { result in
|
||||
self.addFeedRequest = request
|
||||
|
||||
request.add(existing: feed) { [weak self] result in
|
||||
progress.completeTask()
|
||||
|
||||
self?.addFeedRequest = nil
|
||||
|
||||
switch result {
|
||||
case .success(let feedlyFeeds):
|
||||
for feedlyFeed in feedlyFeeds where !folder.hasFeed(with: feedlyFeed.feedId) {
|
||||
let feed: Feed = {
|
||||
if with.url == FeedlyFeedResourceId(id: feedlyFeed.id).url {
|
||||
with.metadata.feedID = feedlyFeed.id
|
||||
with.name = feedlyFeed.title
|
||||
with.homePageURL = feedlyFeed.website
|
||||
return with
|
||||
} else {
|
||||
let resourceId = FeedlyFeedResourceId(id: feedlyFeed.id)
|
||||
return account.createFeed(with: feedlyFeed.title,
|
||||
url: resourceId.url,
|
||||
feedID: feedlyFeed.id,
|
||||
homePageURL: feedlyFeed.website)
|
||||
}
|
||||
}()
|
||||
folder.addFeed(feed)
|
||||
}
|
||||
|
||||
case .success:
|
||||
completion(.success(()))
|
||||
|
||||
case .failure(let error):
|
||||
completion(.failure(error))
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ enum FeedlyAccountDelegateError: LocalizedError {
|
|||
case unableToMoveFeedBetweenFolders(Feed, Folder, Folder)
|
||||
case addFeedChooseFolder
|
||||
case addFeedInvalidFolder(Folder)
|
||||
case unableToRenameFeed(String, String)
|
||||
case unableToRemoveFeed(Feed)
|
||||
|
||||
var errorDescription: String? {
|
||||
|
@ -46,6 +47,10 @@ enum FeedlyAccountDelegateError: LocalizedError {
|
|||
let template = NSLocalizedString("Feeds cannot be added to the \"%@\" folder.", comment: "Feedly - Feed can only be added to folders.")
|
||||
return String(format: template, invalidFolder.nameForDisplay)
|
||||
|
||||
case .unableToRenameFeed(let from, let to):
|
||||
let template = NSLocalizedString("Could not rename \"%@\" to \"%@\".", comment: "Feedly - Could not rename a feed.")
|
||||
return String(format: template, from, to)
|
||||
|
||||
case .unableToRemoveFeed(let feed):
|
||||
let template = NSLocalizedString("Could not remove \"%@\".", comment: "Feedly - Could not remove a feed.")
|
||||
return String(format: template, feed.nameForDisplay)
|
||||
|
@ -78,6 +83,9 @@ enum FeedlyAccountDelegateError: LocalizedError {
|
|||
|
||||
case .unableToRemoveFeed:
|
||||
return nil
|
||||
|
||||
case .unableToRenameFeed:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,14 @@
|
|||
//
|
||||
// FeedlyCreateFeedOperation.swift
|
||||
// FeedlyAddFeedOperation.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Kiel Gillard on 10/10/19.
|
||||
// Created by Kiel Gillard on 11/10/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol FeedlyResourceProviding {
|
||||
var resource: FeedlyResourceId { get }
|
||||
}
|
||||
|
||||
final class FeedlySubscribeToFeedOperation: FeedlyOperation, FeedlyFeedsAndFoldersProviding, FeedlyResourceProviding {
|
||||
let url: String
|
||||
final class FeedlyAddFeedOperation: FeedlyOperation, FeedlyFeedsAndFoldersProviding, FeedlyResourceProviding {
|
||||
let feedName: String?
|
||||
let collectionId: String
|
||||
let caller: FeedlyAPICaller
|
||||
|
@ -21,11 +16,10 @@ final class FeedlySubscribeToFeedOperation: FeedlyOperation, FeedlyFeedsAndFolde
|
|||
let folder: Folder
|
||||
let feedResource: FeedlyFeedResourceId
|
||||
|
||||
init(account: Account, folder: Folder, url: String, feedName: String?, collectionId: String, caller: FeedlyAPICaller) {
|
||||
init(account: Account, folder: Folder, feedResource: FeedlyFeedResourceId, feedName: String? = nil, collectionId: String, caller: FeedlyAPICaller) {
|
||||
self.account = account
|
||||
self.folder = folder
|
||||
self.url = url
|
||||
self.feedResource = FeedlyFeedResourceId(url: url)
|
||||
self.feedResource = feedResource
|
||||
self.feedName = feedName
|
||||
self.collectionId = collectionId
|
||||
self.caller = caller
|
|
@ -0,0 +1,130 @@
|
|||
//
|
||||
// FeedlyCreateFeedRequest.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Kiel Gillard on 10/10/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import os.log
|
||||
|
||||
final class FeedlyAddFeedRequest {
|
||||
let account: Account
|
||||
let caller: FeedlyAPICaller
|
||||
let container: Container
|
||||
let log: OSLog
|
||||
|
||||
init(account: Account, caller: FeedlyAPICaller, container: Container, log: OSLog) {
|
||||
self.account = account
|
||||
self.caller = caller
|
||||
self.container = container
|
||||
self.log = log
|
||||
}
|
||||
|
||||
private class Delegate: FeedlyOperationDelegate {
|
||||
let resourceProvider: FeedlyResourceProviding
|
||||
|
||||
init(resourceProvider: FeedlyResourceProviding) {
|
||||
self.resourceProvider = resourceProvider
|
||||
}
|
||||
|
||||
var completionHandler: ((Result<Feed, Error>) -> ())?
|
||||
var error: Error?
|
||||
|
||||
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
||||
self.error = error
|
||||
}
|
||||
}
|
||||
|
||||
func addNewFeed(at url: String, name: String? = nil, completion: @escaping (Result<Feed, Error>) -> Void) {
|
||||
let resource = FeedlyFeedResourceId(url: url)
|
||||
self.start(resource: resource, name: name, refreshes: true, completion: completion)
|
||||
}
|
||||
|
||||
func add(existing feed: Feed, name: String? = nil, completion: @escaping (Result<Feed, Error>) -> Void) {
|
||||
let resource = FeedlyFeedResourceId(id: feed.feedID)
|
||||
self.start(resource: resource, name: name, refreshes: false, completion: completion)
|
||||
}
|
||||
|
||||
private func start(resource: FeedlyFeedResourceId, name: String?, refreshes: Bool, completion: @escaping (Result<Feed, Error>) -> Void) {
|
||||
|
||||
let (folder, collectionId): (Folder, String)
|
||||
do {
|
||||
let validator = FeedlyFeedContainerValidator(container: container, userId: caller.credentials?.username)
|
||||
(folder, collectionId) = try validator.getValidContainer()
|
||||
} catch {
|
||||
return DispatchQueue.main.async {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
let delegate = Delegate(resourceProvider: resource)
|
||||
delegate.completionHandler = completion
|
||||
|
||||
let createFeed = FeedlyCompoundOperation() {
|
||||
let addRequest = FeedlyAddFeedOperation(account: account, folder: folder, feedResource: resource, feedName: name, collectionId: collectionId, caller: caller)
|
||||
|
||||
let createFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: addRequest, log: log)
|
||||
createFeeds.addDependency(addRequest)
|
||||
|
||||
let getStream: FeedlyGetStreamOperation? = {
|
||||
if refreshes {
|
||||
let op = FeedlyGetStreamOperation(account: account, resourceProvider: addRequest, caller: caller, newerThan: nil)
|
||||
op.addDependency(createFeeds)
|
||||
return op
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
let organiseByFeed: FeedlyOrganiseParsedItemsByFeedOperation? = {
|
||||
if let getStream = getStream {
|
||||
let op = FeedlyOrganiseParsedItemsByFeedOperation(account: account, entryProvider: getStream, log: log)
|
||||
op.addDependency(getStream)
|
||||
return op
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
let updateAccount: FeedlyUpdateAccountFeedsWithItemsOperation? = {
|
||||
if let organiseByFeed = organiseByFeed {
|
||||
let op = FeedlyUpdateAccountFeedsWithItemsOperation(account: account, organisedItemsProvider: organiseByFeed, log: log)
|
||||
op.addDependency(organiseByFeed)
|
||||
return op
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
|
||||
let operations = [addRequest, createFeeds, getStream, organiseByFeed, updateAccount].compactMap { $0 }
|
||||
|
||||
for operation in operations {
|
||||
assert(operation.isReady == (operation === addRequest), "Only the add request operation should be ready.")
|
||||
operation.delegate = delegate
|
||||
}
|
||||
|
||||
return operations
|
||||
}
|
||||
|
||||
let callback = BlockOperation() {
|
||||
guard let handler = delegate.completionHandler else {
|
||||
return
|
||||
}
|
||||
|
||||
defer { delegate.completionHandler = nil }
|
||||
|
||||
if let error = delegate.error {
|
||||
handler(.failure(error))
|
||||
|
||||
} else if let feed = folder.existingFeed(withFeedID: resource.id) {
|
||||
handler(.success(feed))
|
||||
|
||||
} else {
|
||||
handler(.failure(AccountError.createErrorNotFound))
|
||||
}
|
||||
}
|
||||
|
||||
callback.addDependency(createFeed)
|
||||
|
||||
OperationQueue.main.addOperations([createFeed, callback], waitUntilFinished: false)
|
||||
}
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
//
|
||||
// FeedlyCreateFeedRequest.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Kiel Gillard on 10/10/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import os.log
|
||||
|
||||
final class FeedlyCreateFeedRequest {
|
||||
let account: Account
|
||||
let caller: FeedlyAPICaller
|
||||
let container: Container
|
||||
let log: OSLog
|
||||
|
||||
init(account: Account, caller: FeedlyAPICaller, container: Container, log: OSLog) {
|
||||
self.account = account
|
||||
self.caller = caller
|
||||
self.container = container
|
||||
self.log = log
|
||||
}
|
||||
|
||||
private class Delegate: FeedlyOperationDelegate {
|
||||
let resourceProvider: FeedlyResourceProviding
|
||||
|
||||
init(resourceProvider: FeedlyResourceProviding) {
|
||||
self.resourceProvider = resourceProvider
|
||||
}
|
||||
|
||||
var completionHandler: ((Result<Feed, Error>) -> ())?
|
||||
var error: Error?
|
||||
|
||||
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
||||
self.error = error
|
||||
}
|
||||
}
|
||||
|
||||
func start(url: String, name: String?, completion: @escaping (Result<Feed, Error>) -> Void) {
|
||||
|
||||
let (folder, collectionId): (Folder, String)
|
||||
do {
|
||||
let validator = FeedlyFeedContainerValidator(container: container, userId: caller.credentials?.username)
|
||||
(folder, collectionId) = try validator.getValidContainer()
|
||||
} catch {
|
||||
return DispatchQueue.main.async {
|
||||
completion(.failure(error))
|
||||
}
|
||||
}
|
||||
|
||||
let subscribeRequest = FeedlySubscribeToFeedOperation(account: account, folder: folder, url: url, feedName: name, collectionId: collectionId, caller: caller)
|
||||
|
||||
let delegate = Delegate(resourceProvider: subscribeRequest)
|
||||
delegate.completionHandler = completion
|
||||
|
||||
let createFeed = FeedlyCompoundOperation() {
|
||||
let createFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: subscribeRequest, log: log)
|
||||
let getStream = FeedlyGetStreamOperation(account: account, resourceProvider: subscribeRequest, caller: caller, newerThan: nil)
|
||||
let organiseByFeed = FeedlyOrganiseParsedItemsByFeedOperation(account: account, entryProvider: getStream, log: log)
|
||||
let updateAccount = FeedlyUpdateAccountFeedsWithItemsOperation(account: account, organisedItemsProvider: organiseByFeed, log: log)
|
||||
|
||||
createFeeds.addDependency(subscribeRequest)
|
||||
getStream.addDependency(createFeeds)
|
||||
organiseByFeed.addDependency(getStream)
|
||||
updateAccount.addDependency(organiseByFeed)
|
||||
|
||||
let operations = [subscribeRequest, createFeeds, getStream, organiseByFeed, updateAccount]
|
||||
|
||||
for operation in operations {
|
||||
operation.delegate = delegate
|
||||
}
|
||||
|
||||
return operations
|
||||
}
|
||||
|
||||
let callback = BlockOperation() {
|
||||
guard let handler = delegate.completionHandler else {
|
||||
return
|
||||
}
|
||||
|
||||
defer { delegate.completionHandler = nil }
|
||||
|
||||
if let error = delegate.error {
|
||||
handler(.failure(error))
|
||||
|
||||
} else if let feed = folder.existingFeed(withFeedID: subscribeRequest.resource.id) {
|
||||
handler(.success(feed))
|
||||
|
||||
} else {
|
||||
handler(.failure(AccountError.createErrorNotFound))
|
||||
}
|
||||
}
|
||||
|
||||
callback.addDependency(createFeed)
|
||||
|
||||
OperationQueue.main.addOperations([createFeed, callback], waitUntilFinished: false)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
//
|
||||
// FeedlyResourceProviding.swift
|
||||
// Account
|
||||
//
|
||||
// Created by Kiel Gillard on 11/10/19.
|
||||
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol FeedlyResourceProviding {
|
||||
var resource: FeedlyResourceId { get }
|
||||
}
|
||||
|
||||
extension FeedlyFeedResourceId: FeedlyResourceProviding {
|
||||
|
||||
var resource: FeedlyResourceId {
|
||||
return self
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue