Skip to content

Latest commit

 

History

History
100 lines (89 loc) · 2.83 KB

README.md

File metadata and controls

100 lines (89 loc) · 2.83 KB

Android CI

DroidDownloader

A simple file downloader android library written in kotlin for modern android apps

INSTALLATION

Gradle

Add the JitPack repository to you project level build.gradle file

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}

Add the dependency to your module build.gradle file

dependencies {
  implementation 'com.github.Victor-El:DroidDownloader:v0.1.1-alpha.0'
}

Maven

Add the JitPack repository

<repositories>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

Add the dependency

<dependency>
  <groupId>com.github.Victor-El</groupId>
  <artifactId>DroidDownloader</artifactId>
  <version>0.1.1-alpha.0</version>
</dependency>

USAGE

Note: Depending on the download location add the neccessary permissions

Getting Downloader instance

val downloader: DroidDownloader = DefaultDroidDownloader.Builder()
            .addConnectTimeOutMillis(5_000)
            .addWriteTimeOutMillis(30_000)
            .build()

Downloading a file

suspend fun downloadFileAsync(
        url: String,
        path: String,
        filename: String,
    ) {
        val status = downloader.downloadFile(url, path, filename)
        completedTasks.add(status)
        withContext(Dispatchers.Main) {
            when (status) {
                is DownloadStatus.Success -> downloadFlow.emit(DownloadResult.Success(status.filePath))
                is DownloadStatus.Failure.NetworkFailure -> downloadFlow.emit(DownloadResult.Error(status.message))
                is DownloadStatus.Failure.DiskFailure -> downloadFlow.emit(DownloadResult.Error(status.message))
            }
        }
    }

Downloading a file with authorization

suspend fun downloadFileAsync(
        url: String,
        path: String,
        filename: String,
        authBearerToken: String,
    ) {
        val status = downloader.downloadFileWithBearerAuth(url, path, filename, authBearerToken)
        completedTasks.add(status)
        withContext(Dispatchers.Main) {
            when (status) {
                is DownloadStatus.Success -> downloadFlow.emit(DownloadResult.Success(status.filePath))
                is DownloadStatus.Failure.NetworkFailure -> downloadFlow.emit(DownloadResult.Error(status.message))
                is DownloadStatus.Failure.DiskFailure -> downloadFlow.emit(DownloadResult.Error(status.message))
            }
        }
    }

Author

Elezua Victor

License

This project is licensed under the Apache MIT License - See: https://github.com/Victor-El/DroidDownloader/blob/master/LICENSE