forked from Ranchero-Software/NetNewsWire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultFeedsImporter.swift
63 lines (47 loc) · 1.53 KB
/
DefaultFeedsImporter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// DefaultFeedsImporter.swift
// NetNewsWire
//
// Created by Brent Simmons on 8/13/15.
// Copyright © 2015 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import Articles
import Account
import RSCore
typealias DiskFeedDictionary = [String: Any]
struct DefaultFeedsImporter {
static func importIfNeeded(_ firstRun: Bool, account: Account) {
if shouldImportDefaultFeeds(firstRun) {
appDelegate.logDebugMessage("Importing default feeds.")
FeedsImporter.importFeeds(defaultFeeds(), account: account)
}
}
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 {
if !isFirstRun || AccountManager.shared.anyAccountHasAtLeastOneFeed() {
return false
}
return true
}
}
struct FeedsImporter {
static func importFeeds(_ feedDictionaries: [DiskFeedDictionary], account: Account) {
let feedsToImport = feeds(with: feedDictionaries, accountID: account.accountID)
BatchUpdate.shared.perform {
for feed in feedsToImport {
if !account.hasFeed(with: feed.feedID) {
account.addFeed(feed, to: nil)
}
}
}
account.dirty = true
}
private static func feeds(with feedDictionaries: [DiskFeedDictionary], accountID: String) -> Set<Feed> {
let feedArray = feedDictionaries.compactMap { Feed(accountID: accountID, dictionary: $0) }
return Set(feedArray)
}
}