NetNewsWire/Importers/DefaultFeedsImporter.swift

64 lines
1.5 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 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
2017-09-27 06:43:40 +02:00
let feedsToImport = feeds(with: feedDictionaries, accountID: account.accountID)
2017-10-06 05:34:29 +02:00
2017-10-06 05:38:54 +02:00
BatchUpdate.shared.perform {
2017-10-06 05:34:29 +02:00
for feed in feedsToImport {
if !account.hasFeed(with: feed.feedID) {
2017-10-22 01:33:12 +02:00
account.addFeed(feed, to: nil)
2017-10-06 05:34:29 +02:00
}
2017-09-27 06:43:40 +02:00
}
}
account.dirty = true
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.compactMap { Feed(accountID: accountID, dictionary: $0) }
return Set(feedArray)
2017-05-27 19:43:27 +02:00
}
}