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
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
protocol FeedlyFeedsAndFoldersProviding {
|
|
|
|
var feedsAndFolders: [([FeedlyFeed], Folder)] { get }
|
|
|
|
}
|
|
|
|
|
2020-01-19 23:19:06 +01:00
|
|
|
/// Reflect Collections from Feedly as Folders.
|
2020-01-29 08:28:37 +01:00
|
|
|
final class FeedlyMirrorCollectionsAsFoldersOperation: FeedlyOperation, 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
|
|
|
|
2019-10-10 00:15:22 +02:00
|
|
|
private(set) var feedsAndFolders = [([FeedlyFeed], Folder)]()
|
2020-01-19 23:19:06 +01: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
|
|
|
}
|
|
|
|
|
2020-01-16 06:30:37 +01:00
|
|
|
override func run() {
|
|
|
|
defer {
|
|
|
|
didFinish()
|
|
|
|
}
|
2019-09-19 04:56:43 +02:00
|
|
|
|
|
|
|
let localFolders = account.folders ?? Set()
|
|
|
|
let collections = collectionsProvider.collections
|
|
|
|
|
2020-01-29 08:28:37 +01:00
|
|
|
feedsAndFolders = collections.compactMap { collection -> ([FeedlyFeed], Folder)? in
|
|
|
|
let parser = FeedlyCollectionParser(collection: collection)
|
|
|
|
guard let folder = account.ensureFolder(with: parser.folderName) else {
|
2019-09-23 09:29:53 +02:00
|
|
|
assertionFailure("Why wasn't a folder created?")
|
2019-09-19 04:56:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-29 08:28:37 +01:00
|
|
|
folder.externalID = parser.externalID
|
2019-10-10 00:15:22 +02:00
|
|
|
return (collection.feeds, folder)
|
|
|
|
}
|
|
|
|
|
2020-01-29 08:28:37 +01:00
|
|
|
os_log(.debug, log: log, "Ensured %i folders for %i collections.", feedsAndFolders.count, collections.count)
|
|
|
|
|
2019-09-19 04:56:43 +02:00
|
|
|
// Remove folders without a corresponding collection
|
2020-01-29 08:28:37 +01:00
|
|
|
let collectionFolders = Set(feedsAndFolders.map { $0.1 })
|
2019-09-19 04:56:43 +02:00
|
|
|
let foldersWithoutCollections = localFolders.subtracting(collectionFolders)
|
2019-09-23 09:29:53 +02:00
|
|
|
|
2020-01-29 08:28:37 +01:00
|
|
|
if !foldersWithoutCollections.isEmpty {
|
|
|
|
for unmatched in foldersWithoutCollections {
|
|
|
|
account.removeFolder(unmatched)
|
|
|
|
}
|
|
|
|
|
|
|
|
os_log(.debug, log: log, "Removed %i folders: %@", foldersWithoutCollections.count, foldersWithoutCollections.map { $0.externalID ?? $0.nameForDisplay })
|
|
|
|
}
|
2019-09-19 04:56:43 +02:00
|
|
|
}
|
|
|
|
}
|