Skip to content

Commit 19ef517

Browse files
committed
Track data usage in DownloadManager
Records bytes transferred during episode downloads, distinguishing between user-initiated and automatic downloads.
1 parent bfd53e0 commit 19ef517

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

podcasts/DownloadManager+URLSessionDelegate.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ extension DownloadManager: URLSessionDelegate, URLSessionDownloadDelegate {
210210
return
211211
}
212212

213+
// Track cellular data usage
214+
let bytesReceived = task.countOfBytesReceived
215+
let isCellular = metrics.transactionMetrics.last?.isCellular ?? false
216+
217+
if bytesReceived > 0 {
218+
let autoDownloadStatus = AutoDownloadStatus(rawValue: episode.autoDownloadStatus)
219+
let operationType: CellularDataUsageManager.OperationType
220+
switch autoDownloadStatus {
221+
case .playerDownloadedForStreaming:
222+
operationType = .stream
223+
default:
224+
operationType = .download
225+
}
226+
227+
let sessionType: CellularDataUsageManager.SessionType =
228+
session === wifiOnlyBackgroundSession ? .background :
229+
(session === cellularBackgroundSession ? .background : .foreground)
230+
231+
let bytesDownloaded = operationType != .stream ? bytesReceived : 0
232+
let bytesStreamed = operationType == .stream ? bytesReceived : 0
233+
let connectionType: CellularDataUsageManager.ConnectionType = isCellular ? .cellular : .wifi
234+
235+
dataManager.cellularDataUsageManager.add(
236+
episodeUuid: episode.uuid,
237+
podcastUuid: episode.parentIdentifier(),
238+
bytesDownloaded: bytesDownloaded,
239+
bytesStreamed: bytesStreamed,
240+
operationType: operationType,
241+
connectionType: connectionType,
242+
sessionType: sessionType
243+
)
244+
}
245+
213246
if let failure = taskFailure[episode.uuid] {
214247
logDownload(episode, failure: failure, metrics: metrics, session: session)
215248
taskFailure.removeValue(forKey: episode.uuid)

podcasts/DownloadManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ class DownloadManager: NSObject, FilePathProtocol {
399399
let exportPath = outputURL.pathComponents.joined(separator: "/")
400400
let exportStatus = ExportStatus()
401401
let originalSizeInBytes = episode.sizeInBytes
402-
let customLoaderDelegate = MediaExporterResourceLoaderDelegate(saveFilePath: exportPath) { [weak self, exportStatus] status, contentType, bytesDownloaded, bytesExpected in
402+
let customLoaderDelegate = MediaExporterResourceLoaderDelegate(saveFilePath: exportPath, episodeUuid: episode.uuid, podcastUuid: episode.parentIdentifier()) { [weak self, exportStatus] status, contentType, bytesDownloaded, bytesExpected in
403403
guard let self else {
404404
return
405405
}

0 commit comments

Comments
 (0)