2019-09-19 04:56:43 +02:00
|
|
|
//
|
|
|
|
// FeedlyMirrorCollectionsAsFoldersOperation.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 20/9/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2019-09-23 09:29:53 +02:00
|
|
|
import os.log
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
protocol FeedlyCollectionsAndFoldersProviding: class {
|
|
|
|
var collectionsAndFolders: [(FeedlyCollection, Folder)] { get }
|
|
|
|
}
|
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
protocol FeedlyFeedsAndFoldersProviding {
|
|
|
|
var feedsAndFolders: [([FeedlyFeed], Folder)] { get }
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
/// Single responsibility is accurately reflect Collections from Feedly as Folders.
|
2019-10-10 00:15:22 +02:00
|
|
|
final class FeedlyMirrorCollectionsAsFoldersOperation: FeedlyOperation, FeedlyCollectionsAndFoldersProviding, FeedlyFeedsAndFoldersProviding {
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
let account: Account
|
|
|
|
let collectionsProvider: FeedlyCollectionProviding
|
2019-09-23 09:29:53 +02:00
|
|
|
let log: OSLog
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
private(set) var collectionsAndFolders = [(FeedlyCollection, Folder)]()
|
2019-10-10 00:15:22 +02:00
|
|
|
private(set) var feedsAndFolders = [([FeedlyFeed], Folder)]()
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-10-18 23:21:02 +02:00
|
|
|
init(account: Account, collectionsProvider: FeedlyCollectionProviding, log: OSLog) {
|
2019-09-19 04:56:43 +02:00
|
|
|
self.collectionsProvider = collectionsProvider
|
|
|
|
self.account = account
|
2019-09-23 09:29:53 +02:00
|
|
|
self.log = log
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override func main() {
|
|
|
|
defer { didFinish() }
|
|
|
|
|
|
|
|
guard !isCancelled else { return }
|
|
|
|
|
|
|
|
let localFolders = account.folders ?? Set()
|
|
|
|
let collections = collectionsProvider.collections
|
|
|
|
|
|
|
|
let pairs = collections.compactMap { collection -> (FeedlyCollection, Folder)? in
|
2019-09-23 09:29:53 +02:00
|
|
|
guard let folder = account.ensureFolder(with: collection.label) else {
|
|
|
|
assertionFailure("Why wasn't a folder created?")
|
2019-09-19 04:56:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-09-23 09:29:53 +02:00
|
|
|
folder.externalID = collection.id
|
|
|
|
return (collection, folder)
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
collectionsAndFolders = pairs
|
2019-09-23 09:29:53 +02:00
|
|
|
os_log(.debug, log: log, "Ensured %i folders for %i collections.", pairs.count, collections.count)
|
2019-09-19 04:56:43 +02:00
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
feedsAndFolders = pairs.map { (collection, folder) -> (([FeedlyFeed], Folder)) in
|
|
|
|
return (collection.feeds, folder)
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
// Remove folders without a corresponding collection
|
|
|
|
let collectionFolders = Set(pairs.map { $0.1 })
|
|
|
|
let foldersWithoutCollections = localFolders.subtracting(collectionFolders)
|
|
|
|
for unmatched in foldersWithoutCollections {
|
|
|
|
account.removeFolder(unmatched)
|
|
|
|
}
|
2019-09-23 09:29:53 +02:00
|
|
|
|
|
|
|
os_log(.debug, log: log, "Removed %i folders: %@", foldersWithoutCollections.count, foldersWithoutCollections.map { $0.externalID ?? $0.nameForDisplay })
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
}
|