2019-09-19 04:56:43 +02:00
|
|
|
//
|
|
|
|
// FeedlyOrganiseParsedItemsByFeedOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 20/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import RSParser
|
|
|
|
import os.log
|
|
|
|
|
|
|
|
protocol FeedlyParsedItemsByFeedProviding {
|
2019-10-15 09:31:24 +02:00
|
|
|
var providerName: String { get }
|
2019-10-16 08:35:28 +02:00
|
|
|
var parsedItemsKeyedByFeedId: [String: Set<ParsedItem>] { get }
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Single responsibility is to group articles by their feeds.
|
2019-09-23 09:29:53 +02:00
|
|
|
final class FeedlyOrganiseParsedItemsByFeedOperation: FeedlyOperation, FeedlyParsedItemsByFeedProviding {
|
2019-09-19 04:56:43 +02:00
|
|
|
private let account: Account
|
2019-10-18 23:21:02 +02:00
|
|
|
private let parsedItemProvider: FeedlyParsedItemProviding
|
2019-09-19 04:56:43 +02:00
|
|
|
private let log: OSLog
|
|
|
|
|
2019-10-16 08:35:28 +02:00
|
|
|
var parsedItemsKeyedByFeedId: [String : Set<ParsedItem>] {
|
2019-10-15 09:31:24 +02:00
|
|
|
assert(Thread.isMainThread) // Needs to be on main thread because Feed is a main-thread-only model type.
|
2019-10-16 08:35:28 +02:00
|
|
|
return itemsKeyedByFeedId
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
|
2019-10-15 09:31:24 +02:00
|
|
|
var providerName: String {
|
2019-10-18 23:21:02 +02:00
|
|
|
return parsedItemProvider.resource.id
|
2019-10-15 09:31:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
private var itemsKeyedByFeedId = [String: Set<ParsedItem>]()
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
init(account: Account, parsedItemProvider: FeedlyParsedItemProviding, log: OSLog) {
|
2019-09-19 04:56:43 +02:00
|
|
|
self.account = account
|
2019-10-18 23:21:02 +02:00
|
|
|
self.parsedItemProvider = parsedItemProvider
|
2019-09-19 04:56:43 +02:00
|
|
|
self.log = log
|
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
|
|
|
defer { didFinish() }
|
|
|
|
|
|
|
|
guard !isCancelled else { return }
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
let items = parsedItemProvider.parsedEntries
|
2019-09-19 04:56:43 +02:00
|
|
|
var dict = [String: Set<ParsedItem>](minimumCapacity: items.count)
|
|
|
|
|
|
|
|
for item in items {
|
|
|
|
let key = item.feedURL
|
|
|
|
let value: Set<ParsedItem> = {
|
|
|
|
if var items = dict[key] {
|
|
|
|
items.insert(item)
|
|
|
|
return items
|
|
|
|
} else {
|
|
|
|
return [item]
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
dict[key] = value
|
|
|
|
|
|
|
|
guard !isCancelled else { return }
|
|
|
|
}
|
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
os_log(.debug, log: log, "Grouped %i items by %i feeds for %@", items.count, dict.count, parsedItemProvider.resource.id)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
itemsKeyedByFeedId = dict
|
|
|
|
}
|
|
|
|
}
|