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 {
|
2020-01-09 06:24:47 +01:00
|
|
|
var parsedItemsByFeedProviderName: String { get }
|
2019-10-16 08:35:28 +02:00
|
|
|
var parsedItemsKeyedByFeedId: [String: Set<ParsedItem>] { get }
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
/// Group articles by their feeds.
|
2019-09-23 09:29:53 +02:00
|
|
|
final class FeedlyOrganiseParsedItemsByFeedOperation: FeedlyOperation, FeedlyParsedItemsByFeedProviding {
|
2020-01-19 23:19:06 +01:00
|
|
|
|
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
|
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
var parsedItemsByFeedProviderName: String {
|
|
|
|
return name ?? String(describing: Self.self)
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:35:28 +02:00
|
|
|
var parsedItemsKeyedByFeedId: [String : Set<ParsedItem>] {
|
2020-01-16 06:30:37 +01:00
|
|
|
precondition(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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
|
|
|
defer {
|
|
|
|
didFinish()
|
|
|
|
}
|
2020-01-19 23:19:06 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:24:47 +01:00
|
|
|
os_log(.debug, log: log, "Grouped %i items by %i feeds for %@", items.count, dict.count, parsedItemProvider.parsedItemProviderName)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
itemsKeyedByFeedId = dict
|
|
|
|
}
|
|
|
|
}
|