From 4258ed7de69c5ad84088cccfcba4471cc1a6ff76 Mon Sep 17 00:00:00 2001 From: PruthiviRaj27 Date: Tue, 9 Jan 2024 12:23:27 +0530 Subject: [PATCH] fix --- Source/Network/Models/Asset.swift | 7 +++++++ Source/Offline/Model/OfflineAsset.swift | 6 ++++++ Source/Offline/TPStreamsDownloadManager.swift | 20 ++++--------------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Source/Network/Models/Asset.swift b/Source/Network/Models/Asset.swift index 3ea6acf..7479a9c 100644 --- a/Source/Network/Models/Asset.swift +++ b/Source/Network/Models/Asset.swift @@ -17,3 +17,10 @@ struct Asset { let status: String } } + +extension Asset { + func isDownloaded() -> Bool { + let offlineAsset = OfflineAsset.manager.get(assetId: self.id) + return offlineAsset != nil + } +} diff --git a/Source/Offline/Model/OfflineAsset.swift b/Source/Offline/Model/OfflineAsset.swift index 4988a1b..5d495f4 100644 --- a/Source/Offline/Model/OfflineAsset.swift +++ b/Source/Offline/Model/OfflineAsset.swift @@ -29,6 +29,12 @@ extension OfflineAsset { } } } + + internal static func create(assetId: String, srcURL: String) -> OfflineAsset { + let offlineAsset.assetId = asset.id + offlineAsset.srcURL = asset.video.playbackURL + return offlineAsset + } } enum Status: String { diff --git a/Source/Offline/TPStreamsDownloadManager.swift b/Source/Offline/TPStreamsDownloadManager.swift index ca15204..0c431b3 100644 --- a/Source/Offline/TPStreamsDownloadManager.swift +++ b/Source/Offline/TPStreamsDownloadManager.swift @@ -31,34 +31,22 @@ public final class TPStreamsDownloadManager { internal func startDownload(asset: Asset, bitRate: Int) { - if isDownloaded(asset: asset){ - print("\(asset.id) Asset already downloaded or in progress") - return - } + if asset.isDownloaded() { return} - let avUrlAsset = AVURLAsset(url: URL(string: asset.video.playbackURL)!) + guard let avUrlAsset = AVURLAsset(url: URL(string: asset.video.playbackURL)) else { return } guard let task = assetDownloadURLSession.makeAssetDownloadTask( asset: avUrlAsset, assetTitle: asset.title, assetArtworkData: nil, options: [AVAssetDownloadTaskMinimumRequiredMediaBitrateKey: bitRate] - ) else { - return - } + ) else { return } - let offlineAsset = OfflineAsset() - offlineAsset.assetId = asset.id - offlineAsset.srcURL = asset.video.playbackURL + let offlineAsset = OfflineAsset.create(assetId: asset.id, srcURL: asset.video.playbackURL) OfflineAsset.manager.add(object: offlineAsset) offlineAssetDownloadManager.activeDownloadsMap[task] = offlineAsset task.resume() } - - private func isDownloaded(asset: Asset) -> Bool { - let offlineAsset = OfflineAsset.manager.get(assetId: asset.id) - return offlineAsset != nil - } }