Skip to content

Commit

Permalink
feature: 下载进度回调再主线程中进行
Browse files Browse the repository at this point in the history
  • Loading branch information
tangwanchao committed Feb 2, 2023
1 parent 29a00b3 commit 24c0c29
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.twc.source.download

import androidx.annotation.MainThread
import okhttp3.ResponseBody
import java.io.File

Expand Down Expand Up @@ -31,11 +32,13 @@ interface DownloadCallback {
/**
* 开始下载
*/
@MainThread
fun onStart() {}

/**
* 下载中
*/
@MainThread
fun onDownload(downloadLength: Int, totalLength: Long) {}

/**
Expand Down
4 changes: 3 additions & 1 deletion download/src/main/java/me/twc/source/download/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class Downloader(
bs = responseBody.byteStream()
outputFile = callback.genSaveFile() ?: genSaveFile()
fos = FileOutputStream(outputFile)
callback.onStart()
withContext(Dispatchers.Main){
callback.onStart()
}
val totalLength = responseBody.contentLength()
var downloadLength = 0
val buffer = ByteArray(8 * 1024)
Expand Down

0 comments on commit 24c0c29

Please sign in to comment.