From b51407d6e10de7505c4a28827ec34afc205724ca Mon Sep 17 00:00:00 2001 From: Md Imam Hossain Date: Sun, 3 Mar 2024 01:43:32 +0600 Subject: [PATCH] Added onProgressBytes method to Listener interface. --- README.md | 4 +- app/src/main/java/com/example/MainActivity.kt | 43 ++++++++++++++++--- .../main/java/com/kdownloader/KDownloader.kt | 2 + .../internal/DownloadDispatchers.kt | 3 ++ .../kdownloader/internal/DownloadRequest.kt | 1 + .../com/kdownloader/internal/DownloadTask.kt | 4 ++ 6 files changed, 51 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 815098e..a4a990b 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,9 @@ downloadId = kDownloader.enqueue(request, onStart = { }, onProgress = { - }, + }, + onProgressBytes = { currentBytes, totalBytes -> + }, onCompleted = { }, onError = { diff --git a/app/src/main/java/com/example/MainActivity.kt b/app/src/main/java/com/example/MainActivity.kt index 5046e19..c5cbddc 100644 --- a/app/src/main/java/com/example/MainActivity.kt +++ b/app/src/main/java/com/example/MainActivity.kt @@ -7,6 +7,7 @@ import android.view.View import com.example.databinding.ActivityMainBinding import com.kdownloader.KDownloader import com.kdownloader.Status +import java.util.Locale class MainActivity : AppCompatActivity() { @@ -35,15 +36,15 @@ class MainActivity : AppCompatActivity() { dirPath = Environment.getExternalStorageDirectory().path + "/Download" val request1 = kDownloader.newRequestBuilder( - "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_30mb.mp4", dirPath, "bunny.mp4", + "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4", dirPath, "bunny.mp4", ).tag(TAG + "1").build() val request2 = kDownloader.newRequestBuilder( - "https://speed.hetzner.de/100MB.bin", dirPath, "100MB.bin", + "https://ash-speed.hetzner.com/100MB.bin", dirPath, "100MB.bin", ).tag(TAG + "2").build() val request3 = kDownloader.newRequestBuilder( - "https://file-examples.com/storage/fe3286c49f6458f86eb9ed5/2017/10/file-example_PDF_1MB.pdf", dirPath, "docu.pdf", + "https://filesamples.com/samples/document/pdf/sample3.pdf", dirPath, "docu.pdf", ).tag(TAG + "3").build() val request4 = kDownloader.newRequestBuilder( @@ -51,11 +52,11 @@ class MainActivity : AppCompatActivity() { ).tag(TAG + "4").build() val request5 = kDownloader.newRequestBuilder( - "https://speed.hetzner.de/1GB.bin", dirPath, "1GB.bin", + "https://ash-speed.hetzner.com/1GB.bin", dirPath, "1GB.bin", ).tag(TAG + "5").build() val request6 = kDownloader.newRequestBuilder( - "https://file-examples.com/storage/fe3286c49f6458f86eb9ed5/2017/11/file_example_MP3_5MG.mp3", dirPath, "music.mp3", + "https://freetestdata.com/wp-content/uploads/2021/09/Free_Test_Data_1OMB_MP3.mp3", dirPath, "music.mp3", ).tag(TAG + "6").build() @@ -78,6 +79,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar1.progress = it binding.progressText1.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText1.text.toString() + binding.progressText1.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status1.text = "Completed" binding.progressText3.text = "100%" @@ -128,6 +133,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar2.progress = it binding.progressText2.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText2.text.toString() + binding.progressText2.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status2.text = "Completed" binding.progressText3.text = "100%" @@ -181,6 +190,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar3.progress = it binding.progressText3.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText3.text.toString() + binding.progressText3.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status3.text = "Completed" binding.progressText3.text = "100%" @@ -233,6 +246,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar4.progress = it binding.progressText4.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText4.text.toString() + binding.progressText4.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status4.text = "Completed" binding.progressText4.text = "100%" @@ -284,6 +301,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar5.progress = it binding.progressText5.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText5.text.toString() + binding.progressText5.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status5.text = "Completed" binding.progressText5.text = "100%" @@ -336,6 +357,10 @@ class MainActivity : AppCompatActivity() { binding.progressBar6.progress = it binding.progressText6.text = "$it%" }, + onProgressBytes = { currentBytes, totalBytes -> + val value = binding.progressText6.text.toString() + binding.progressText6.text = "$value ${getProgressDisplayLine(currentBytes, totalBytes)}" + }, onCompleted = { binding.status6.text = "Completed" binding.progressText6.text = "100%" @@ -366,4 +391,12 @@ class MainActivity : AppCompatActivity() { } } + + private fun getBytesToMBString(bytes: Long): String { + return String.format(Locale.ENGLISH, "%.2fMB", bytes / (1024.00 * 1024.00)) + } + + private fun getProgressDisplayLine(currentBytes: Long, totalBytes: Long): String { + return "${getBytesToMBString(currentBytes)} / ${getBytesToMBString(totalBytes)}" + } } \ No newline at end of file diff --git a/library/src/main/java/com/kdownloader/KDownloader.kt b/library/src/main/java/com/kdownloader/KDownloader.kt index da52045..1adc354 100644 --- a/library/src/main/java/com/kdownloader/KDownloader.kt +++ b/library/src/main/java/com/kdownloader/KDownloader.kt @@ -41,12 +41,14 @@ class KDownloader private constructor(dbHelper: DbHelper, private val config: Do req: DownloadRequest, crossinline onStart: () -> Unit = {}, crossinline onProgress: (value: Int) -> Unit = { _ -> }, + crossinline onProgressBytes: (currentBytes: Long, totalBytes: Long) -> Unit = { _, _ -> }, crossinline onPause: () -> Unit = {}, crossinline onError: (error: String) -> Unit = { _ -> }, crossinline onCompleted: () -> Unit = {} ) = enqueue(req, object : DownloadRequest.Listener { override fun onStart() = onStart() override fun onProgress(value: Int) = onProgress(value) + override fun onProgressBytes(currentBytes: Long, totalBytes: Long) = onProgressBytes(currentBytes, totalBytes) override fun onPause() = onPause() override fun onError(error: String) = onError(error) override fun onCompleted() = onCompleted() diff --git a/library/src/main/java/com/kdownloader/internal/DownloadDispatchers.kt b/library/src/main/java/com/kdownloader/internal/DownloadDispatchers.kt index 116ba16..552640b 100644 --- a/library/src/main/java/com/kdownloader/internal/DownloadDispatchers.kt +++ b/library/src/main/java/com/kdownloader/internal/DownloadDispatchers.kt @@ -35,6 +35,9 @@ class DownloadDispatchers(private val dbHelper: DbHelper) { onProgress = { executeOnMainThread { request.listener?.onProgress(it) } }, + onProgressBytes = { it, it1 -> + executeOnMainThread { request.listener?.onProgressBytes(it, it1) } + }, onPause = { executeOnMainThread { request.listener?.onPause() } }, diff --git a/library/src/main/java/com/kdownloader/internal/DownloadRequest.kt b/library/src/main/java/com/kdownloader/internal/DownloadRequest.kt index c4ffc6c..3726c97 100644 --- a/library/src/main/java/com/kdownloader/internal/DownloadRequest.kt +++ b/library/src/main/java/com/kdownloader/internal/DownloadRequest.kt @@ -73,6 +73,7 @@ class DownloadRequest private constructor( interface Listener { fun onStart() fun onProgress(value: Int) + fun onProgressBytes(currentBytes: Long, totalBytes: Long) fun onPause() fun onCompleted() fun onError(error: String) diff --git a/library/src/main/java/com/kdownloader/internal/DownloadTask.kt b/library/src/main/java/com/kdownloader/internal/DownloadTask.kt index c1eb4ef..658e535 100644 --- a/library/src/main/java/com/kdownloader/internal/DownloadTask.kt +++ b/library/src/main/java/com/kdownloader/internal/DownloadTask.kt @@ -52,6 +52,7 @@ class DownloadTask( suspend inline fun run( crossinline onStart: () -> Unit = {}, crossinline onProgress: (value: Int) -> Unit = { _ -> }, + crossinline onProgressBytes: (currentBytes: Long, totalBytes: Long) -> Unit = { _, _ -> }, crossinline onError: (error: String) -> Unit = { _ -> }, crossinline onCompleted: () -> Unit = {}, crossinline onPause: () -> Unit = {} @@ -60,6 +61,8 @@ class DownloadTask( override fun onProgress(value: Int) = onProgress(value) + override fun onProgressBytes(currentBytes: Long, totalBytes: Long) = onProgressBytes(currentBytes, totalBytes) + override fun onError(error: String) = onError(error) override fun onCompleted() = onCompleted() @@ -228,6 +231,7 @@ class DownloadTask( progress = ((req.downloadedBytes * 100) / totalBytes).toInt() } listener.onProgress(progress) + listener.onProgressBytes(req.downloadedBytes, totalBytes) } while (true) if (req.status === Status.CANCELLED) {