-
Notifications
You must be signed in to change notification settings - Fork 128
/
PodcastViewController.swift
958 lines (762 loc) · 37.7 KB
/
PodcastViewController.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
import Combine
import DifferenceKit
import PocketCastsDataModel
import PocketCastsServer
import PocketCastsUtils
import UIKit
import UIDeviceIdentifier
protocol PodcastActionsDelegate: AnyObject {
func isSummaryExpanded() -> Bool
func setSummaryExpanded(expanded: Bool)
func isDescriptionExpanded() -> Bool
func setDescriptionExpanded(expanded: Bool)
func tableView() -> UITableView
func displayedPodcast() -> Podcast?
func episodeCount() -> Int
func archivedEpisodeCount() -> Int
func manageSubscriptionTapped()
func settingsTapped()
func folderTapped()
func subscribe()
func unsubscribe()
func refreshArtwork(fromRect: CGRect, inView: UIView)
func searchEpisodes(query: String)
func clearSearch()
func toggleShowArchived()
func showingArchived() -> Bool
func archiveAllTapped(playedOnly: Bool)
func unarchiveAllTapped()
func downloadAllTapped()
func queueAllTapped()
func downloadableEpisodeCount(items: [ListItem]?) -> Int
func didActivateSearch()
func enableMultiSelect()
var podcastRatingViewModel: PodcastRatingViewModel { get }
func showBookmarks()
}
class PodcastViewController: FakeNavViewController, PodcastActionsDelegate, SyncSigninDelegate, MultiSelectActionDelegate {
var podcast: Podcast?
var episodeInfo = [ArraySection<String, ListItem>]()
var uuidsThatMatchSearch = [String]()
var featuredPodcast = false
var listUuid: String?
var summaryExpanded = false
var descriptionExpanded = false
var searchController: EpisodeListSearchController?
var cellHeights: [IndexPath: CGFloat] = [:]
var podcastRatingViewModel = PodcastRatingViewModel()
private var podcastInfo: PodcastInfo?
var loadingPodcastInfo = false
lazy var isoFormatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
return formatter
}()
@IBOutlet var episodesTableTopConstraint: NSLayoutConstraint!
@IBOutlet var episodesTable: UITableView! {
didSet {
registerCells()
registerLongPress()
episodesTable.rowHeight = UITableView.automaticDimension
episodesTable.estimatedRowHeight = 80.0
episodesTable.allowsMultipleSelectionDuringEditing = true
episodesTable.sectionHeaderTopPadding = 0
}
}
@IBOutlet var loadingIndicator: UIActivityIndicatorView!
@IBOutlet var loadingBgView: UIView! {
didSet {
loadingBgView.backgroundColor = AppTheme.defaultPodcastBackgroundColor()
}
}
@IBOutlet var loadingImageBg: UIView! {
didSet {
loadingImageBg.backgroundColor = ThemeColor.primaryUi05()
}
}
var isMultiSelectEnabled = false {
didSet {
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
self.episodesTable.beginUpdates()
self.episodesTable.setEditing(self.isMultiSelectEnabled, animated: true)
if self.episodesTable.numberOfSections > 0 {
self.episodesTable.reloadSections([0], with: .none)
}
self.episodesTable.endUpdates()
if self.isMultiSelectEnabled {
if self.selectedEpisodes.count == 0, self.longPressMultiSelectIndexPath == nil, !self.multiSelectGestureInProgress {
self.tableView().scrollToRow(at: IndexPath(row: NSNotFound, section: PodcastViewController.allEpisodesSection), at: .top, animated: true)
}
self.multiSelectFooter.setSelectedCount(count: self.selectedEpisodes.count)
if let selectedIndexPath = self.longPressMultiSelectIndexPath {
self.tableView().selectIndexPath(selectedIndexPath)
self.longPressMultiSelectIndexPath = nil
}
if let podcast = self.podcast {
let podcastBgColor = ColorManager.backgroundColorForPodcast(podcast)
self.multiSelectHeaderView.backgroundColor = ThemeColor.podcastUi05(podcastColor: podcastBgColor)
self.multiSelectCancelBtn.setTitleColor(ThemeColor.contrast01(), for: .normal)
self.multiSelectAllBtn.setTitleColor(ThemeColor.contrast01(), for: .normal)
self.updateSelectAllBtn()
self.multiSelectFooterBottomConstraint.constant = PlaybackManager.shared.currentEpisode() == nil ? 16 : Constants.Values.miniPlayerOffset + 16
self.multiSelectHeaderView.isHidden = false
self.view.bringSubviewToFront(self.multiSelectHeaderView)
// Adjusts multiSelectHeaderView based on screen width
self.setMultiSelectHeaderViewConstraint()
}
} else {
self.multiSelectHeaderView.isHidden = true
self.selectedEpisodes.removeAll()
}
self.searchController?.isOverflowButtonEnabled = !self.isMultiSelectEnabled
}
}
}
var multiSelectGestureInProgress = false
var longPressMultiSelectIndexPath: IndexPath?
@IBOutlet var multiSelectFooter: MultiSelectFooterView! {
didSet {
multiSelectFooter.delegate = self
}
}
@IBOutlet var multiSelectFooterBottomConstraint: NSLayoutConstraint!
var selectedEpisodes = [ListEpisode]() {
didSet {
multiSelectFooter.setSelectedCount(count: selectedEpisodes.count)
updateSelectAllBtn()
}
}
@IBOutlet var multiSelectCancelBtn: UIButton! {
didSet {
multiSelectCancelBtn.setTitle(L10n.cancel, for: .normal)
}
}
@IBOutlet var multiSelectAllBtn: UIButton!
@IBOutlet var multiSelectHeaderView: ThemeableView!
private let operationQueue = OperationQueue()
// Constraint to adjust multiSelectHeader based on device size
@IBOutlet weak var multiSelectHeaderViewConstraint: NSLayoutConstraint!
private func setMultiSelectHeaderViewConstraint() {
let screenWidth = UIScreen.main.bounds.width
var setConstant: Double
switch screenWidth {
/* iPod Touch (320) to iPhone SE 3rd gen (375) and
iPad Mini 4 (760) to iPad 6th Gen (1024) */
case 320...380, 760...1024:
setConstant = 65.0
/* Covers most modern devices (380+ width),
from iPhone 6 Plus (414) to iPhone 14 Pro Max (430) */
default:
setConstant = 90.0
}
self.multiSelectHeaderViewConstraint.constant = setConstant
}
static let headerSection = 0
static let allEpisodesSection = 1
private var isSearching = false
private var cancellables = Set<AnyCancellable>()
init(podcast: Podcast) {
self.podcast = podcast
// show the expaned view for unsubscribed podcasts, as well as paid podcasts that have expired and you no longer have access to play/download
summaryExpanded = !podcast.isSubscribed() || (podcast.isPaid && podcast.licensing == PodcastLicensing.deleteEpisodesAfterExpiry.rawValue && (SubscriptionHelper.subscriptionForPodcast(uuid: podcast.uuid)?.isExpired() ?? false))
AnalyticsHelper.podcastOpened(uuid: podcast.uuid)
podcastRatingViewModel.update(uuid: podcast.uuid)
super.init(nibName: "PodcastViewController", bundle: nil)
}
init(podcastInfo: PodcastInfo, existingImage: UIImage?) {
if let uuid = podcastInfo.uuid, let existingPodcast = DataManager.sharedManager.findPodcast(uuid: uuid, includeUnsubscribed: true) {
podcast = existingPodcast
summaryExpanded = !existingPodcast.isSubscribed()
} else {
self.podcastInfo = podcastInfo
summaryExpanded = true
}
if let uuid = podcastInfo.uuid {
podcastRatingViewModel.update(uuid: uuid)
AnalyticsHelper.podcastOpened(uuid: uuid)
}
super.init(nibName: "PodcastViewController", bundle: nil)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
deinit {
operationQueue.cancelAllOperations()
}
override func viewDidLoad() {
super.viewDidLoad()
closeTapped = { [weak self] in
_ = self?.navigationController?.popViewController(animated: true)
}
searchController = EpisodeListSearchController()
searchController?.podcastDelegate = self
operationQueue.maxConcurrentOperationCount = 1
scrollPointToChangeTitle = 38
addRightAction(image: UIImage(named: "podcast-share"), accessibilityLabel: L10n.share, action: #selector(shareTapped(_:)))
addGoogleCastBtn()
loadPodcastInfo()
updateColors()
NotificationCenter.default.addObserver(self, selector: #selector(podcastUpdated(_:)), name: Constants.Notifications.podcastUpdated, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(folderChanged(_:)), name: Constants.Notifications.folderChanged, object: nil)
listenForBookmarkChanges()
}
private func listenForBookmarkChanges() {
let bookmarkManager = PlaybackManager.shared.bookmarkManager
// Refresh when a bookmark is added to our podcast
bookmarkManager.onBookmarkCreated
.filter({ [weak self] event in
event.podcast == self?.podcast?.uuid
})
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] _ in
self?.upNextChanged()
})
.store(in: &cancellables)
// Reload when a bookmark is deleted
bookmarkManager.onBookmarksDeleted
.filter({ [weak self] event in
event.items.contains(where: { $0.podcast == self?.podcast?.uuid })
})
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] _ in
self?.upNextChanged()
})
.store(in: &cancellables)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Load the ratings even if we've already started loading them to cover all other potential view states
// The view model will ignore extra calls
if let uuid = [podcast?.uuid, podcastInfo?.uuid].compactMap({ $0 }).first {
podcastRatingViewModel.update(uuid: uuid)
}
updateColors()
}
private var hasAppearedAlready = false
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addCustomObserver(Constants.Notifications.podcastColorsDownloaded, selector: #selector(colorsDidDownload(_:)))
updateColors()
addCustomObserver(Constants.Notifications.episodeArchiveStatusChanged, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.manyEpisodesChanged, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.episodeStarredChanged, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.playbackTrackChanged, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.playbackStarted, selector: #selector(hideSearchKeyboard))
addCustomObserver(Constants.Notifications.playbackEnded, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.playbackFailed, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.upNextEpisodeRemoved, selector: #selector(upNextChanged))
addCustomObserver(Constants.Notifications.upNextEpisodeAdded, selector: #selector(upNextChanged))
addCustomObserver(Constants.Notifications.upNextQueueChanged, selector: #selector(upNextChanged))
addCustomObserver(Constants.Notifications.searchRequested, selector: #selector(searchRequested))
// Episode grouping can change based on download and play status, so listen for both those events and refresh when they happen
addCustomObserver(Constants.Notifications.episodeDownloadStatusChanged, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.episodeDownloaded, selector: #selector(refreshEpisodes))
addCustomObserver(Constants.Notifications.episodePlayStatusChanged, selector: #selector(refreshEpisodes))
if featuredPodcast, !hasAppearedAlready {
Analytics.track(.discoverFeaturedPodcastTapped, properties: ["uuid": podcastUUID])
AnalyticsHelper.openedFeaturedPodcast()
}
// if it's a local podcast, refresh it when the view appears, eg: when you tab back to it
if let podcast = podcast, podcast.isSubscribed(), hasAppearedAlready {
refreshEpisodes()
}
hasAppearedAlready = true // we use this so the page doesn't double load from viewDidLoad and viewDidAppear
Analytics.track(.podcastScreenShown, properties: ["uuid": podcastUUID])
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
removeAllCustomObservers()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
guard let window = view.window else { return }
let multiSelectFooterOffset: CGFloat = isMultiSelectEnabled ? 80 : 0
episodesTable.contentInset = UIEdgeInsets(top: navBarHeight(window: window), left: 0, bottom: Constants.Values.miniPlayerOffset + multiSelectFooterOffset, right: 0)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
UIStatusBarStyle.lightContent
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
searchController?.hideKeyboard()
}
@objc private func searchRequested() {
guard podcast != nil, let searchBar = searchController?.searchTextField else { return }
searchBar.becomeFirstResponder()
}
@objc private func colorsDidDownload(_ notification: Notification) {
guard let uuidLoaded = notification.object as? String else { return }
if let uuid = podcast?.uuid, uuid == uuidLoaded {
if let podcast = DataManager.sharedManager.findPodcast(uuid: uuid, includeUnsubscribed: true) {
self.podcast = podcast
}
updateColors()
}
}
private func updateColors() {
episodesTable.reloadData()
if let podcast = podcast {
let podcastBgColor = ColorManager.backgroundColorForPodcast(podcast)
updateNavColors(bgColor: ThemeColor.podcastUi03(podcastColor: podcastBgColor), titleColor: UIColor.white, buttonColor: ThemeColor.contrast01())
multiSelectHeaderView.backgroundColor = ThemeColor.podcastUi05(podcastColor: podcastBgColor)
multiSelectCancelBtn.setTitleColor(ThemeColor.contrast01(), for: .normal)
multiSelectAllBtn.setTitleColor(ThemeColor.contrast01(), for: .normal)
} else {
updateNavColors(bgColor: AppTheme.defaultPodcastBackgroundColor(), titleColor: UIColor.white, buttonColor: ThemeColor.contrast01())
}
}
override func handleThemeChanged() {
updateColors()
}
@objc private func podcastUpdated(_ notification: Notification) {
guard let podcastUuid = notification.object as? String, podcastUuid == podcast?.uuid else { return }
podcast = DataManager.sharedManager.findPodcast(uuid: podcastUuid, includeUnsubscribed: true)
if viewIfLoaded?.window != nil {
refreshEpisodes()
}
}
@objc private func folderChanged(_ notification: Notification) {
guard let podcastUuid = podcast?.uuid else { return }
podcast = DataManager.sharedManager.findPodcast(uuid: podcastUuid, includeUnsubscribed: true)
if viewIfLoaded?.window != nil {
refreshEpisodes()
}
}
@objc private func refreshEpisodes() {
guard let podcast = podcast else { return }
if episodesTable.numberOfSections > 0 {
episodesTable.reloadSections([0], with: .none)
}
loadLocalEpisodes(podcast: podcast, animated: true)
}
@objc private func upNextChanged() {
episodesTable.reloadData()
}
@objc private func shareTapped(_ sender: UIButton) {
guard let podcast = podcast else { return }
let sourceRect = sender.superview!.convert(sender.frame, to: view)
SharingHelper.shared.shareLinkTo(podcast: podcast, fromController: self, sourceRect: sourceRect, sourceView: view)
Analytics.track(.podcastScreenShareTapped)
}
private func loadPodcastInfo() {
if let podcast = podcast {
if podcast.isSubscribed() {
loadLocalEpisodes(podcast: podcast, animated: false)
checkIfPodcastNeedsUpdating()
} else {
let podcastUuid = podcast.uuid
PodcastManager.shared.deletePodcastIfUnused(podcast)
if let _ = DataManager.sharedManager.findPodcast(uuid: podcastUuid, includeUnsubscribed: true) {
// podcast wasn't deleted, but needs to be updated
loadLocalEpisodes(podcast: podcast, animated: false)
checkIfPodcastNeedsUpdating()
} else {
// podcast was deleted, reload the entire thing
self.podcast = nil
loadPodcastInfoFromUuid(podcastUuid)
}
}
} else if let uuid = podcastInfo?.uuid {
loadPodcastInfoFromUuid(uuid)
} else if let iTunesId = podcastInfo?.iTunesId {
loadPodcastInfoFromiTunesId(iTunesId)
}
}
func loadLocalEpisodes(podcast: Podcast, animated: Bool) {
let uuidsToFilter = (searchController?.searchInProgress() ?? false) ? uuidsThatMatchSearch : nil
let refreshOperation = PodcastEpisodesRefreshOperation(podcast: podcast, uuidsToFilter: uuidsToFilter) { [weak self] newData in
guard let self = self else { return }
self.navTitle = podcast.title
// add the episode limit placehold if it's needed
var finalData = newData
var needsNoEpisodesMessage = false
var needsNoSearchResultsMessage = false
let searching = self.searchController?.searchTextField?.text?.count ?? 0 > 0
if podcast.podcastGrouping() == .none {
let episodeLimit = Int(podcast.autoArchiveEpisodeLimit)
var episodes = newData[safe: 1]?.elements
let episodeCount = episodes?.count ?? 0
if episodeCount > 0, episodeLimit > 0, podcast.overrideGlobalArchive {
var indexToInsertAt = -1
if PodcastEpisodeSortOrder.newestToOldest.rawValue == Int(podcast.episodeSortOrder) {
indexToInsertAt = episodeLimit <= episodeCount ? episodeLimit : episodeCount
} else if PodcastEpisodeSortOrder.oldestToNewest.rawValue == Int(podcast.episodeSortOrder) {
indexToInsertAt = episodeCount > episodeLimit ? episodeCount - episodeLimit : episodeCount - 1
}
if indexToInsertAt >= 0 {
let message = episodeLimit == 1 ? L10n.podcastLimitSingular : L10n.podcastLimitPluralFormat(episodeLimit.localized())
let placeholder = EpisodeLimitPlaceholder(limit: episodeLimit, message: message)
episodes?.insert(placeholder, at: indexToInsertAt)
finalData[1] = ArraySection(model: "episodes", elements: episodes!)
}
} else if episodeCount == 0, searching {
needsNoSearchResultsMessage = true
} else if episodeCount == 0, !self.showingArchived() {
needsNoEpisodesMessage = true
}
} else {
var totalEpisodeCount = -1 // the search header counts as an item below, so start from -1
for group in finalData {
totalEpisodeCount += group.elements.count
}
needsNoEpisodesMessage = totalEpisodeCount == 0 && !self.showingArchived() && !searching
needsNoSearchResultsMessage = totalEpisodeCount == 0 && searching
}
if needsNoSearchResultsMessage {
let placeholder = NoSearchResultsPlaceholder()
finalData[1] = ArraySection(model: "episodes", elements: [placeholder])
} else if needsNoEpisodesMessage {
let archivedCount = self.archivedEpisodeCount()
let message = L10n.podcastArchivedMsg(archivedCount.localized())
let placeholder = AllArchivedPlaceholder(archived: archivedCount, message: message)
finalData[1] = ArraySection(model: "episodes", elements: [placeholder])
}
if animated {
let oldData = self.episodeInfo
let changeSet = StagedChangeset(source: oldData, target: finalData)
do {
try SJCommonUtils.catchException {
self.episodesTable.reload(using: changeSet, with: .none, setData: { data in
self.episodeInfo = data
})
}
} catch {
self.episodeInfo = finalData
self.episodesTable.reloadData()
}
} else {
self.episodeInfo = finalData
self.episodesTable.reloadData()
}
self.searchController?.episodesDidReload()
if self.isMultiSelectEnabled {
self.updateSelectAllBtn()
}
}
operationQueue.addOperation(refreshOperation)
}
@objc func hideSearchKeyboard() {
searchController?.hideKeyboard()
}
// MARK: - PodcastActionsDelegate
func refreshArtwork(fromRect: CGRect, inView: UIView) {
guard let podcast = podcast else { return }
let optionsPicker = OptionsPicker(title: nil)
let refreshAction = OptionAction(label: L10n.podcastRefreshArtwork, icon: nil) {
ImageManager.sharedManager.clearCache(podcastUuid: podcast.uuid, recacheWhenDone: true)
}
optionsPicker.addAction(action: refreshAction)
optionsPicker.show(statusBarStyle: preferredStatusBarStyle)
}
func unsubscribe() {
var downloadedCount = 0
for object in episodeInfo[1].elements {
guard let listEpisode = object as? ListEpisode else { continue }
if listEpisode.episode.episodeStatus == DownloadStatus.downloaded.rawValue {
downloadedCount += 1
}
}
let optionPicker = OptionsPicker(title: downloadedCount > 0 ? nil : L10n.areYouSure)
let unsubscribeAction = OptionAction(label: L10n.unsubscribe, icon: nil, action: { [weak self] in
self?.performUnsubscribe()
})
if downloadedCount > 0 {
unsubscribeAction.destructive = true
optionPicker.addDescriptiveActions(title: L10n.downloadedFilesConf(downloadedCount), message: L10n.downloadedFilesConfMessage, icon: "option-alert", actions: [unsubscribeAction])
} else {
optionPicker.addAction(action: unsubscribeAction)
}
optionPicker.show(statusBarStyle: preferredStatusBarStyle)
Analytics.track(.podcastScreenUnsubscribeTapped)
}
private func performUnsubscribe() {
guard let podcast = podcast else { return }
PodcastManager.shared.unsubscribe(podcast: podcast)
navigationController?.popViewController(animated: true)
Analytics.track(.podcastUnsubscribed, properties: ["source": analyticsSource, "uuid": podcast.uuid])
}
func subscribe() {
guard let podcast = podcast else { return }
podcast.subscribed = 1
podcast.syncStatus = SyncStatus.notSynced.rawValue
DataManager.sharedManager.save(podcast: podcast)
ServerPodcastManager.shared.updateLatestEpisodeInfo(podcast: podcast, setDefaults: true)
loadLocalEpisodes(podcast: podcast, animated: true)
if featuredPodcast {
Analytics.track(.discoverFeaturedPodcastSubscribed, properties: ["podcast_uuid": podcast.uuid])
AnalyticsHelper.subscribedToFeaturedPodcast()
}
if let listId = listUuid {
AnalyticsHelper.podcastSubscribedFromList(listId: listId, podcastUuid: podcast.uuid)
}
HapticsHelper.triggerSubscribedHaptic()
Analytics.track(.podcastScreenSubscribeTapped)
Analytics.track(.podcastSubscribed, properties: ["source": analyticsSource, "uuid": podcast.uuid])
}
func isSummaryExpanded() -> Bool {
summaryExpanded
}
func setSummaryExpanded(expanded: Bool) {
summaryExpanded = expanded
}
func isDescriptionExpanded() -> Bool {
descriptionExpanded
}
func setDescriptionExpanded(expanded: Bool) {
descriptionExpanded = expanded
}
func tableView() -> UITableView {
episodesTable
}
func displayedPodcast() -> Podcast? {
podcast
}
func episodeCount() -> Int {
guard let podcast = podcast else { return 0 }
return DataManager.sharedManager.count(query: "SELECT COUNT(*) FROM \(DataManager.episodeTableName) WHERE podcast_id == ?", values: [podcast.id])
}
func archivedEpisodeCount() -> Int {
guard let podcast = podcast else { return 0 }
return DataManager.sharedManager.count(query: "SELECT COUNT(*) FROM \(DataManager.episodeTableName) WHERE podcast_id == ? AND archived = 1", values: [podcast.id])
}
func settingsTapped() {
guard let podcast = podcast else { return }
let settingsController = PodcastSettingsViewController(podcast: podcast)
settingsController.episodes = episodeInfo
navigationController?.pushViewController(settingsController, animated: true)
Analytics.track(.podcastScreenSettingsTapped)
}
func manageSubscriptionTapped() {
guard SyncManager.isUserLoggedIn() else {
let signinPage = SyncSigninViewController()
signinPage.delegate = self
navigationController?.pushViewController(signinPage, animated: true)
return
}
guard let podcast = podcast, let bundle = SubscriptionHelper.bundleSubscriptionForPodcast(podcastUuid: podcast.uuid) else { return }
let subscriptionController = SupporterPodcastViewController(bundleSubscription: bundle)
navigationController?.pushViewController(subscriptionController, animated: true)
}
func didActivateSearch() {
// Add padding to the bottom of the table to allow it to scroll up
let tableBounds = tableView().bounds
let view = UIView()
view.frame = CGRect(x: 0, y: 0, width: tableBounds.width, height: tableBounds.height - 320)
view.backgroundColor = UIColor.clear
tableView().tableFooterView = view
// scroll the search box to the top of the page
tableView().scrollToRow(at: IndexPath(row: NSNotFound, section: PodcastViewController.allEpisodesSection), at: .top, animated: true)
}
func folderTapped() {
Analytics.track(.podcastScreenFolderTapped)
if !SubscriptionHelper.hasActiveSubscription() {
NavigationManager.sharedManager.showUpsellView(from: self, source: .folders)
return
}
guard let podcast = podcast else { return }
if let currentFolder = podcast.folderUuid, !currentFolder.isEmpty {
// podcast is already in a folder, present the options for removing/moving it
showPodcastFolderMoveOptions(currentFolderUuid: currentFolder)
return
}
showFolderPickerDialog()
}
func searchEpisodes(query: String) {
performEpisodeSearch(query: query)
if !isSearching {
isSearching = true
Analytics.track(.podcastScreenSearchPerformed)
}
}
func clearSearch() {
guard let podcast = podcast else { return }
uuidsThatMatchSearch.removeAll()
loadLocalEpisodes(podcast: podcast, animated: true)
isSearching = false
Analytics.track(.podcastScreenSearchCleared)
}
func toggleShowArchived() {
guard let podcast = podcast else { return }
podcast.showArchived = !podcast.showArchived
DataManager.sharedManager.save(podcast: podcast)
loadLocalEpisodes(podcast: podcast, animated: true)
Analytics.track(.podcastScreenToggleArchived, properties: ["show_archived": podcast.showArchived])
}
func showingArchived() -> Bool {
podcast?.showArchived ?? false
}
func archiveAllTapped(playedOnly: Bool) {
archiveAll(playedOnly: playedOnly)
}
func unarchiveAllTapped() {
guard let podcast = podcast else { return }
DispatchQueue.global().async {
DataManager.sharedManager.markAllUnarchivedForPodcast(id: podcast.id)
AnalyticsEpisodeHelper.shared.currentSource = .podcastScreen
AnalyticsEpisodeHelper.shared.bulkUnarchiveEpisodes(count: self.episodeCount())
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else { return }
strongSelf.loadLocalEpisodes(podcast: podcast, animated: false)
}
}
}
func archiveAll(playedOnly: Bool = false) {
guard let podcast = podcast else { return }
DispatchQueue.global().async { [weak self] in
guard let allObjects = self?.episodeInfo[safe: 1]?.elements, allObjects.count > 0 else { return }
var count = 0
for object in allObjects {
guard let listEpisode = object as? ListEpisode else { continue }
if listEpisode.episode.archived || (playedOnly && !listEpisode.episode.played()) { continue }
EpisodeManager.archiveEpisode(episode: listEpisode.episode, fireNotification: false, userInitiated: false)
count += 1
}
AnalyticsEpisodeHelper.shared.currentSource = .podcastScreen
AnalyticsEpisodeHelper.shared.bulkArchiveEpisodes(count: count)
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else { return }
strongSelf.loadLocalEpisodes(podcast: podcast, animated: false)
}
}
}
func downloadAllTapped() {
DispatchQueue.global().async { [weak self] in
guard let self = self, let allObjects = self.episodeInfo[safe: 1]?.elements, allObjects.count > 0 else { return }
let episodes = allObjects.compactMap { ($0 as? ListEpisode)?.episode }
AnalyticsEpisodeHelper.shared.currentSource = .podcastScreen
AnalyticsEpisodeHelper.shared.bulkDownloadEpisodes(episodes: episodes)
self.downloadItems(allObjects: allObjects)
}
}
func downloadItems(allObjects: [ListItem]) {
var queuedEpisodes = 0
for object in allObjects {
guard let listEpisode = object as? ListEpisode else { continue }
if listEpisode.episode.downloading() || listEpisode.episode.downloaded(pathFinder: DownloadManager.shared) || listEpisode.episode.queued() {
continue
}
DownloadManager.shared.addToQueue(episodeUuid: listEpisode.episode.uuid, fireNotification: true, autoDownloadStatus: .notSpecified)
queuedEpisodes += 1
if queuedEpisodes == Constants.Limits.maxBulkDownloads {
return
}
}
}
func queueAllTapped() {
DispatchQueue.global().async { [weak self] in
guard let self = self, let allObjects = self.episodeInfo[safe: 1]?.elements, allObjects.count > 0 else { return }
self.queueItems(allObjects: allObjects)
}
}
func queueItems(allObjects: [ListItem]) {
var queuedEpisodes = 0
for object in allObjects {
guard let listEpisode = object as? ListEpisode else { continue }
if listEpisode.episode.downloading() || listEpisode.episode.downloaded(pathFinder: DownloadManager.shared) || listEpisode.episode.queued() {
continue
}
DownloadManager.shared.queueForLaterDownload(episodeUuid: listEpisode.episode.uuid, fireNotification: true, autoDownloadStatus: .notSpecified)
queuedEpisodes += 1
if queuedEpisodes == Constants.Limits.maxBulkDownloads {
return
}
}
}
func downloadableEpisodeCount(items: [ListItem]? = nil) -> Int {
guard let allObjects = items == nil ? episodeInfo[safe: 1]?.elements : items, allObjects.count > 0 else { return 0 }
var count = 0
for object in allObjects {
guard let listEpisode = object as? ListEpisode else { continue }
if !listEpisode.episode.downloaded(pathFinder: DownloadManager.shared), !listEpisode.episode.downloading(), !listEpisode.episode.queued() {
count += 1
}
}
return count
}
func enableMultiSelect() {
isMultiSelectEnabled = true
}
private func showPodcastFolderMoveOptions(currentFolderUuid: String) {
guard let podcast = podcast, let folder = DataManager.sharedManager.findFolder(uuid: currentFolderUuid) else { return }
let optionsPicker = OptionsPicker(title: folder.name.localizedUppercase)
let removeAction = OptionAction(label: L10n.folderRemoveFrom.localizedCapitalized, icon: "folder-remove") {
podcast.sortOrder = ServerPodcastManager.shared.highestSortOrderForHomeGrid() + 1
podcast.folderUuid = nil
podcast.syncStatus = SyncStatus.notSynced.rawValue
DataManager.sharedManager.save(podcast: podcast)
DataManager.sharedManager.updateFolderSyncModified(folderUuid: currentFolderUuid, syncModified: TimeFormatter.currentUTCTimeInMillis())
NotificationCenter.postOnMainThread(notification: Constants.Notifications.folderChanged, object: currentFolderUuid)
Analytics.track(.folderPodcastModalOptionTapped, properties: ["option": "remove"])
}
optionsPicker.addAction(action: removeAction)
let changeFolderAction = OptionAction(label: L10n.folderChange.localizedCapitalized, icon: "folder-arrow") { [weak self] in
guard let self = self else { return }
self.showFolderPickerDialog()
Analytics.track(.folderPodcastModalOptionTapped, properties: ["option": "change"])
}
optionsPicker.addAction(action: changeFolderAction)
let goToFolderAction = OptionAction(label: L10n.folderGoTo.localizedCapitalized, icon: "folder-goto") {
NavigationManager.sharedManager.navigateTo(NavigationManager.folderPageKey, data: [NavigationManager.folderKey: folder])
Analytics.track(.folderPodcastModalOptionTapped, properties: ["option": "go_to"])
}
optionsPicker.addAction(action: goToFolderAction)
optionsPicker.show(statusBarStyle: preferredStatusBarStyle)
}
private func showFolderPickerDialog() {
guard let podcast = podcast else { return }
let model = ChoosePodcastFolderModel(pickingFor: podcast.uuid, currentFolder: podcast.folderUuid)
let chooseFolderView = ChoosePodcastFolderView(model: model) { [weak self] _ in
self?.dismiss(animated: true, completion: nil)
}
let hostingController = PCHostingController(rootView: chooseFolderView.environmentObject(Theme.sharedTheme))
present(hostingController, animated: true, completion: nil)
}
func showBookmarks() {
guard let podcast else { return }
Analytics.track(.podcastsScreenTabTapped, properties: ["value": "bookmarks"])
let controller = BookmarksPodcastListController(podcast: podcast)
present(controller, animated: true)
}
// MARK: - Long press actions
func archiveAll(startingAt: Episode) {
guard let podcast = podcast else { return }
DispatchQueue.global().async { [weak self] in
guard let allObjects = self?.episodeInfo[safe: 1]?.elements, allObjects.count > 0 else { return }
var haveFoundFirst = false
for object in allObjects {
guard let listEpisode = object as? ListEpisode else { continue }
if !haveFoundFirst, listEpisode.episode.uuid != startingAt.uuid { continue }
haveFoundFirst = true
if listEpisode.episode.archived { continue }
EpisodeManager.archiveEpisode(episode: listEpisode.episode, fireNotification: false)
}
DispatchQueue.main.async { [weak self] in
guard let strongSelf = self else { return }
strongSelf.loadLocalEpisodes(podcast: podcast, animated: false)
}
}
}
// MARK: - Accessibility fix
// Not quite sure why this view controller won't close with the z-gesture
// I suspect it has something to do with the way it is pushed in MainTabController
// Implementing the following function restores expected functionality
override func accessibilityPerformEscape() -> Bool {
navigationController?.popViewController(animated: true)
return true
}
// MARK: - SyncSigninDelegate
func signingProcessCompleted() {
navigationController?.popToViewController(self, animated: true)
}
}
// MARK: - Analytics
extension PodcastViewController: AnalyticsSourceProvider {
var analyticsSource: AnalyticsSource {
.podcastScreen
}
}
private extension PodcastViewController {
var podcastUUID: String {
podcast?.uuid ?? podcastInfo?.analyticsDescription ?? "unknown"
}
}