NetNewsWire/Importers/DefaultFeedsImporter.swift

61 lines
1.4 KiB
Swift
Raw Normal View History

2017-05-27 19:43:27 +02:00
//
// DefaultFeedsImporter.swift
// Evergreen
//
// Created by Brent Simmons on 8/13/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Data
2017-09-17 21:34:10 +02:00
import Account
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) {
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
2017-09-27 06:43:40 +02:00
let feedsToImport = feeds(with: feedDictionaries, accountID: account.accountID)
2017-10-06 05:34:29 +02:00
batchUpdate.perform {
for feed in feedsToImport {
if !account.hasFeed(with: feed.feedID) {
let _ = account.addFeed(feed, to: nil)
}
2017-09-27 06:43:40 +02:00
}
}
2017-09-25 07:35:57 +02:00
}
2017-10-06 05:34:29 +02:00
2017-09-27 06:43:40 +02:00
private static func feeds(with feedDictionaries: [DiskFeedDictionary], accountID: String) -> Set<Feed> {
let feedArray = feedDictionaries.flatMap { Feed(accountID: accountID, dictionary: $0) }
return Set(feedArray)
2017-05-27 19:43:27 +02:00
}
}