NetNewsWire/Frameworks/Account/Feedly/Operations/FeedlyAddFeedToCollectionOperation.swift

73 lines
1.9 KiB
Swift
Raw Normal View History

2019-10-10 12:24:45 +02:00
//
// FeedlyAddFeedToCollectionOperation.swift
2019-10-10 12:24:45 +02:00
// Account
//
2019-10-11 09:14:23 +02:00
// Created by Kiel Gillard on 11/10/19.
2019-10-10 12:24:45 +02:00
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
protocol FeedlyAddFeedToCollectionService {
func addFeed(with feedId: FeedlyFeedResourceId, title: String?, toCollectionWith collectionId: String, completion: @escaping (Result<[FeedlyFeed], Error>) -> ())
}
final class FeedlyAddFeedToCollectionOperation: FeedlyOperation, FeedlyFeedsAndFoldersProviding, FeedlyResourceProviding {
2019-10-10 12:24:45 +02:00
let feedName: String?
let collectionId: String
let service: FeedlyAddFeedToCollectionService
2019-10-10 12:24:45 +02:00
let account: Account
let folder: Folder
let feedResource: FeedlyFeedResourceId
init(account: Account, folder: Folder, feedResource: FeedlyFeedResourceId, feedName: String? = nil, collectionId: String, service: FeedlyAddFeedToCollectionService) {
2019-10-10 12:24:45 +02:00
self.account = account
self.folder = folder
2019-10-11 09:14:23 +02:00
self.feedResource = feedResource
2019-10-10 12:24:45 +02:00
self.feedName = feedName
self.collectionId = collectionId
self.service = service
2019-10-10 12:24:45 +02:00
}
private(set) var feedsAndFolders = [([FeedlyFeed], Folder)]()
var resource: FeedlyResourceId {
return feedResource
}
override func run() {
service.addFeed(with: feedResource, title: feedName, toCollectionWith: collectionId) { [weak self] result in
guard let self = self else {
return
}
if self.isCanceled {
self.didFinish()
return
}
2019-10-10 12:24:45 +02:00
self.didCompleteRequest(result)
}
}
}
private extension FeedlyAddFeedToCollectionOperation {
func didCompleteRequest(_ result: Result<[FeedlyFeed], Error>) {
2019-10-10 12:24:45 +02:00
switch result {
case .success(let feedlyFeeds):
feedsAndFolders = [(feedlyFeeds, folder)]
let feedsWithCreatedFeedId = feedlyFeeds.filter { $0.id == resource.id }
2019-10-10 12:24:45 +02:00
if feedsWithCreatedFeedId.isEmpty {
didFinish(with: AccountError.createErrorNotFound)
2019-10-10 12:24:45 +02:00
} else {
didFinish()
}
case .failure(let error):
didFinish(with: error)
2019-10-10 12:24:45 +02:00
}
}
}