-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPodcastsTableViewController.swift
413 lines (329 loc) · 17.4 KB
/
PodcastsTableViewController.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//
// PodcastsTableViewController.swift
//
//
// Created by Mitchell Downey on 6/2/15.
//
//
import UIKit
import CoreData
import Lock
class PodcastsTableViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var bottomButton: UIButton!
var managedObjectContext:NSManagedObjectContext!
var subscribedPodcastsArray = [Podcast]()
var followedPodcastsArray = [Podcast]()
let coreDataHelper = CoreDataHelper.sharedInstance
var playlistManager = PlaylistManager.sharedInstance
let parsingPodcasts = ParsingPodcastsList.shared
let reachability = PVReachability.manager
var refreshControl: UIRefreshControl!
@IBOutlet weak var parsingActivity: UIActivityIndicatorView!
@IBOutlet weak var parsingActivityLabel: UILabel!
@IBOutlet weak var parsingActivityBar: UIProgressView!
@IBOutlet weak var parsingActivityContainer: UIView!
private let REFRESH_PODCAST_TIME:Double = 3600
override func viewDidLoad() {
super.viewDidLoad()
var isFirstTimeAppOpened: Bool = false
if NSUserDefaults.standardUserDefaults().objectForKey("ONE_TIME_LOGIN") == nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let loginVC = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as? LoginViewController {
loginVC.delegate = self
self.presentViewController(loginVC, animated: false, completion: nil)
}
NSUserDefaults.standardUserDefaults().setObject(NSUUID().UUIDString, forKey: "ONE_TIME_LOGIN")
isFirstTimeAppOpened = true
}
navigationItem.title = "Podcasts"
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
tabBarController?.tabBar.translucent = false
bottomButton.hidden = true
showFindAPodcastIfNoneAreFollowed()
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh all podcasts")
refreshControl.addTarget(self, action: #selector(refreshData), forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(removePlayerNavButtonAndReload), name: Constants.kPlayerHasNoItem, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(reloadPodcastData), name: Constants.kDownloadHasFinished, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(clearParsingActivity), name: Constants.kInternetIsUnreachable, object: nil)
updateParsingActivity()
if isFirstTimeAppOpened != true {
reloadPodcastData()
refreshPodcastFeeds()
}
startCheckSubscriptionsForNewEpisodesTimer()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationItem.rightBarButtonItem = self.playerNavButton()
showFindAPodcastIfNoneAreFollowed()
self.tableView.reloadData()
}
func refreshData() {
if reachability.hasInternetConnection() == false && refreshControl.refreshing == true {
showInternetNeededAlert("Connect to WiFi or cellular data to parse podcast feeds.")
refreshControl.endRefreshing()
return
}
refreshPodcastFeeds()
}
private func refreshPodcastFeeds() {
let moc = self.coreDataHelper.managedObjectContext
let podcastsPredicate = NSPredicate(format: "isSubscribed == %@", NSNumber(bool: true))
let podcastArray = CoreDataHelper.fetchEntities("Podcast", predicate: podcastsPredicate, moc:moc) as! [Podcast]
for podcast in podcastArray {
parsingPodcasts.urls.append(podcast.feedURL)
let feedURL = NSURL(string:podcast.feedURL)
dispatch_async(Constants.feedParsingQueue) {
let feedParser = PVFeedParser(onlyGetMostRecentEpisode: true, shouldSubscribe:false, shouldFollow: false, shouldParseChannelOnly: false)
feedParser.delegate = self
if let feedURLString = feedURL?.absoluteString {
feedParser.parsePodcastFeed(feedURLString)
dispatch_async(dispatch_get_main_queue(), {
self.updateParsingActivity()
})
}
}
}
showFindAPodcastIfNoneAreFollowed()
refreshControl.endRefreshing()
}
private func showFindAPodcastIfNoneAreFollowed() {
if subscribedPodcastsArray.count == 0 && followedPodcastsArray.count == 0 {
bottomButton.setTitle("Find a podcast", forState: .Normal)
bottomButton.hidden = false
} else {
bottomButton.hidden = true
}
}
// MARK: - Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Show Episodes" {
let episodesTableViewController = segue.destinationViewController as! EpisodesTableViewController
if let index = tableView.indexPathForSelectedRow {
// if there are no downloaded episodes, then showAllEpisodes
let downloadedEpisodesArrayPredicate = NSPredicate(format: "fileName != nil || taskIdentifier != nil", argumentArray: nil)
var downloadedEpisodesArray: NSSet!
if index.section == 0 {
episodesTableViewController.selectedPodcastId = subscribedPodcastsArray[index.row].objectID
downloadedEpisodesArray = subscribedPodcastsArray[index.row].episodes.filteredSetUsingPredicate(downloadedEpisodesArrayPredicate)
} else {
episodesTableViewController.selectedPodcastId = followedPodcastsArray[index.row].objectID
downloadedEpisodesArray = followedPodcastsArray[index.row].episodes.filteredSetUsingPredicate(downloadedEpisodesArrayPredicate)
}
if downloadedEpisodesArray.count > 0 {
episodesTableViewController.showAllEpisodes = false
} else {
episodesTableViewController.showAllEpisodes = true
}
}
} else if segue.identifier == Constants.TO_PLAYER_SEGUE_ID {
let mediaPlayerViewController = segue.destinationViewController as! MediaPlayerViewController
mediaPlayerViewController.hidesBottomBarWhenPushed = true
}
}
func removePlayerNavButtonAndReload() {
self.removePlayerNavButton()
self.reloadPodcastData()
}
@IBAction func bottomButtonAction(sender: AnyObject) {
tabBarController?.selectedIndex = 2
}
// This function runs once on app load, then runs in the background every 30 minutes.
// Check if a new episode is available for a subscribed podcast; if true, download that episode.
// TODO: shouldn't we check via push notifications? Rather than a timer that continuously runs in the background?
func startCheckSubscriptionsForNewEpisodesTimer() {
NSTimer.scheduledTimerWithTimeInterval(REFRESH_PODCAST_TIME, target: self, selector: #selector(refreshData), userInfo: nil, repeats: true)
}
func reloadPodcastData() {
let subscribedPredicate = NSPredicate(format: "isSubscribed == %@", true)
self.managedObjectContext = coreDataHelper.managedObjectContext
self.subscribedPodcastsArray = CoreDataHelper.fetchEntities("Podcast", predicate: subscribedPredicate, moc:managedObjectContext) as! [Podcast]
self.subscribedPodcastsArray.sortInPlace{ $0.title.removeArticles() < $1.title.removeArticles() }
for podcast in self.subscribedPodcastsArray {
let podcastPredicate = NSPredicate(format: "podcast == %@", podcast)
let mostRecentEpisodeArray = CoreDataHelper.fetchOnlyEntityWithMostRecentPubDate("Episode", predicate: podcastPredicate, moc:managedObjectContext) as! [Episode]
if let mostRecentEpisodePubDate = mostRecentEpisodeArray.first?.pubDate {
podcast.lastPubDate = mostRecentEpisodePubDate
}
}
let notSubscribedPredicate = NSPredicate(format: "isSubscribed == %@", false)
let followedPredicate = NSPredicate(format: "isFollowed == %@", true)
let compoundPredicate = NSCompoundPredicate.init(andPredicateWithSubpredicates: [notSubscribedPredicate, followedPredicate])
self.followedPodcastsArray = CoreDataHelper.fetchEntities("Podcast", predicate: compoundPredicate, moc:managedObjectContext) as! [Podcast]
self.followedPodcastsArray.sortInPlace{ $0.title.removeArticles() < $1.title.removeArticles() }
for podcast in self.followedPodcastsArray {
let podcastPredicate = NSPredicate(format: "podcast == %@", podcast)
let mostRecentEpisodeArray = CoreDataHelper.fetchOnlyEntityWithMostRecentPubDate("Episode", predicate: podcastPredicate, moc:managedObjectContext) as! [Episode]
if let mostRecentEpisodePubDate = mostRecentEpisodeArray.first?.pubDate {
podcast.lastPubDate = mostRecentEpisodePubDate
}
}
self.tableView.reloadData()
}
func clearParsingActivity() {
parsingPodcasts.itemsParsing = 0
self.parsingActivityContainer.hidden = true
}
func updateParsingActivity() {
self.parsingActivityLabel.text = "\(parsingPodcasts.itemsParsing) of \(parsingPodcasts.urls.count) parsed"
self.parsingActivityBar.progress = Float(parsingPodcasts.itemsParsing)/Float(parsingPodcasts.urls.count)
if parsingPodcasts.itemsParsing >= parsingPodcasts.urls.count {
self.parsingActivityContainer.hidden = true
self.parsingActivity.stopAnimating()
}
else {
self.parsingActivityContainer.hidden = false
self.parsingActivity.startAnimating()
}
}
}
extension PodcastsTableViewController: UITableViewDelegate, UITableViewDataSource {
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if section == 0 {
return "Subscribed"
} else {
return "Following"
}
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 100
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return subscribedPodcastsArray.count
} else {
return followedPodcastsArray.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! PodcastsTableCell
let podcast: Podcast!
if indexPath.section == 0 {
podcast = subscribedPodcastsArray[indexPath.row]
} else {
podcast = followedPodcastsArray[indexPath.row]
}
cell.title?.text = podcast.title
let episodes = podcast.episodes.allObjects as! [Episode]
let episodesDownloaded = episodes.filter{ $0.fileName != nil }
cell.episodesDownloadedOrStarted?.text = "\(episodesDownloaded.count) downloaded"
cell.totalClips?.text = "\(podcast.totalClips) clips"
cell.lastPublishedDate?.text = ""
if let lastPubDate = podcast.lastPubDate {
cell.lastPublishedDate?.text = PVUtility.formatDateToString(lastPubDate)
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
var cellImage:UIImage?
if let imageData = podcast.imageThumbData, image = UIImage(data: imageData) {
cellImage = image
}
else {
cellImage = UIImage(named: "PodverseIcon")
}
dispatch_async(dispatch_get_main_queue(), {
if let visibleRows = self.tableView.indexPathsForVisibleRows where visibleRows.contains(indexPath) {
let existingCell = self.tableView.cellForRowAtIndexPath(indexPath) as! PodcastsTableCell
existingCell.pvImage.image = cellImage
}
})
})
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("Show Episodes", sender: nil)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// Override to support conditional editing of the table view.
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// Return NO if you do not want the specified item to be editable.
return true
}
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
let podcastToEdit: Podcast!
if indexPath.section == 0 {
podcastToEdit = subscribedPodcastsArray[indexPath.row]
} else {
podcastToEdit = followedPodcastsArray[indexPath.row]
}
var subscribeOrFollow = "Subscribe"
if podcastToEdit.isSubscribed == true {
subscribeOrFollow = "Follow"
}
let subscribeOrFollowAction = UITableViewRowAction(style: .Default, title: subscribeOrFollow, handler: {action, indexpath in
if subscribeOrFollow == "Subscribe" {
PVSubscriber.subscribeToPodcast(podcastToEdit.feedURL, podcastTableDelegate: self)
} else {
PVFollower.followPodcast(podcastToEdit.feedURL, podcastTableDelegate: self)
}
})
subscribeOrFollowAction.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0);
let deleteAction = UITableViewRowAction(style: .Default, title: "Delete", handler: {action, indexpath in
// Remove Player button if the now playing episode was one of the podcast's episodes
let allPodcastEpisodes = podcastToEdit.episodes.allObjects as! [Episode]
if let nowPlayingEpisode = PVMediaPlayer.sharedInstance.nowPlayingEpisode {
if allPodcastEpisodes.contains(nowPlayingEpisode) {
self.navigationItem.rightBarButtonItem = nil
}
}
if indexPath.section == 0 {
self.subscribedPodcastsArray.removeAtIndex(indexPath.row)
} else {
self.followedPodcastsArray.removeAtIndex(indexPath.row)
}
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
PVFollower.unfollowPodcast(podcastToEdit.objectID, completionBlock: nil)
self.showFindAPodcastIfNoneAreFollowed()
})
return [deleteAction, subscribeOrFollowAction]
}
}
extension PodcastsTableViewController: PVFeedParserDelegate {
func feedParsingComplete(feedURL:String?) {
if let url = feedURL, let index = self.subscribedPodcastsArray.indexOf({ url == $0.feedURL }) {
let podcast = CoreDataHelper.fetchEntityWithID(self.subscribedPodcastsArray[index].objectID, moc: self.managedObjectContext) as! Podcast
self.subscribedPodcastsArray[index] = podcast
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 0)], withRowAnimation: .None)
} else if let url = feedURL, let index = self.followedPodcastsArray.indexOf({ url == $0.feedURL }) {
let podcast = CoreDataHelper.fetchEntityWithID(self.followedPodcastsArray[index].objectID, moc: self.managedObjectContext) as! Podcast
self.followedPodcastsArray[index] = podcast
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: index, inSection: 1)], withRowAnimation: .None)
}
else {
self.reloadPodcastData()
}
updateParsingActivity()
}
func feedParsingStarted() {
updateParsingActivity()
}
func feedParserChannelParsed() {
self.reloadPodcastData()
}
}
extension PodcastsTableViewController:LoginModalDelegate {
// TODO: what happens if a user logs into a different account through the app?
func loginTapped() {
PVAuth.sharedInstance.showAuth0LockLoginVC(self)
}
}
extension String {
func removeArticles() -> String {
var words = self.componentsSeparatedByString(" ")
//Only one word so count it as sortable
if(words.count <= 1) {
return self
}
if( words[0].lowercaseString == "a" || words[0].lowercaseString == "the" || words[0].lowercaseString == "an" ) {
words.removeFirst()
return words.joinWithSeparator(" ")
}
return self
}
}