Fix / remember state and resend on rebind
This commit is contained in:
parent
174084a256
commit
f5c1ad8f2a
@ -27,6 +27,8 @@ import javax.inject.Inject
|
|||||||
class DefaultContentDownloadStateTracker @Inject constructor() : ProgressListener, ContentDownloadStateTracker {
|
class DefaultContentDownloadStateTracker @Inject constructor() : ProgressListener, ContentDownloadStateTracker {
|
||||||
|
|
||||||
private val mainHandler = Handler(Looper.getMainLooper())
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
|
// TODO this will grow undefinitly..
|
||||||
private val states = mutableMapOf<String, ContentDownloadStateTracker.State>()
|
private val states = mutableMapOf<String, ContentDownloadStateTracker.State>()
|
||||||
private val listeners = mutableMapOf<String, MutableList<ContentDownloadStateTracker.UpdateListener>>()
|
private val listeners = mutableMapOf<String, MutableList<ContentDownloadStateTracker.UpdateListener>>()
|
||||||
|
|
||||||
@ -35,6 +37,14 @@ class DefaultContentDownloadStateTracker @Inject constructor() : ProgressListene
|
|||||||
if (!listeners.contains(updateListener)) {
|
if (!listeners.contains(updateListener)) {
|
||||||
listeners.add(updateListener)
|
listeners.add(updateListener)
|
||||||
}
|
}
|
||||||
|
val currentState = states[key] ?: ContentDownloadStateTracker.State.Idle
|
||||||
|
mainHandler.post {
|
||||||
|
try {
|
||||||
|
updateListener.onDownloadStateUpdate(currentState)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Timber.e(e, "## ContentUploadStateTracker.onUpdate() failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun unTrack(key: String, updateListener: ContentDownloadStateTracker.UpdateListener) {
|
override fun unTrack(key: String, updateListener: ContentDownloadStateTracker.UpdateListener) {
|
||||||
@ -51,21 +61,25 @@ class DefaultContentDownloadStateTracker @Inject constructor() : ProgressListene
|
|||||||
|
|
||||||
override fun update(url: String, bytesRead: Long, contentLength: Long, done: Boolean) {
|
override fun update(url: String, bytesRead: Long, contentLength: Long, done: Boolean) {
|
||||||
Timber.v("## DL Progress url:$url read:$bytesRead total:$contentLength done:$done")
|
Timber.v("## DL Progress url:$url read:$bytesRead total:$contentLength done:$done")
|
||||||
listeners[url]?.forEach {
|
if (done) {
|
||||||
tryThis {
|
updateState(url, ContentDownloadStateTracker.State.Success)
|
||||||
if (done) {
|
} else {
|
||||||
it.onDownloadStateUpdate(ContentDownloadStateTracker.State.Success)
|
updateState(url, ContentDownloadStateTracker.State.Downloading(bytesRead, contentLength, contentLength == -1L))
|
||||||
} else {
|
|
||||||
it.onDownloadStateUpdate(ContentDownloadStateTracker.State.Downloading(bytesRead, contentLength, contentLength == -1L))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun error(url: String, errorCode: Int) {
|
override fun error(url: String, errorCode: Int) {
|
||||||
Timber.v("## DL Progress Error code:$errorCode")
|
Timber.v("## DL Progress Error code:$errorCode")
|
||||||
|
updateState(url, ContentDownloadStateTracker.State.Failure(errorCode))
|
||||||
listeners[url]?.forEach {
|
listeners[url]?.forEach {
|
||||||
tryThis { it.onDownloadStateUpdate(ContentDownloadStateTracker.State.Failure(errorCode)) }
|
tryThis { it.onDownloadStateUpdate(ContentDownloadStateTracker.State.Failure(errorCode)) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateState(url: String, state: ContentDownloadStateTracker.State) {
|
||||||
|
states[url] = state
|
||||||
|
listeners[url]?.forEach {
|
||||||
|
tryThis { it.onDownloadStateUpdate(state) }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user