-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedListVC.swift
380 lines (303 loc) · 13.6 KB
/
FeedListVC.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
// FeedListVC.swift
// RSSReader
//
// Created by Mitchell Cooper on 9/23/14.
// Copyright (c) 2014 Mitchell Cooper. All rights reserved.
//
import UIKit
class FeedListVC: UITableViewController, InfoCellDataSource {
var group: FeedGroup!
// standard section identifiers
var secInfo = 0
var secFeedList = 1
var secOptions = 2
// initialize with a group
convenience init(group: FeedGroup) {
self.init(style: .grouped)
self.group = group
}
// MARK:- View controller
override func viewDidLoad() {
tableView.register(UINib(nibName: "InfoCell", bundle: nil), forCellReuseIdentifier: "info")
navigationItem.title = group.title.capitalized
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target:nil, action:nil)
navigationItem.rightBarButtonItem = self.editButtonItem
//tableView.allowsMultipleSelectionDuringEditing = true
tableView.separatorColor = UIColor.clear
tableView.separatorStyle = .none
tableView.backgroundColor = Colors.tableColor
// watch for group title change
rss.center.addObserver(self, selector: "groupChanged", name: FeedGroup.Notifications.AppearanceChanged, object: group)
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
// MARK:- Table view data source
// info, feed list, and settings
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
// number of rows in each section
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch section {
case secInfo: return 1
case secFeedList: return group.feeds.count
case secOptions: return 2
default: return 0
}
}
// row height
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case secInfo: return 120
case secFeedList: return 80
case secOptions: return 55
default: return 0
}
}
// section header height
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
// 0 height not permitted
// this is a workaround to use a cell for a table header view
if section == secInfo { return 0.000001 }
return 20
}
// can't edit add, settings, or info cells
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return indexPath.section == secFeedList
}
// allow moving of feed cells but not fixed cells
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// can't move info or options
if indexPath.section == secInfo || indexPath.section == secOptions {
return false
}
return true
}
// returns the location to drop a cell when moving it
override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
// can never move between sections
if sourceIndexPath.section != proposedDestinationIndexPath.section {
return sourceIndexPath
}
// cannot move options
if proposedDestinationIndexPath.section == secOptions {
return sourceIndexPath
}
return proposedDestinationIndexPath
}
// perform a deletion
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
group.feeds.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
// swap two feeds' locations
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
rss.log("Swapping feed at \(sourceIndexPath.row) with \(destinationIndexPath.row)")
swap(&group.feeds[sourceIndexPath.row], &group.feeds[destinationIndexPath.row])
}
// return a cell for a row
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// top info cell.
if indexPath.section == secInfo {
let cell = tableView.dequeueReusableCell(withIdentifier: "info") as! InfoCell
cell.dataSource = self
cell.backgroundColor = Colors.cellColor
return cell
}
// options
if indexPath.section == secOptions {
if indexPath.row == 0 { return addButtonCellNamed("add feed") }
if indexPath.row == 1 { return settingsCellNamed("group settings") }
}
// feed cell
// first, try to dequeue a cell.
var cell: FeedCell
if let cellMaybe = tableView.dequeueReusableCell(withIdentifier: "feed") as? FeedCell {
cell = cellMaybe
}
// create a new cell.
else {
let items = Bundle.main.loadNibNamed("FeedCell", owner: self, options: nil)
cell = items?[0] as! FeedCell
//cell.accessoryType = .DisclosureIndicator
cell.backgroundColor = Colors.cellColor
}
let feed = group.feeds[indexPath.row]
cell.setFeed(feed)
// this is an attempt to smooth edges
cell.iconView.layer.shadowColor = UIColor.white.cgColor
cell.iconView.layer.shadowRadius = 1
cell.iconView.layer.shadowOffset = CGSize(width: 0, height: 0)
cell.iconView.layer.shadowOpacity = 0.8
cell.iconView.layer.shouldRasterize = true
cell.iconView.layer.rasterizationScale = 3
return cell
}
// the info section should never be selected; only individual buttons in it should
override func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return indexPath.section != secInfo
}
// user selected a feed
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// all articles.
if indexPath.section == secInfo {
pushArticleViewToCollection(group)
return
}
// add feed
if indexPath == IndexPath(row: 0, section: secOptions) {
presentFeedCreator(nil)
return
}
// group settings
if indexPath == IndexPath(row: 1, section: secOptions) {
pushGroupEditorForGroup(group)
return
}
// feed.
let feed = group.feeds[indexPath.row]
// no articles; fetch them
if feed.articles.isEmpty {
feed.fetch()
}
pushArticleViewToCollection(feed)
}
// returns a settings button cell with a title
func settingsCellNamed(_ title: String) -> UITableViewCell {
var cell: UITableViewCell
if let oldCell = tableView.dequeueReusableCell(withIdentifier: "settings") as? UITableViewCell {
cell = oldCell
}
else {
cell = UITableViewCell(style: .default, reuseIdentifier: "settings")
cell.imageView?.image = UIImage(named: "icons/gear")
cell.backgroundColor = Colors.cellColor
cell.textLabel?.textColor = UIColor.white
cell.selectedBackgroundView = Colors.cellSelectedBackgroundView
}
cell.textLabel?.text = title
return cell
}
// returns an add button cell with a title
func addButtonCellNamed(_ title: String) -> UITableViewCell {
// reuse cell
if let cell = tableView.dequeueReusableCell(withIdentifier: "add") as? UITableViewCell {
cell.textLabel?.text = title
return cell
}
// create base cell
let cell = UITableViewCell(style: .default, reuseIdentifier: "add")
cell.textLabel?.text = title
cell.imageView?.image = UIImage(named: "icons/plus")
cell.backgroundColor = Colors.cellColor
cell.textLabel?.textColor = UIColor.white
cell.selectedBackgroundView = Colors.cellSelectedBackgroundView
// add hairline border
let hairline = UIView()
hairline.setTranslatesAutoresizingMaskIntoConstraints(false)
hairline.backgroundColor = Colors.separatorColor
cell.contentView.addSubview(hairline)
// constraints
let views = [ "hl": hairline ]
let c1 = NSLayoutConstraint.constraintsWithVisualFormat("V:[hl(1)]-0-|", options: nil, metrics: nil, views: views)
let c2 = NSLayoutConstraint.constraintsWithVisualFormat("|-0-[hl]-0-|", options: nil, metrics: nil, views: views)
// iOS 8
// activate constraints
if NSLayoutConstraint.responds(to: #selector(NSLayoutConstraint.activate(_:))) {
NSLayoutConstraint.activateConstraints(c1 + c2)
}
// iOS 7
// add constraints to superview
else {
cell.addConstraints(c1 + c2)
}
return cell
}
// MARK:- Interface interaction
// push to the article list view for a feed.
func pushArticleViewToCollection(_ collection: ArticleCollection) {
let artVC = ArticleListVC(style: .plain)
artVC.collection = collection
navigationController?.pushViewController(artVC, animated: true)
}
// present the feed creator
fileprivate var _textField : UITextField?
func presentFeedCreator(_ sender: AnyObject?) {
tableView.setEditing(false, animated: true)
let alert = PSTAlertController(title: "Add feed", message: nil, preferredStyle: .alert)
// text field.
alert?.addTextField { textField in
self._textField = textField
textField?.placeholder = "URL"
textField?.keyboardAppearance = .dark
}
// OK button.
let action = PSTAlertAction(title: "OK", style: .default) { _ in
// empty string?
let string = self._textField!.text!
self._textField = nil
if string.isEmpty { return }
// does the feed exist already? reload it.
if let feed = rss.manager.feedFromURLString(string) {
let alert = PSTAlertController(title: "Already exists", message: "The feed you tried to add already exists in your collection.", preferredStyle: .alert)
alert?.addAction(PSTAlertAction(title: "OK", handler: nil))
alert?.showWithSender(nil, controller: self, animated: true, completion: nil)
feed.fetch()
self.tableView.reloadData()
return
}
// create and add the feed.
let newFeed: Feed! = Feed(group: self.group, storage: string)
// the url was not valid
if newFeed == nil {
let alert = PSTAlertController(title: "Feed error", message: "The supplied URL is not valid.", preferredStyle: .alert)
alert?.addAction(PSTAlertAction(title: "OK", handler: nil))
alert?.showWithSender(nil, controller: self, animated: true, completion: nil)
return
}
self.group.addFeed(newFeed)
// fetch feed, update the table, save to database.
newFeed.fetch()
self.tableView.reloadData()
return
}
alert?.addAction(action)
alert?.addAction(PSTAlertAction(title: "Cancel", style: .cancel, handler: nil))
// present it.
alert?.showWithSender(sender, controller: self, animated: true, completion: nil)
}
// push group editor
func pushGroupEditorForGroup(_ group: FeedGroup) {
let groupVC = GroupEditorVC(group: group)
navigationController?.pushViewController(groupVC, animated: true)
}
// push feed list for group
func pushFeedViewForGroup(_ group: FeedGroup) {
let feedVC = FeedListVC(group: group)
navigationController?.pushViewController(feedVC, animated: true)
}
// the current group's title has changed
func groupChanged() {
navigationItem.title = group.title.capitalized
}
// MARK:- Info cell data source
// all articles in group
func totalArticleCollectionForInfoCell(_ infoCell: InfoCell) -> ArticleCollection {
return GenericArticleCollection(title: "all \(group.title)", articles: group.articles)
}
// unread articles in group
func unreadArticleCollectionForInfoCell(_ infoCell: InfoCell) -> ArticleCollection {
let collection = totalArticleCollectionForInfoCell(infoCell)
return GenericArticleCollection(title: "unread \(group.title)", articles: collection.unread)
}
// saved articles in group
func savedArticleCollectionForInfoCell(_ infoCell: InfoCell) -> ArticleCollection {
let collection = totalArticleCollectionForInfoCell(infoCell)
return GenericArticleCollection(title: "saved \(group.title)", articles: collection.saved)
}
}