-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.swift
136 lines (115 loc) · 3.43 KB
/
Manager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// Manager.swift
// RSSReader
//
// Created by Mitchell Cooper on 10/9/14.
// Copyright (c) 2014 Mitchell Cooper. All rights reserved.
//
import Foundation
class Manager {
final var groups = [FeedGroup]()
let saveQueue = DispatchQueue(label: "save", attributes: [])
// MARK: Feed management
// all feeds
var feeds: [Feed] {
var f = [Feed]()
for group in groups {
f += group.feeds
}
return f
}
// all articles
var articles: [Article] {
var a = [Article]()
for group in groups {
a += group.articles
}
return a
}
// IDs of articles which have been deleted
final var deletedArticleIDs = [String]()
// all feeds to refresh automatically
fileprivate var refreshFeeds: [Feed] {
var f = [Feed]()
for group in groups {
if !group.automaticRefresh {
continue
}
f += group.feeds
}
return f
}
// fetch all the feeds asynchronously.
func fetchAllFeeds() {
fetchAllFeedsThen(nil)
}
// fetch all the feeds asynchronously, then do something.
var lastFetchTime = Date()
func fetchAllFeedsThen(_ then: ((Void) -> Void)?) {
lastFetchTime = Date()
for feed in refreshFeeds {
feed.fetchThen(then)
}
}
// find the default group
var defaultGroup: FeedGroup? {
for group in groups {
if group.isDefault { return group }
}
return nil
}
// list of groups, excluding default
var notDefaultGroups: [FeedGroup] {
var otherGroups = groups
otherGroups.remove(at: 0)
return otherGroups
}
// find a feed by its URL.
// consider: capitalization...
func feedFromURLString(_ urlString: String) -> Feed? {
for feed in feeds {
if urlString == feed.url.absoluteString! {
return feed
}
}
return nil
}
// MARK: Persistence
let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
lazy var plistPath: String = {
return self.documents.appendingPathComponent("manager.plist")
}()
// load from plist
func loadContext() {
if let storage = NSDictionary(contentsOfFile: plistPath) {
if let stored = storage["groups"] as? [NSDictionary] {
for info in stored {
let group = FeedGroup(storage: info)
groups.append(group)
}
}
if let ids = storage["deletedArticleIDs"] as? [String] {
deletedArticleIDs = ids
}
}
}
// save to plist
func saveContext() {
// save the feed manager
saveQueue.async {
rss.log("Saving changes")
let storage = self.forStorage()
storage.write(toFile: self.plistPath, atomically: true)
}
// save the user defaults
UserDefaults.standard.synchronize()
}
func forStorage() -> NSDictionary {
let x = [
"groups": groups.map { $0.forStorage() },
"deletedArticleIDs": deletedArticleIDs
] as [String : Any]
rss.log("manager.forStorage: \(x)")
return x as NSDictionary
}
}