Skip to content

Commit

Permalink
Add logic for download actions
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioEstevao committed Oct 24, 2024
1 parent cf658d5 commit 0961656
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ import Foundation
/// Whether this is a regular episode, or an user episode (File)
var isUserEpisode: Bool { get }
}

extension BaseEpisode {
public var isInDownloadProcess: Bool {
return downloading() || queued() || waitingForWifi()
}
}
15 changes: 12 additions & 3 deletions podcasts/Enumerations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ extension PlayerAction: AnalyticsDescribable {
case .transcript:
return L10n.transcript
case .download:
return L10n.download
guard let episode else {
return L10n.download
}
return episode.downloaded(pathFinder: DownloadManager.shared) ? L10n.removeDownload : (episode.isInDownloadProcess ? L10n.statusDownloading : L10n.download)
}
}

Expand Down Expand Up @@ -390,7 +393,10 @@ extension PlayerAction: AnalyticsDescribable {
case .transcript:
return "transcript"
case .download:
return "download"
guard let episode else {
return "episode-download"
}
return episode.downloaded(pathFinder: DownloadManager.shared) ? "episode-downloaded" : "episode-download"
}
}

Expand Down Expand Up @@ -419,7 +425,10 @@ extension PlayerAction: AnalyticsDescribable {
case .transcript:
return "transcript"
case .download:
return "download"
guard let episode else {
return "episode-download"
}
return episode.downloaded(pathFinder: DownloadManager.shared) ? "episode-downloaded" : "episode-download"
}
}

Expand Down
35 changes: 33 additions & 2 deletions podcasts/NowPlayingPlayerItemViewController+Shelf.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extension NowPlayingPlayerItemViewController: NowPlayingActionsDelegate {
let actions = Settings.playerActions()

// don't reload the actions unless we need to
if !lastShelfLoadState.updateRequired(shelfActions: actions, episodeUuid: playingEpisode.uuid, effectsOn: PlaybackManager.shared.effects().effectsEnabled(), sleepTimerOn: PlaybackManager.shared.sleepTimerActive(), episodeStarred: playingEpisode.keepEpisode) { return }
if !lastShelfLoadState.updateRequired(shelfActions: actions, episodeUuid: playingEpisode.uuid, effectsOn: PlaybackManager.shared.effects().effectsEnabled(), sleepTimerOn: PlaybackManager.shared.sleepTimerActive(), episodeStarred: playingEpisode.keepEpisode, episodeStatus: playingEpisode.episodeStatus) { return }

// load the first 4 actions into the player, followed by an overflow icon
playerControlsStackView.removeAllSubviews()
Expand Down Expand Up @@ -246,7 +246,38 @@ extension NowPlayingPlayerItemViewController: NowPlayingActionsDelegate {
}

func downloadTapped() {
return
guard let episode = PlaybackManager.shared.currentEpisode() as? Episode else { return }

AnalyticsEpisodeHelper.shared.currentSource = analyticsSource

if episode.downloaded(pathFinder: DownloadManager.shared) {
let confirmation = OptionsPicker(title: L10n.podcastDetailsRemoveDownload)
let yesAction = OptionAction(label: L10n.remove, icon: nil) {
self.deleteDownloadedFile()
Toast.show(L10n.playerEpisodeWasRemoved)
}
yesAction.destructive = true
confirmation.addAction(action: yesAction)

confirmation.show(statusBarStyle: preferredStatusBarStyle)
} else if episode.isInDownloadProcess {
PlaybackActionHelper.stopDownload(episodeUuid: episode.uuid)
Toast.show(L10n.playerEpisodeDownloadCancelled)
} else {
PlaybackActionHelper.download(episodeUuid: episode.uuid)
Toast.show(L10n.playerEpisodeQueuedForDownload)
}
}

private func deleteDownloadedFile() {
guard let episode = PlaybackManager.shared.currentEpisode() as? Episode else { return }

EpisodeManager.analyticsHelper.currentSource = analyticsSource

PlaybackManager.shared.removeIfPlayingOrQueued(episode: episode, fireNotification: true, userInitiated: false)
EpisodeManager.deleteDownloadedFiles(episode: episode, userInitated: true)

NotificationCenter.postOnMainThread(notification: Constants.Notifications.episodeDownloadStatusChanged, object: episode.uuid)
}

// MARK: - Player Actions
Expand Down
1 change: 1 addition & 0 deletions podcasts/NowPlayingPlayerItemViewController+Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension NowPlayingPlayerItemViewController {
addCustomObserver(Constants.Notifications.playerActionsUpdated, selector: #selector(reloadShelfActions))
addCustomObserver(UIApplication.willEnterForegroundNotification, selector: #selector(update))
addCustomObserver(Constants.Notifications.episodeStarredChanged, selector: #selector(reloadShelfActions))
addCustomObserver(Constants.Notifications.episodeDownloadStatusChanged, selector: #selector(reloadShelfActions))
}

@objc private func playbackTrackChanged() {
Expand Down
6 changes: 4 additions & 2 deletions podcasts/ShelfLoadState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ struct ShelfLoadState {
private var effectsAreOn = false
private var sleepTimerIsOn = false
private var episodeIsStarred = false
private var episodeStatus: Int32 = 0

mutating func updateRequired(shelfActions: [PlayerAction], episodeUuid: String, effectsOn: Bool, sleepTimerOn: Bool, episodeStarred: Bool) -> Bool {
if lastShelfActionsLoaded == shelfActions, lastShelfEpisodeUuid == episodeUuid, effectsAreOn == effectsOn, sleepTimerIsOn == sleepTimerOn, episodeIsStarred == episodeStarred {
mutating func updateRequired(shelfActions: [PlayerAction], episodeUuid: String, effectsOn: Bool, sleepTimerOn: Bool, episodeStarred: Bool, episodeStatus: Int32) -> Bool {
if lastShelfActionsLoaded == shelfActions, lastShelfEpisodeUuid == episodeUuid, effectsAreOn == effectsOn, sleepTimerIsOn == sleepTimerOn, episodeIsStarred == episodeStarred, episodeStatus == self.episodeStatus {
return false
}

Expand All @@ -18,6 +19,7 @@ struct ShelfLoadState {
effectsAreOn = effectsOn
sleepTimerIsOn = sleepTimerOn
episodeIsStarred = episodeStarred
self.episodeStatus = episodeStatus

return true
}
Expand Down
6 changes: 6 additions & 0 deletions podcasts/Strings+Generated.swift

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions podcasts/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -4508,4 +4508,13 @@
/* Auto Downloads Setting - Limits downloads to a number of show episodes. `%1$@' is a placeholder for the number of episodes*/
"auto_download_limit_number_of_episodes_show" = "%1$@ Latest Episodes per Show";

/* Toast message when episode download is removed*/
"player_episode_was_removed" = "Episode was removed";

/* Toast message when episode is queued for download*/
"player_episode_queued_for_download" = "Episode queued for download";

/* Toast message when episode download is cancelled*/
"player_episode_download_cancelled" = "Episode download cancelled";


0 comments on commit 0961656

Please sign in to comment.