@@ -8,12 +8,15 @@ A lightweight Kotlin Multiplatform download manager for Android and iOS. Uses pl
88
99## Features
1010
11- - Simple, unified API across platforms
11+ - Kotlin DSL for easy configuration
1212- Progress tracking with callbacks
13- - Error handling with detailed information
14- - Cancel downloads by ID
15- - MIME type detection
16- - Platform-native implementations (Android ` DownloadManager ` , iOS ` NSURLSession ` )
13+ - State management (pending, downloading, paused, completed, failed, cancelled)
14+ - Authentication support (Bearer token, Basic auth)
15+ - Custom headers
16+ - Network type restrictions (WiFi only)
17+ - Platform-native implementations:
18+ - Android: ` DownloadManager ` (system-managed, survives app kill, shows notifications)
19+ - iOS: ` NSURLSession ` (background download support)
1720
1821## Installation
1922
@@ -37,42 +40,118 @@ dependencyResolutionManagement {
3740kotlin {
3841 sourceSets {
3942 commonMain.dependencies {
40- implementation(" dev.onexeor.kdownloader:shared:0.1 .0" )
43+ implementation(" dev.onexeor.kdownloader:shared:0.2 .0" )
4144 }
4245 }
4346}
4447```
4548
49+ ### Android Setup
50+
51+ Initialize KDownloader in your Application class:
52+
53+ ``` kotlin
54+ class MyApp : Application () {
55+ override fun onCreate () {
56+ super .onCreate()
57+ KDownloader .init (this )
58+ }
59+ }
60+ ```
61+
4662## Usage
4763
4864### Basic Download
4965
5066``` kotlin
51- val downloader = KDownloader (context) // Android requires Context
52-
53- val downloadId = downloader.downloadFile(
54- url = " https://example.com/file.pdf" ,
55- fileName = " document.pdf" ,
56- progressListener = { uri, status ->
57- println (" Download progress: $uri , status: $status " )
58- },
59- errorListener = { error ->
60- println (" Error: ${error.description} (${error.statusCode} )" )
67+ val downloader = KDownloader ()
68+
69+ downloader.download(" https://example.com/file.pdf" ) {
70+ fileName = " document.pdf"
71+
72+ onProgress { progress ->
73+ println (" ${progress.percentage} %" )
6174 }
62- )
75+
76+ onComplete { filePath ->
77+ println (" Downloaded to: $filePath " )
78+ }
79+
80+ onError { error ->
81+ println (" Failed: ${error.message} " )
82+ }
83+ }
6384```
6485
65- ### Cancel Download
86+ ### With Authentication
6687
6788``` kotlin
68- downloader.cancelDownloadById(downloadId)
89+ downloader.download(" https://api.example.com/private/file.zip" ) {
90+ fileName = " data.zip"
91+
92+ auth {
93+ bearer(" your-access-token" )
94+ }
95+
96+ onComplete { println (" Done: $it " ) }
97+ }
6998```
7099
71- ### Get Download Info
100+ ### With Custom Headers
72101
73102``` kotlin
74- val mimeType = downloader.getMimeTypeById(downloadId)
75- val url = downloader.getUrlById(downloadId)
103+ downloader.download(" https://example.com/file.pdf" ) {
104+ headers {
105+ " X-Custom-Header" to " value"
106+ " Accept" to " application/octet-stream"
107+ }
108+ }
109+ ```
110+
111+ ### WiFi Only Download
112+
113+ ``` kotlin
114+ downloader.download(" https://example.com/large-file.zip" ) {
115+ wifiOnly()
116+
117+ onStateChange { state ->
118+ when (state) {
119+ is DownloadState .Paused -> println (" Waiting: ${state.reason} " )
120+ is DownloadState .Downloading -> println (" Progress: ${state.progress.percentage} %" )
121+ is DownloadState .Completed -> println (" Done!" )
122+ else -> {}
123+ }
124+ }
125+ }
126+ ```
127+
128+ ### Task Control
129+
130+ ``` kotlin
131+ val task = downloader.download(" https://example.com/file.zip" )
132+
133+ // Add listeners after creation (fluent API)
134+ task.onProgress { println (" ${it.percentage} %" ) }
135+ .onComplete { println (" Done: $it " ) }
136+ .onError { println (" Error: ${it.message} " ) }
137+
138+ // Check current state
139+ println (" State: ${task.currentState} " )
140+ println (" Progress: ${task.currentProgress.percentage} %" )
141+
142+ // Cancel if needed
143+ task.cancel()
144+ ```
145+
146+ ### Configuration
147+
148+ ``` kotlin
149+ val downloader = KDownloader (
150+ KDownloaderConfig (
151+ defaultDirectory = " MyApp/Downloads" ,
152+ defaultNetworkType = NetworkType .ANY
153+ )
154+ )
76155```
77156
78157## API Reference
@@ -81,35 +160,57 @@ val url = downloader.getUrlById(downloadId)
81160
82161| Method | Description |
83162| --------| -------------|
84- | ` downloadFile(url, fileName?, progressListener?, errorListener?) ` | Start a download, returns download ID |
85- | ` cancelDownloadById(downloadId) ` | Cancel an active download |
86- | ` getMimeTypeById(downloadId) ` | Get MIME type of downloaded file |
87- | ` getUrlById(downloadId) ` | Get original URL of download |
88-
89- ### DownloadError
163+ | ` download(url, builder) ` | Start a download with DSL configuration |
164+ | ` download(request) ` | Start a download with pre-built request |
165+ | ` getTask(id) ` | Get an existing download task by ID |
166+ | ` cancelAll() ` | Cancel all active downloads |
167+
168+ ### DownloadState
169+
170+ | State | Description |
171+ | -------| -------------|
172+ | ` Pending ` | Download is queued |
173+ | ` Downloading(progress) ` | Download is in progress |
174+ | ` Paused(reason) ` | Download is paused |
175+ | ` Completed(filePath) ` | Download completed successfully |
176+ | ` Failed(error) ` | Download failed with an error |
177+ | ` Cancelled ` | Download was cancelled |
178+
179+ ### DownloadError Types
180+
181+ | Type | Description |
182+ | ------| -------------|
183+ | ` Network ` | Connection failed, timeout, etc. |
184+ | ` Http ` | HTTP error response (4xx, 5xx) |
185+ | ` Storage ` | Disk full, permission denied, etc. |
186+ | ` InvalidUrl ` | Malformed URL |
187+ | ` Cancelled ` | User cancelled the download |
188+ | ` Unknown ` | Unexpected error |
189+
190+ ### Auth
90191
91192``` kotlin
92- data class DownloadError (
93- val url : String , // Original download URL
94- val status : Int , // Download manager status code
95- val description : String ,// Human-readable error description
96- val statusCode : Int // HTTP status code
97- )
193+ auth {
194+ bearer(" token" ) // Bearer token authentication
195+ basic(" user" , " password" ) // Basic authentication
196+ }
98197```
99198
100199## Platform Requirements
101200
102201| Platform | Minimum Version |
103202| ----------| -----------------|
104203| Android | API 24 (7.0) |
105- | iOS | Supported via KMP |
204+ | iOS | 13.0 |
106205
107206## Roadmap
108207
109- - [ ] Authentication support (Basic, Token )
110- - [ ] Cookie handling
111- - [ ] Custom headers
208+ - [x ] Authentication support (Bearer, Basic )
209+ - [x] Custom headers
210+ - [x] Kotlin DSL API
112211- [ ] Download queue management
212+ - [ ] Resume/pause support
213+ - [ ] Download speed tracking
113214
114215## License
115216
0 commit comments