NetNewsWire/Importers/DefaultFeedsImporter.swift

60 lines
1.4 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// DefaultFeedsImporter.swift
2018-08-29 07:18:24 +02:00
// NetNewsWire
2017-05-27 19:43:27 +02:00
//
// Created by Brent Simmons on 8/13/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Articles
2017-09-17 21:34:10 +02:00
import Account
import RSCore
2017-05-27 19:43:27 +02:00
2017-09-27 06:43:40 +02:00
typealias DiskFeedDictionary = [String: Any]
2017-09-25 07:35:57 +02:00
struct DefaultFeedsImporter {
2017-05-27 19:43:27 +02:00
2017-09-25 07:35:57 +02:00
static func importIfNeeded(_ firstRun: Bool, account: Account) {
if shouldImportDefaultFeeds(firstRun) {
appDelegate.logDebugMessage("Importing default feeds.")
FeedsImporter.importFeeds(defaultFeeds(), account: account)
2017-09-25 07:35:57 +02:00
}
2017-05-27 19:43:27 +02:00
}
2017-09-25 07:35:57 +02:00
private static func defaultFeeds() -> [DiskFeedDictionary] {
let f = Bundle.main.path(forResource: "DefaultFeeds", ofType: "plist")!
return NSArray(contentsOfFile: f)! as! [DiskFeedDictionary]
}
private static func shouldImportDefaultFeeds(_ isFirstRun: Bool) -> Bool {
2017-10-02 22:15:07 +02:00
if !isFirstRun || AccountManager.shared.anyAccountHasAtLeastOneFeed() {
2017-05-27 19:43:27 +02:00
return false
}
2017-09-25 07:35:57 +02:00
return true
2017-05-27 19:43:27 +02:00
}
}
2017-09-25 07:35:57 +02:00
struct FeedsImporter {
2017-05-27 19:43:27 +02:00
2017-09-27 06:43:40 +02:00
static func importFeeds(_ feedDictionaries: [DiskFeedDictionary], account: Account) {
2017-09-25 07:35:57 +02:00
let feedsToImport = feeds(with: feedDictionaries, account: account)
2017-10-06 05:34:29 +02:00
2017-10-06 05:38:54 +02:00
BatchUpdate.shared.perform {
feedsToImport.forEach{ account.addFeed($0) }
2017-09-27 06:43:40 +02:00
}
2018-09-27 07:05:35 +02:00
account.structureDidChange()
2017-09-25 07:35:57 +02:00
}
2017-10-06 05:34:29 +02:00
private static func feeds(with feedDictionaries: [DiskFeedDictionary], account: Account) -> Set<Feed> {
let feedArray = feedDictionaries.compactMap { Feed(account: account, dictionary: $0) }
return Set(feedArray)
2017-05-27 19:43:27 +02:00
}
}