OkDownload Documentation

repository·master·Indexed 26 days ago

https://github.com/lingochamp/okdownload

A reliable, flexible, and high-performance download engine for Android and successor to FileDownloader2. It supports simple and advanced use cases, including managing download queues, parallel task configuration, and custom component injection. The library provides Kotlin extensions for concise DownloadListener creation, coroutine support via .await(), and progress tracking through Kotlin Channels.

Tokens
1.3K
Snippets
6
Records
13
Agent score
90%

What's inside OkDownload

  1. Use OkDownload for advanced configurations

    master

    Advanced usage scenarios include:

    • Setting the maximum number of parallel running tasks.
    • Setting the delay (in milliseconds) for submitting tasks to the database.
    • Injecting custom components.

    Detailed instructions are available in the Advanced Use Guideline in the project wiki.

  2. Start a DownloadTask using Kotlin extension functions

    master

    The library provides Kotlin extension methods on DownloadTask to start tasks and handle listeners in a single call. This eliminates the need to manually create listener objects.

    lateinit var task: DownloadTask
    // start task with a listener via extension function
    task.enqueue { task, cause, realCause ->
    }
  3. Use OkDownload

    master

    OkDownload provides different levels of usage depending on your requirements:

    Simple Use Cases

    Use the simple guidelines for:

    • Starting and canceling downloads
    • Managing download queues
    • Retrieving task state or task information

    Advanced Use Cases

    Use the advanced guidelines for:

    • Setting the maximum number of parallel running tasks
    • Setting database delay milliseconds
    • Injecting custom components
  4. Show download progress using Kotlin Channels

    master

    You can observe download progress via a Kotlin Channel. After starting the task (e.g., via enqueue3), call task.spChannel() to get the channel and iterate over it within a coroutine.

    lateinit var task: DownloadTask
    // 1. Start the task
    task.enqueue3 { task, cause, realCause ->
    }
    
    // 2. Access the channel (must be after starting task)
    val pb = task.spChannel()
    lifecycleScope.launch {
        for (progress in pb) {
            // show progress
        }
    }
  5. Create a DownloadListener3

    master

    Use createListener3 for a highly concise listener where the terminal block is of type () -> Unit. This block is invoked whenever warn, error, completed, or canceled occurs. To handle progress updates, provide the onProgress parameter.

    // Only a terminal block is necessary
    val listener3 = createListener3 {}
    
    // Including progress callback
    val listener3 = createListener3(
        onProgress = { task, currentOffset, totalLength -> }
    ) {}