@ -13,6 +13,7 @@ import kotlinx.android.synthetic.main.activity_downloads.*
@@ -13,6 +13,7 @@ import kotlinx.android.synthetic.main.activity_downloads.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -24,10 +25,21 @@ class DownloadsActivity : AppCompatActivity() {
@@ -24,10 +25,21 @@ class DownloadsActivity : AppCompatActivity() {
setContentView ( R . layout . activity _downloads )
downloads . itemAnimator = null
adapter = DownloadsAdapter ( this , DownloadChangedListener ( ) ) . also {
it . setHasStableIds ( true )
downloads . layoutManager = LinearLayoutManager ( this )
downloads . adapter = it
}
GlobalScope . launch ( IO ) {
while ( true ) {
delay ( 500 )
refreshProgress ( )
}
}
}
override fun onResume ( ) {
@ -80,6 +92,28 @@ class DownloadsActivity : AppCompatActivity() {
@@ -80,6 +92,28 @@ class DownloadsActivity : AppCompatActivity() {
}
}
private suspend fun refreshProgress ( ) {
val cursor = Otter . get ( ) . exoDownloadManager . downloadIndex . getDownloads ( )
while ( cursor . moveToNext ( ) ) {
val download = cursor . download
download . getMetadata ( ) ?. let { info ->
adapter . downloads . withIndex ( ) . associate { it . value to it . index } . filter { it . key . id == info . id } . toList ( ) . getOrNull ( 0 ) ?. let { match ->
if ( download . state == Download . STATE _DOWNLOADING && download . percentDownloaded != info . download ?. percentDownloaded ?: 0 ) {
withContext ( Main ) {
adapter . downloads [ match . second ] = info . apply {
this . download = download
}
adapter . notifyItemChanged ( match . second )
}
}
}
}
}
}
inner class DownloadChangedListener : DownloadsAdapter . OnDownloadChangedListener {
override fun onItemRemoved ( index : Int ) {
adapter . downloads . removeAt ( index )