DownloadManager class provides functionality to download audiobooks from both YouTube and direct URLs, with progress tracking, pause/resume capabilities, and persistent state management.
Overview
DownloadManager is a singleton service that handles:
- Downloading audiobook files from YouTube using
youtube_explode_dart - Direct URL downloads using
background_downloader - Progress tracking and status persistence in Hive
- Download cancellation and cleanup
- Permission handling for storage and notifications
lib/resources/services/download/download_manager.dart
Public Methods
checkAndRequestPermissions
Requests necessary permissions for downloading files. Returns:Future<bool> - True if permissions granted
Example:
downloadAudiobook
Downloads all files for an audiobook with progress tracking.Unique identifier for the audiobook
Title of the audiobook for notifications
List of file metadata with ‘title’, ‘url’ keys
Callback invoked with progress from 0.0 to 1.0
Callback invoked when download completes (true) or fails (false)
Future<void>
The method:
- Detects YouTube URLs and extracts audio streams
- Handles direct URL downloads with background downloader
- Updates progress in Hive storage (
download_status_box) - Automatically cleans up partial downloads on failure
- Supports notifications if permission granted
cancelDownload
Cancels an active download and removes partial files.ID of the audiobook to cancel
void
Example:
isDownloading
Checks if an audiobook is currently being downloaded.ID of the audiobook to check
bool
Example:
isDownloaded
Checks if an audiobook download has completed successfully.ID of the audiobook to check
bool
Example:
getProgress
Retrieves the current download progress for an audiobook.ID of the audiobook
double - Progress from 0.0 to 1.0
Example:
getError
Retrrieves any error message for a failed download.ID of the audiobook
String? - Error message or null if no error
Example:
isYouTubeDownload
Checks if the audiobook was downloaded from YouTube.ID of the audiobook
bool? - True if YouTube download, false if direct URL, null if not found
Example:
pauseDownload
Pauses a direct URL download (not supported for YouTube downloads).Task ID for the specific file being downloaded
Future<void>
Note: Only works with direct URL downloads, not YouTube streams.
resumeDownload
Resumes a paused direct URL download.Task ID for the specific file to resume
Future<void>
getTaskIdsForAudiobook
Retrieves all download task IDs for a specific audiobook.ID of the audiobook
List<String> - List of task IDs
Example:
Download Status Structure
The download status stored in Hive contains:YouTube Download Implementation
For YouTube URLs, the manager:- Parses video ID from URL
- Fetches audio manifest using
YoutubeExplodewith AndroidVR client - Selects highest quality MP4 audio stream
- Streams audio data in chunks to handle throttling
- Saves to external storage as MP3 files
Storage Location
Downloaded files are stored at:Error Handling
The manager handles:- Network failures during download
- Invalid YouTube URLs
- Insufficient storage space
- Permission denials
- Download cancellations
AppLogger and persisted in the download status.
Cleanup
Partial downloads are automatically cleaned up when:- Download is cancelled via
cancelDownload() - Download fails with an error
- User requests cancellation during active download