Browse Source

Periodically refresh download progress while in DownloadsActivity.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
ff2a915ba4
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 34
      app/src/main/java/com/github/apognu/otter/activities/DownloadsActivity.kt
  2. 6
      app/src/main/java/com/github/apognu/otter/adapters/DownloadsAdapter.kt
  3. 5
      app/src/main/res/layout/row_download.xml

34
app/src/main/java/com/github/apognu/otter/activities/DownloadsActivity.kt

@ -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)

6
app/src/main/java/com/github/apognu/otter/adapters/DownloadsAdapter.kt

@ -22,6 +22,8 @@ class DownloadsAdapter(private val context: Context, private val listener: OnDow @@ -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 @@ -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

5
app/src/main/res/layout/row_download.xml

@ -5,7 +5,10 @@ @@ -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

Loading…
Cancel
Save