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
|
2017-09-17 21:22:15 +02:00
|
|
|
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) {
|
2017-09-25 22:31:36 +02:00
|
|
|
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) {
|
|
|
|
let _ = account.addFeed(feed, to: nil)
|
|
|
|
}
|
2017-09-27 06:43:40 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-08 05:28:39 +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> {
|
2017-10-05 22:09:09 +02:00
|
|
|
|
|
|
|
let feedArray = feedDictionaries.flatMap { Feed(accountID: accountID, dictionary: $0) }
|
|
|
|
return Set(feedArray)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|