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
|
2019-11-27 09:04:55 +01:00
|
|
|
import os.log
|
2019-10-10 00:15:22 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
protocol FeedlyEntryProviding {
|
2019-10-10 00:15:22 +02:00
|
|
|
var entries: [FeedlyEntry] { get }
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protocol FeedlyParsedItemProviding {
|
|
|
|
var resource: FeedlyResourceId { get }
|
2019-10-10 00:15:22 +02:00
|
|
|
var parsedEntries: Set<ParsedItem> { get }
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
protocol FeedlyGetStreamContentsOperationDelegate: class {
|
|
|
|
func feedlyGetStreamContentsOperation(_ operation: FeedlyGetStreamContentsOperation, didGetContentsOf stream: FeedlyStream)
|
2019-10-16 13:12:37 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
/// Single responsibility is to get the stream content of a Collection from Feedly.
|
2019-10-18 23:21:02 +02:00
|
|
|
final class FeedlyGetStreamContentsOperation: FeedlyOperation, FeedlyEntryProviding, FeedlyParsedItemProviding {
|
2019-10-10 00:15:22 +02:00
|
|
|
|
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] {
|
2019-10-18 23:21:02 +02:00
|
|
|
guard let entries = stream?.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
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
private(set) var stream: FeedlyStream? {
|
2019-10-10 00:15:22 +02:00
|
|
|
didSet {
|
|
|
|
storedParsedEntries = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private var storedParsedEntries: Set<ParsedItem>?
|
|
|
|
|
|
|
|
let account: Account
|
2019-10-18 23:21:02 +02:00
|
|
|
let service: FeedlyGetStreamContentsService
|
2019-10-10 00:15:22 +02:00
|
|
|
let unreadOnly: Bool?
|
|
|
|
let newerThan: Date?
|
2019-10-16 13:12:37 +02:00
|
|
|
let continuation: String?
|
2019-11-27 09:04:55 +01:00
|
|
|
let log: OSLog
|
2019-10-16 13:12:37 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
weak var streamDelegate: FeedlyGetStreamContentsOperationDelegate?
|
2019-10-10 00:15:22 +02:00
|
|
|
|
2019-11-27 09:04:55 +01:00
|
|
|
init(account: Account, resource: FeedlyResourceId, service: FeedlyGetStreamContentsService, continuation: String? = nil, newerThan: Date?, unreadOnly: Bool? = nil, log: OSLog) {
|
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-18 23:21:02 +02:00
|
|
|
self.service = service
|
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-11-27 09:04:55 +01:00
|
|
|
self.log = log
|
2019-10-10 00:15:22 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 09:04:55 +01:00
|
|
|
convenience init(account: Account, resourceProvider: FeedlyResourceProviding, service: FeedlyGetStreamContentsService, newerThan: Date?, unreadOnly: Bool? = nil, log: OSLog) {
|
|
|
|
self.init(account: account, resource: resourceProvider.resource, service: service, newerThan: newerThan, unreadOnly: unreadOnly, log: log)
|
2019-10-10 12:24:45 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
override func main() {
|
|
|
|
guard !isCancelled else {
|
|
|
|
didFinish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
service.getStreamContents(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):
|
2019-10-18 23:21:02 +02:00
|
|
|
self.stream = stream
|
2019-10-16 13:12:37 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
self.streamDelegate?.feedlyGetStreamContentsOperation(self, didGetContentsOf: stream)
|
2019-10-16 13:12:37 +02:00
|
|
|
|
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):
|
2019-11-27 09:04:55 +01:00
|
|
|
os_log(.debug, log: self.log, "Unable to get stream contents: %{public}@.", error as NSError)
|
2019-10-10 00:15:22 +02:00
|
|
|
self.didFinish(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|