Add chapters, history, bookmarks, live download progress, and i18n
- Chapter navigation with auto-scroll to current chapter and end-of-chapter sleep timer - Opt-in listening history (local-only) with XML export and per-item quick menu - Bookmarks with server sync via Audiobookshelf API - Live MB counter during downloads via URLSessionDownloadTask delegate - In-progress downloads shown in "Heruntergeladen" with dimmed cover + ring overlay - Cover image cache (50 MB memory / 500 MB disk URLCache) - German/English localization (de.lproj, en.lproj) - Loading spinner now triggers immediately on view switch instead of waiting for the network Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -66,11 +66,16 @@ struct DownloadedItem: Codable, Hashable {
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
final class DownloadManager {
|
||||
final class DownloadManager: @unchecked Sendable {
|
||||
private let client: ABSClient
|
||||
/// Keyed by downloadKey (itemId or "itemId|episodeId").
|
||||
private(set) var states: [String: DownloadState] = [:]
|
||||
private(set) var downloadedItems: [String: DownloadedItem] = [:]
|
||||
/// Items currently being downloaded (removed on completion or cancellation).
|
||||
private(set) var pendingItems: [String: LibraryItem] = [:]
|
||||
/// Bytes received for the currently in-flight track per downloadKey.
|
||||
/// Reset between tracks; cleared when the download finishes/cancels.
|
||||
private(set) var inFlightBytes: [String: Int64] = [:]
|
||||
|
||||
private var indexFile: URL { AppPaths.supportDirectory.appendingPathComponent("downloads-index.json") }
|
||||
private var activeTasks: [String: Task<Void, Never>] = [:]
|
||||
@@ -90,6 +95,36 @@ final class DownloadManager {
|
||||
return false
|
||||
}
|
||||
|
||||
func downloadedBytes(for downloadKey: String) -> Int64 {
|
||||
if let item = downloadedItems[downloadKey] {
|
||||
return item.tracks.reduce(Int64(0)) { sum, track in
|
||||
let url = AppPaths.downloadsDirectory.appendingPathComponent(track.localPath)
|
||||
return sum + fileSize(at: url)
|
||||
}
|
||||
}
|
||||
if let pending = pendingItems[downloadKey] {
|
||||
let onDisk = folderSize(at: AppPaths.downloadsDirectory.appendingPathComponent(pending.id))
|
||||
let inFlight = inFlightBytes[downloadKey] ?? 0
|
||||
return onDisk + inFlight
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
/// Called from the URL session delegate (any queue) to update in-flight bytes
|
||||
/// for a currently downloading track.
|
||||
nonisolated func _updateInFlightBytes(_ bytes: Int64, for key: String) {
|
||||
Task { @MainActor [self] in
|
||||
self.inFlightBytes[key] = bytes
|
||||
}
|
||||
}
|
||||
|
||||
private func fileSize(at url: URL) -> Int64 {
|
||||
guard let attrs = try? FileManager.default.attributesOfItem(atPath: url.path) else { return 0 }
|
||||
if let n = attrs[.size] as? NSNumber { return n.int64Value }
|
||||
if let i = attrs[.size] as? Int { return Int64(i) }
|
||||
return 0
|
||||
}
|
||||
|
||||
func localTrackURLs(for downloadKey: String) -> [URL]? {
|
||||
guard let item = downloadedItems[downloadKey] else { return nil }
|
||||
return item.tracks.map { AppPaths.downloadsDirectory.appendingPathComponent($0.localPath) }
|
||||
@@ -100,6 +135,7 @@ final class DownloadManager {
|
||||
let key = item.downloadKey
|
||||
guard activeTasks[key] == nil else { return }
|
||||
states[key] = .downloading(progress: 0)
|
||||
pendingItems[key] = item
|
||||
|
||||
let task = Task { @MainActor [weak self] in
|
||||
guard let self else { return }
|
||||
@@ -110,12 +146,14 @@ final class DownloadManager {
|
||||
workItem = try await self.client.fetchItemDetail(itemId: item.id)
|
||||
} catch {
|
||||
self.states[key] = .failed(message: "Detail konnte nicht geladen werden: \(error.localizedDescription)")
|
||||
self.pendingItems.removeValue(forKey: key)
|
||||
self.activeTasks[key] = nil
|
||||
return
|
||||
}
|
||||
}
|
||||
if workItem.audioFiles.isEmpty {
|
||||
self.states[key] = .failed(message: "Keine herunterladbaren Audiodateien gefunden.")
|
||||
self.pendingItems.removeValue(forKey: key)
|
||||
self.activeTasks[key] = nil
|
||||
return
|
||||
}
|
||||
@@ -129,9 +167,12 @@ final class DownloadManager {
|
||||
activeTasks[downloadKey]?.cancel()
|
||||
activeTasks[downloadKey] = nil
|
||||
states[downloadKey] = .notDownloaded
|
||||
pendingItems.removeValue(forKey: downloadKey)
|
||||
inFlightBytes.removeValue(forKey: downloadKey)
|
||||
}
|
||||
|
||||
func delete(downloadKey: String) {
|
||||
pendingItems.removeValue(forKey: downloadKey)
|
||||
cancel(downloadKey: downloadKey)
|
||||
if let item = downloadedItems[downloadKey] {
|
||||
let dir = directoryURL(itemId: item.itemId, episodeId: item.episodeId)
|
||||
@@ -148,6 +189,18 @@ final class DownloadManager {
|
||||
persistIndex()
|
||||
}
|
||||
|
||||
private func folderSize(at url: URL) -> Int64 {
|
||||
guard let enumerator = FileManager.default.enumerator(
|
||||
atPath: url.path
|
||||
) else { return 0 }
|
||||
var total: Int64 = 0
|
||||
for case let relPath as String in enumerator {
|
||||
let fileURL = url.appendingPathComponent(relPath)
|
||||
total += fileSize(at: fileURL)
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
private func directoryURL(itemId: String, episodeId: String?) -> URL {
|
||||
var dir = AppPaths.downloadsDirectory.appendingPathComponent(itemId, isDirectory: true)
|
||||
if let episodeId {
|
||||
@@ -162,6 +215,10 @@ final class DownloadManager {
|
||||
}
|
||||
|
||||
private func performDownload(workItem: LibraryItem, downloadKey: String) async {
|
||||
defer {
|
||||
pendingItems.removeValue(forKey: downloadKey)
|
||||
inFlightBytes.removeValue(forKey: downloadKey)
|
||||
}
|
||||
let itemDir = directoryURL(itemId: workItem.id, episodeId: workItem.episodeId)
|
||||
do {
|
||||
try FileManager.default.createDirectory(at: itemDir, withIntermediateDirectories: true)
|
||||
@@ -184,7 +241,7 @@ final class DownloadManager {
|
||||
|
||||
let tempURL: URL
|
||||
do {
|
||||
tempURL = try await downloadWithRetry(request: request, filename: file.filename)
|
||||
tempURL = try await downloadWithRetry(request: request, filename: file.filename, downloadKey: downloadKey)
|
||||
} catch is CancellationError {
|
||||
states[downloadKey] = .notDownloaded
|
||||
return
|
||||
@@ -210,6 +267,9 @@ final class DownloadManager {
|
||||
durationSeconds: file.durationSeconds
|
||||
))
|
||||
states[downloadKey] = .downloading(progress: Double(idx + 1) / Double(total))
|
||||
// Track is on disk now (folderSize picks it up). Clear the in-flight
|
||||
// counter so the next track's bytes don't double-count.
|
||||
inFlightBytes[downloadKey] = 0
|
||||
}
|
||||
|
||||
let downloaded = DownloadedItem(
|
||||
@@ -226,22 +286,22 @@ final class DownloadManager {
|
||||
}
|
||||
|
||||
/// Downloads with up to `maxAttempts` retries and resume-data support so a brief
|
||||
/// network dropout picks up where it left off. Uses the client session so that
|
||||
/// self-signed server certificates are accepted.
|
||||
private func downloadWithRetry(request: URLRequest, filename: String, maxAttempts: Int = 5) async throws -> URL {
|
||||
let session = client.session
|
||||
/// network dropout picks up where it left off. Uses a classic URLSessionDownloadTask
|
||||
/// with explicit delegate (wrapped in a continuation) so we reliably get
|
||||
/// `didWriteData` progress callbacks — the async `download(for:delegate:)` API
|
||||
/// often doesn't fire them.
|
||||
private func downloadWithRetry(request: URLRequest, filename: String, downloadKey: String, maxAttempts: Int = 5) async throws -> URL {
|
||||
var resumeData: Data? = nil
|
||||
var lastError: Error = URLError(.unknown)
|
||||
|
||||
for attempt in 0..<maxAttempts {
|
||||
try Task.checkCancellation()
|
||||
do {
|
||||
let (tempURL, response): (URL, URLResponse)
|
||||
if let resume = resumeData {
|
||||
(tempURL, response) = try await session.download(resumeFrom: resume)
|
||||
} else {
|
||||
(tempURL, response) = try await session.download(for: request)
|
||||
}
|
||||
let (tempURL, response) = try await streamingDownload(
|
||||
request: request,
|
||||
resumeData: resumeData,
|
||||
downloadKey: downloadKey
|
||||
)
|
||||
if let http = response as? HTTPURLResponse, !(200..<300).contains(http.statusCode) {
|
||||
try? FileManager.default.removeItem(at: tempURL)
|
||||
throw URLError(.badServerResponse)
|
||||
@@ -262,6 +322,32 @@ final class DownloadManager {
|
||||
throw lastError
|
||||
}
|
||||
|
||||
/// Runs a single download attempt via URLSessionDownloadTask, reporting byte
|
||||
/// progress to `inFlightBytes` and moving the system-temp file to a stable
|
||||
/// location before returning.
|
||||
private func streamingDownload(
|
||||
request: URLRequest,
|
||||
resumeData: Data?,
|
||||
downloadKey: String
|
||||
) async throws -> (URL, URLResponse) {
|
||||
let session = client.session
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
let delegate = DownloadProgressDelegate(
|
||||
manager: self,
|
||||
downloadKey: downloadKey,
|
||||
continuation: continuation
|
||||
)
|
||||
let task: URLSessionDownloadTask
|
||||
if let resumeData {
|
||||
task = session.downloadTask(withResumeData: resumeData)
|
||||
} else {
|
||||
task = session.downloadTask(with: request)
|
||||
}
|
||||
task.delegate = delegate
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
|
||||
private func loadIndex() {
|
||||
guard let data = try? Data(contentsOf: indexFile),
|
||||
let decoded = try? JSONDecoder().decode([String: DownloadedItem].self, from: data) else { return }
|
||||
@@ -292,3 +378,78 @@ final class DownloadManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Per-task delegate for a single `URLSessionDownloadTask`. Forwards live byte
|
||||
/// progress to the manager and bridges the delegate callbacks back to async/await
|
||||
/// via a checked continuation. The system deletes the `didFinishDownloadingTo`
|
||||
/// temp URL immediately after the callback returns, so we move it to a stable
|
||||
/// location before resuming.
|
||||
private final class DownloadProgressDelegate: NSObject, URLSessionDownloadDelegate, @unchecked Sendable {
|
||||
private let manager: DownloadManager
|
||||
private let downloadKey: String
|
||||
private let continuation: CheckedContinuation<(URL, URLResponse), Error>
|
||||
private var stableTempURL: URL?
|
||||
private var didResume = false
|
||||
private let lock = NSLock()
|
||||
|
||||
init(
|
||||
manager: DownloadManager,
|
||||
downloadKey: String,
|
||||
continuation: CheckedContinuation<(URL, URLResponse), Error>
|
||||
) {
|
||||
self.manager = manager
|
||||
self.downloadKey = downloadKey
|
||||
self.continuation = continuation
|
||||
super.init()
|
||||
}
|
||||
|
||||
private func resumeOnce(with result: Result<(URL, URLResponse), Error>) {
|
||||
lock.lock(); defer { lock.unlock() }
|
||||
guard !didResume else { return }
|
||||
didResume = true
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
|
||||
func urlSession(
|
||||
_ session: URLSession,
|
||||
downloadTask: URLSessionDownloadTask,
|
||||
didWriteData bytesWritten: Int64,
|
||||
totalBytesWritten: Int64,
|
||||
totalBytesExpectedToWrite: Int64
|
||||
) {
|
||||
manager._updateInFlightBytes(totalBytesWritten, for: downloadKey)
|
||||
}
|
||||
|
||||
func urlSession(
|
||||
_ session: URLSession,
|
||||
downloadTask: URLSessionDownloadTask,
|
||||
didFinishDownloadingTo location: URL
|
||||
) {
|
||||
// Move out of the system-temp folder before this delegate method returns
|
||||
// (otherwise the OS deletes it).
|
||||
let target = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString + ".tmp")
|
||||
do {
|
||||
try FileManager.default.moveItem(at: location, to: target)
|
||||
stableTempURL = target
|
||||
} catch {
|
||||
stableTempURL = nil
|
||||
}
|
||||
}
|
||||
|
||||
func urlSession(
|
||||
_ session: URLSession,
|
||||
task: URLSessionTask,
|
||||
didCompleteWithError error: Error?
|
||||
) {
|
||||
if let error {
|
||||
resumeOnce(with: .failure(error))
|
||||
return
|
||||
}
|
||||
guard let url = stableTempURL, let response = task.response else {
|
||||
resumeOnce(with: .failure(URLError(.cannotCreateFile)))
|
||||
return
|
||||
}
|
||||
resumeOnce(with: .success((url, response)))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user