Skip to content

Commit

Permalink
Make ResponseProgressListener a fun interface
Browse files Browse the repository at this point in the history
  • Loading branch information
1ud0v1c committed Aug 15, 2023
1 parent 8294084 commit bdbd77f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package com.ludovic.vimont.nasaapod.api.glide
* the download with a percentage.
*/
class GlideDispatchProgressListener: ResponseProgressListener {

companion object {
private const val HUNDRED_PERCENT: Int = 100
private val listeners = HashMap<String, UIDownloadProgressListener>()
Expand All @@ -20,11 +21,10 @@ class GlideDispatchProgressListener: ResponseProgressListener {
}
}

override fun update(url: String, totalBytesRead: Long, contentLength: Long, isDone: Boolean) {
override fun invoke(url: String, totalBytesRead: Long, contentLength: Long, isDone: Boolean) {
val currentProgress: Int = (HUNDRED_PERCENT * totalBytesRead / contentLength).toInt()
if (isDispatchNeeded(url, currentProgress)) {
listeners[url]?.invoke(currentProgress)
}
if (!isDispatchNeeded(url, currentProgress)) return
listeners[url]?.invoke(currentProgress)
}

private fun isDispatchNeeded(key: String, currentProgress: Int): Boolean {
Expand All @@ -41,4 +41,5 @@ class GlideDispatchProgressListener: ResponseProgressListener {
progresses[key] = currentProgress
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ import okio.Source
class OkHttpResponseBody(private val httpUrl: HttpUrl,
private val responseBody: ResponseBody,
private val progressListener: ResponseProgressListener) : ResponseBody() {
private var bufferedSource: BufferedSource? = null

override fun contentType(): MediaType? {
return responseBody.contentType()
private val bufferedSource: BufferedSource by lazy {
Okio.buffer(source(responseBody.source()))
}

override fun contentLength(): Long {
return responseBody.contentLength()
}
override fun contentType(): MediaType? = responseBody.contentType()

override fun source(): BufferedSource? {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(responseBody.source()))
}
return bufferedSource
}
override fun contentLength(): Long = responseBody.contentLength()

override fun source(): BufferedSource = bufferedSource

private fun source(source: Source): Source {
return object : ForwardingSource(source) {
Expand All @@ -48,9 +42,17 @@ class OkHttpResponseBody(private val httpUrl: HttpUrl,
}
val isDone: Boolean = (bytesRead == -1L)
val url: String = httpUrl.uri().toString()
progressListener.update(url, totalBytesRead, responseBody.contentLength(), isDone)

progressListener.invoke(
url = url,
totalBytesRead = totalBytesRead,
contentLength = responseBody.contentLength(),
isDone = isDone,
)

return bytesRead
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ludovic.vimont.nasaapod.api.glide

interface ResponseProgressListener {
fun update(
fun interface ResponseProgressListener {

operator fun invoke(
url: String,
totalBytesRead: Long,
contentLength: Long,
isDone: Boolean
)

}

0 comments on commit bdbd77f

Please sign in to comment.