Async file downloader for Android
FileBox is an async file downloader library for Android.
Before we develop filebox, we though that URL content changes very rarely. So basically this library downloads a given URL, and if it is already downloaded, it provides downloaded content directly.
FileBox;
val fileBoxRequest = FileBoxRequest("https://url1.png")
val filebox= FileBoxProvider.newInstance(applicationContext, FileBoxConfig.createDefault())
filebox.get(fileBoxRequest)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { fileBoxResponse ->
when (fileBoxResponse) {
is FileBoxResponse.Downloading -> {
val progress: Float = fileBoxResponse.progress
val ongoingRecord: Record = fileBoxResponse.record
}
is FileBoxResponse.Complete -> {
val savedRecord: Record = fileBoxResponse.record
val savedPath = fileBoxResponse.record.getReadableFilePath()
}
is FileBoxResponse.Error -> {
val savedRecord: Record = fileBoxResponse.record
val error = fileBoxResponse.throwable
}
}
}
val fileBoxConfig = FileBoxConfig.FileBoxConfigBuilder()
.setCryptoType(CryptoType.CONCEAL) // Default is CryptoType.NONE
.setTTLInMillis(TimeUnit.DAYS.toMillis(7)) // Default is 7 Days
.setDirectory(DirectoryType.CACHE) // Default is External
.setFolderName("MyPhotos") // Default is none
.build()
val fileBoxMultipleRequest = FileBoxMultiRequest(
arrayListOf(
FileBoxRequest("https://url1.png"),
FileBoxRequest("https://url2.zip"),
FileBoxRequest("https://url3.json"),
FileBoxRequest("https://url4.mp4")
)
)
val filebox = FileBoxProvider.newInstance(applicationContext, FileBoxConfig.createDefault())
filebox.get(fileBoxMultipleRequest)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { fileBoxResponse ->
when (fileBoxResponse) {
is FileBoxMultiResponse.Downloading -> {
val progress = fileBoxResponse.progress
val ongoingFileResponseList = fileBoxResponse.fileBoxResponseList
}
is FileBoxMultiResponse.Complete -> {
val ongoingFileResponseList = fileBoxResponse.fileBoxResponseList
ongoingFileResponseList.forEach { it.record.getReadableFilePath() }
}
is FileBoxMultiResponse.Error -> {
val error = fileBoxResponse.throwable
val ongoingFileResponseList = fileBoxResponse.fileBoxResponseList
}
}
}
Don't forget to destroy the filebox when your initialized scope is destroyed. If you create a filebox in your application class, It is application scoped. If you create a filebox in your activity class, It is activity scoped. Be aware of your lifecycle.
filebox.destroy()
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.lyrebirdstudio:filebox:0.1'
}
Copyright 2020 Lyrebird Studio.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.