Skip to content

Commit b30be39

Browse files
committed
Improved download button state management
1 parent daaffe2 commit b30be39

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

WWDC/SessionActionsViewController.swift

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class SessionActionsViewController: NSViewController {
207207
/// This publisher includes a flag indicating whether the session can be downloaded, as well as the current download state, if any.
208208
let downloadButtonConfig: AnyPublisher<DownloadButtonConfig, Never> = Publishers.CombineLatest(inFlightDownloadSignal, alreadyDownloaded)
209209
.map { inFlightDownload, session in
210-
if session.isDownloaded {
210+
if session.isDownloaded, MediaDownloadManager.shared.hasDownloadedMedia(for: session) {
211211
return DownloadButtonConfig(hasDownloadableContent: true, isDownloaded: true)
212212
} else {
213213
guard !session.assets(matching: Session.mediaDownloadVariants).isEmpty else {
@@ -253,13 +253,36 @@ class SessionActionsViewController: NSViewController {
253253
updateDownloadButton(with: nil)
254254
return
255255
}
256-
256+
257257
downloadStateCancellable = inFlightDownload.$state.receive(on: DispatchQueue.main).sink { [weak self] state in
258258
self?.updateDownloadButton(with: state)
259259
}
260260
}
261261

262262
private func updateDownloadButton(with state: MediaDownloadState?) {
263+
guard let session = viewModel?.session else { return }
264+
265+
func applyStartDownloadState() {
266+
resetDownloadButton()
267+
downloadIndicator.isHidden = true
268+
downloadButton.isHidden = false
269+
clipButton.isHidden = true
270+
if case .failed(let message) = state {
271+
downloadButton.toolTip = message
272+
} else {
273+
downloadButton.toolTip = nil
274+
}
275+
}
276+
277+
/// We may have a download that's in completed state, but where the file has already been deleted,
278+
/// in which case we show the button state to start the download.
279+
if case .completed = state {
280+
guard MediaDownloadManager.shared.hasDownloadedMedia(for: session) else {
281+
applyStartDownloadState()
282+
return
283+
}
284+
}
285+
263286
switch state {
264287
case .waiting:
265288
downloadIndicator.isHidden = false
@@ -282,15 +305,7 @@ class SessionActionsViewController: NSViewController {
282305
downloadIndicator.progress = Float(progress)
283306
}
284307
case .paused, .cancelled, .none, .failed:
285-
resetDownloadButton()
286-
downloadIndicator.isHidden = true
287-
downloadButton.isHidden = false
288-
clipButton.isHidden = true
289-
if case .failed(let message) = state {
290-
downloadButton.toolTip = message
291-
} else {
292-
downloadButton.toolTip = nil
293-
}
308+
applyStartDownloadState()
294309
case .completed:
295310
downloadButton.toolTip = "Delete downloaded video"
296311
downloadButton.isHidden = false

0 commit comments

Comments
 (0)