Periodically refresh download progress while in DownloadsActivity.

This commit is contained in:
Antoine POPINEAU 2020-06-22 22:24:34 +02:00
parent 03fcf1a382
commit ff2a915ba4
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
3 changed files with 42 additions and 3 deletions

View File

@ -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() {
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() {
}
}
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)

View File

@ -22,6 +22,8 @@ class DownloadsAdapter(private val context: Context, private val listener: OnDow
override fun getItemCount() = downloads.size
override fun getItemId(position: Int) = downloads[position].id.toLong()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.row_download, parent, false)
@ -37,12 +39,12 @@ class DownloadsAdapter(private val context: Context, private val listener: OnDow
download.download?.let { state ->
when (state.isTerminalState) {
true -> {
holder.progress.visibility = View.GONE
holder.progress.visibility = View.INVISIBLE
when (state.state) {
Download.STATE_FAILED -> {
holder.toggle.setImageDrawable(context.getDrawable(R.drawable.retry))
holder.progress.visibility = View.GONE
holder.progress.visibility = View.INVISIBLE
}
else -> holder.toggle.visibility = View.GONE

View File

@ -5,7 +5,10 @@
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal"
android:padding="16dp"
android:paddingStart="16dp"
android:paddingTop="6dp"
android:paddingEnd="16dp"
android:paddingBottom="6dp"
tools:showIn="@layout/activity_downloads">
<LinearLayout