NetNewsWire/Importers/DefaultFeedsImporter.swift

63 lines
1.5 KiB
Swift
Raw Normal View History

2017-05-27 10:43:27 -07: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 12:34:10 -07:00
import Account
import RSCore
2017-05-27 10:43:27 -07:00
2017-09-26 21:43:40 -07:00
typealias DiskFeedDictionary = [String: Any]
2017-09-24 22:35:57 -07:00
struct DefaultFeedsImporter {
2017-05-27 10:43:27 -07:00
2017-09-24 22:35:57 -07:00
static func importIfNeeded(_ firstRun: Bool, account: Account) {
if shouldImportDefaultFeeds(firstRun) {
FeedsImporter.importFeeds(defaultFeeds(), account: account)
2017-09-24 22:35:57 -07:00
}
2017-05-27 10:43:27 -07:00
}
2017-09-24 22:35:57 -07: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 13:15:07 -07:00
if !isFirstRun || AccountManager.shared.anyAccountHasAtLeastOneFeed() {
2017-05-27 10:43:27 -07:00
return false
}
2017-09-24 22:35:57 -07:00
return true
2017-05-27 10:43:27 -07:00
}
}
2017-09-24 22:35:57 -07:00
struct FeedsImporter {
2017-05-27 10:43:27 -07:00
2017-09-26 21:43:40 -07:00
static func importFeeds(_ feedDictionaries: [DiskFeedDictionary], account: Account) {
2017-09-24 22:35:57 -07:00
2017-09-26 21:43:40 -07:00
let feedsToImport = feeds(with: feedDictionaries, accountID: account.accountID)
2017-10-05 20:34:29 -07:00
2017-10-05 20:38:54 -07:00
BatchUpdate.shared.perform {
2017-10-05 20:34:29 -07:00
for feed in feedsToImport {
if !account.hasFeed(with: feed.feedID) {
2017-10-21 16:33:12 -07:00
account.addFeed(feed, to: nil)
2017-10-05 20:34:29 -07:00
}
2017-09-26 21:43:40 -07:00
}
}
account.dirty = true
2017-09-24 22:35:57 -07:00
}
2017-10-05 20:34:29 -07:00
2017-09-26 21:43:40 -07: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 10:43:27 -07:00
}
}