2019-12-01 21:39:05 +01:00
|
|
|
//
|
|
|
|
// FeedlySearchOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 1/12/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
protocol FeedlySearchService: class {
|
2019-12-15 01:14:55 +01:00
|
|
|
func getFeeds(for query: String, count: Int, locale: String, completion: @escaping (Result<FeedlyFeedsSearchResponse, Error>) -> ())
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protocol FeedlySearchOperationDelegate: class {
|
|
|
|
func feedlySearchOperation(_ operation: FeedlySearchOperation, didGet response: FeedlyFeedsSearchResponse)
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
/// Find one and only one feed for a given query (usually, a URL).
|
2019-12-01 21:39:05 +01:00
|
|
|
/// What happens when a feed is found for the URL is delegated to the `searchDelegate`.
|
|
|
|
class FeedlySearchOperation: FeedlyOperation {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
let query: String
|
|
|
|
let locale: Locale
|
|
|
|
let searchService: FeedlySearchService
|
|
|
|
weak var searchDelegate: FeedlySearchOperationDelegate?
|
2020-01-19 23:19:06 +01:00
|
|
|
|
2019-12-01 21:39:05 +01:00
|
|
|
init(query: String, locale: Locale = .current, service: FeedlySearchService) {
|
|
|
|
self.query = query
|
|
|
|
self.locale = locale
|
|
|
|
self.searchService = service
|
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
2019-12-01 21:39:05 +01:00
|
|
|
searchService.getFeeds(for: query, count: 1, locale: locale.identifier) { result in
|
|
|
|
switch result {
|
|
|
|
case .success(let response):
|
|
|
|
assert(Thread.isMainThread)
|
|
|
|
self.searchDelegate?.feedlySearchOperation(self, didGet: response)
|
|
|
|
self.didFinish()
|
|
|
|
|
|
|
|
case .failure(let error):
|
2020-01-19 23:19:06 +01:00
|
|
|
self.didFinish(with: error)
|
2019-12-01 21:39:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|