2019-10-10 00:15:22 +02:00
|
|
|
//
|
|
|
|
// FeedlyGetStreamOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 20/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSParser
|
|
|
|
|
|
|
|
protocol FeedlyEntryProviding: class {
|
|
|
|
var resource: FeedlyResourceId { get }
|
|
|
|
var entries: [FeedlyEntry] { get }
|
|
|
|
var parsedEntries: Set<ParsedItem> { get }
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
protocol FeedlyGetStreamOperationDelegate: class {
|
|
|
|
func feedlyGetStreamOperation(_ operation: FeedlyGetStreamOperation, didGet stream: FeedlyStream)
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
/// Single responsibility is to get the stream content of a Collection from Feedly.
|
|
|
|
final class FeedlyGetStreamOperation: FeedlyOperation, FeedlyEntryProviding {
|
|
|
|
|
2019-10-10 12:24:45 +02:00
|
|
|
struct ResourceProvider: FeedlyResourceProviding {
|
|
|
|
var resource: FeedlyResourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
let resourceProvider: FeedlyResourceProviding
|
|
|
|
|
|
|
|
var resource: FeedlyResourceId {
|
|
|
|
return resourceProvider.resource
|
|
|
|
}
|
2019-10-10 00:15:22 +02:00
|
|
|
|
|
|
|
var entries: [FeedlyEntry] {
|
|
|
|
guard let entries = storedStream?.items else {
|
2019-10-16 13:12:37 +02:00
|
|
|
assert(isFinished, "This should only be called when the operation finishes without error.")
|
|
|
|
assertionFailure("Has this operation been addeded as a dependency on the caller?")
|
2019-10-10 00:15:22 +02:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
return entries
|
|
|
|
}
|
|
|
|
|
|
|
|
var parsedEntries: Set<ParsedItem> {
|
|
|
|
if let entries = storedParsedEntries {
|
|
|
|
return entries
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsed = Set(entries.map { FeedlyEntryParser(entry: $0).parsedItemRepresentation })
|
|
|
|
storedParsedEntries = parsed
|
|
|
|
|
|
|
|
return parsed
|
|
|
|
}
|
|
|
|
|
|
|
|
private var storedStream: FeedlyStream? {
|
|
|
|
didSet {
|
|
|
|
storedParsedEntries = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var storedParsedEntries: Set<ParsedItem>?
|
|
|
|
|
|
|
|
let account: Account
|
|
|
|
let caller: FeedlyAPICaller
|
|
|
|
let unreadOnly: Bool?
|
|
|
|
let newerThan: Date?
|
2019-10-16 13:12:37 +02:00
|
|
|
let continuation: String?
|
|
|
|
|
|
|
|
weak var streamDelegate: FeedlyGetStreamOperationDelegate?
|
2019-10-10 00:15:22 +02:00
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
init(account: Account, resource: FeedlyResourceId, caller: FeedlyAPICaller, continuation: String? = nil, newerThan: Date?, unreadOnly: Bool? = nil) {
|
2019-10-10 00:15:22 +02:00
|
|
|
self.account = account
|
2019-10-10 12:24:45 +02:00
|
|
|
self.resourceProvider = ResourceProvider(resource: resource)
|
2019-10-10 00:15:22 +02:00
|
|
|
self.caller = caller
|
2019-10-16 13:12:37 +02:00
|
|
|
self.continuation = continuation
|
2019-10-10 00:15:22 +02:00
|
|
|
self.unreadOnly = unreadOnly
|
|
|
|
self.newerThan = newerThan
|
|
|
|
}
|
|
|
|
|
2019-10-10 12:24:45 +02:00
|
|
|
convenience init(account: Account, resourceProvider: FeedlyResourceProviding, caller: FeedlyAPICaller, newerThan: Date?, unreadOnly: Bool? = nil) {
|
|
|
|
self.init(account: account, resource: resourceProvider.resource, caller: caller, newerThan: newerThan, unreadOnly: unreadOnly)
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
override func main() {
|
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-16 13:12:37 +02:00
|
|
|
caller.getStream(for: resourceProvider.resource, continuation: continuation, newerThan: newerThan, unreadOnly: unreadOnly) { result in
|
2019-10-10 00:15:22 +02:00
|
|
|
switch result {
|
|
|
|
case .success(let stream):
|
|
|
|
self.storedStream = stream
|
2019-10-16 13:12:37 +02:00
|
|
|
|
|
|
|
self.streamDelegate?.feedlyGetStreamOperation(self, didGet: stream)
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
self.didFinish()
|
2019-10-16 13:12:37 +02:00
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
case .failure(let error):
|
|
|
|
self.didFinish(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|