Ketch — Android File Downloader Library
Tags: kotlin, android, androiddev, opensource
Published: 2024-05-05 17:44:29

## About Ketch

Ketch is simple, powerful, customisable file downloader library for Android built entirely in Kotlin. It simplifies the process of downloading files in Android applications by leveraging the power of WorkManager. Ketch guarantees the download irrespective of application state.

## Why use Ketch?

- Ketch can download any type of file. (jpg, png, gif, mp4, mp3, pdf, apk and many more) and guarantees download unless cancelled explicitly or download is failed.
- Ketch provide all download info including speed, file size, progress.
- Ketch provide various callbacks like onQueue, onStart, onProgress, onSuccess, onFailure, onCancel. Ketch also provide observable flow of download items.
- Ketch can download multiple files in parallel with support of cancellation of downloads.
- Ketch provide notification for each download providing download info (speed, time left, total size, progress) with cancel action.

## Usage

Ketch is very simple to use: Add the dependency, instantiate the library, pass the url to download and get the callbacks.

```Kotlin
// build.gradle
dependencies {
  implementation 'com.github.khushpanchal:Ketch:1.0.0'
}

//Activity or Fragment
private lateinit var ketch: Ketch
override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  ketch = Ketch.init(this)
  
  //To download the file
  val request = ketch.download(url,
    onQueue = {},
    onStart = { length -> },
    onProgress = { progress, speedInBytePerMs, length -> },
    onSuccess = { },
    onFailure = { error -> },
    onCancel = { }
  )
  //To cancel the download
  ketch.cancel(request.id)
}
```

To enable the notification, add appropriate notification permission and pass notification config while initialising the library.

```Kotlin
ketch = Ketch.init(this,
         notificationConfig = NotificationConfig(
             enabled = true,
             smallIcon = R.drawable.ic_launcher_foreground // It is required to pass the smallIcon for notification.
         )
       )
```

## How Ketch works?

### High Level Design


![Ketch_HLD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7zvn7mgrz6f2k8399nc.jpeg)

### Component Details

- **Ketch**: It is the core class client interacts with. It has various exposed function including init, download, cancel. It has the instance of DownloadManager.
- **DownloadManager**: It is the central class of library that is responsible for storing in memory queue of downloads mapped with unique id. It has the instance of WorkManager and is responsible for enqueuing the work request. It also observe the work info provided by WorkManager. Additionally this class has StateFlow and it emits the list of download items to the client.
- **DownloadWorker**: It extends CoroutineWorker for downloading the items in background. It has the instance of DownloadTask and DownloadNotificationManager.
- **DownloadTask**: It has the suspend function download and provide callbacks like onStart and onProgress. It has the length of file and also carries the logic of calculating download speed.
- **DownloadService**: It is an interface that has download suspend function and returns ResponseBody. Library uses Retrofit for making the network request.
- **DownloadNotificationManager**: It is responsible for showing the notification to user. It creates notification channel and notification. It also sendBroadcast which is received by NotificationReceiver after work is finished.
- **NotificationReceiver**: It is responsible for showing the end notification to user after Download is finished. It shows Cancel, Failed and Successful Notification message.

### Ketch provides lot more features with plenty of customisation options, check out the GitHub project for more information:

Github Project: [https://github.com/khushpanchal/Ketch](https://github.com/khushpanchal/Ketch)

If this project helps you, show love ❤️ by putting a ⭐ on this project ✌️

**Contact Me:** [LinkedIn](https://www.linkedin.com/in/khush-panchal-241098170/), [Twitter](https://twitter.com/KhushPanchal15)

Happy coding ✌️