From b60f00bb6f74b588b524ece167a8e315f49d2176 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 23 Aug 2022 16:46:50 +0800 Subject: [PATCH] Handle canceling downloads, start on messaging for queued downloads --- .../Sources/Services/AudioSession/AudioSession.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift index afa1ef8ff..00030fb8a 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioSession.swift @@ -40,6 +40,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { var timer: Timer? var player: AVAudioPlayer? + var downloadTask: Task? public init(appEnvironment: AppEnvironment, networker: Networker) { self.appEnvironment = appEnvironment @@ -62,6 +63,7 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { state = .stopped timeElapsed = 0 duration = 1 + downloadTask?.cancel() } public var scrubState: PlayerScrubState = .reset { @@ -107,16 +109,17 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { let pageId = item!.unwrappedID - Task { + downloadTask = Task { do { _ = try await downloadAudioFile(pageId: pageId) + if Task.isCancelled { return } DispatchQueue.main.async { self.startDownloadedAudioFile(pageId: pageId) } } catch { - // TODO: maybe we need a failed state? + // TODO: display a failure toast here DispatchQueue.main.async { - self.state = .stopped + self.stop() } print("FAILED TO DOWNLOAD AUDIO URL") print(error) @@ -325,6 +328,9 @@ public class AudioSession: NSObject, ObservableObject, AVAudioPlayerDelegate { print("httpResponse: ", httpResponse) if let httpResponse = result?.1 as? HTTPURLResponse, httpResponse.statusCode == 202 { print("Tell the user the download has been queued") + DispatchQueue.main.async { + NSNotification.operationSuccess(message: "Your audio is being created.") + } } guard let data = result?.0 else {