Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onProgressBytes method to Listener interface. #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ downloadId = kDownloader.enqueue(request,
onStart = {
},
onProgress = {
},
},
onProgressBytes = { currentBytes, totalBytes ->
},
onCompleted = {
},
onError = {
Expand Down
43 changes: 38 additions & 5 deletions app/src/main/java/com/example/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -35,27 +36,27 @@ 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(
"https://media.giphy.com/media/Bk0CW5frw4qfS/giphy.gif", dirPath, "giphy.gif",
).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()


Expand All @@ -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%"
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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%"
Expand Down Expand Up @@ -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)}"
}
}
2 changes: 2 additions & 0 deletions library/src/main/java/com/kdownloader/KDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down