funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/activities/DownloadsActivity.kt

134 lines
4.0 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
2021-07-16 10:03:52 +02:00
import audio.funkwhale.ffa.adapters.DownloadsAdapter
import audio.funkwhale.ffa.databinding.ActivityDownloadsBinding
import audio.funkwhale.ffa.utils.Event
import audio.funkwhale.ffa.utils.EventBus
import audio.funkwhale.ffa.utils.getMetadata
import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadManager
import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.koin.java.KoinJavaComponent.inject
class DownloadsActivity : AppCompatActivity() {
2021-07-16 10:03:52 +02:00
private lateinit var adapter: DownloadsAdapter
private lateinit var binding: ActivityDownloadsBinding
private val exoDownloadManager: DownloadManager by inject(DownloadManager::class.java)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2021-07-16 10:03:52 +02:00
binding = ActivityDownloadsBinding.inflate(layoutInflater)
2021-07-16 10:03:52 +02:00
setContentView(binding.root)
2021-07-16 10:03:52 +02:00
binding.downloads.itemAnimator = null
adapter = DownloadsAdapter(layoutInflater, this, DownloadChangedListener()).also {
it.setHasStableIds(true)
2021-07-16 10:03:52 +02:00
binding.downloads.layoutManager = LinearLayoutManager(this)
binding.downloads.adapter = it
}
lifecycleScope.launch(Default) {
while (true) {
2020-06-22 22:25:03 +02:00
delay(1000)
refreshProgress()
}
}
}
override fun onResume() {
super.onResume()
lifecycleScope.launch(Default) {
EventBus.get().collect { event ->
if (event is Event.DownloadChanged) {
refreshTrack(event.download)
}
}
}
refresh()
}
private fun refresh() {
lifecycleScope.launch(Main) {
adapter.downloads.clear()
2022-06-21 10:03:46 +02:00
exoDownloadManager.downloadIndex.getDownloads()
.use { cursor ->
while (cursor.moveToNext()) {
val download = cursor.download
download.getMetadata()?.let { info ->
adapter.downloads.add(
info.apply { this.download = download }
)
}
}
}
adapter.notifyDataSetChanged()
}
}
private suspend fun refreshTrack(download: Download) {
download.getMetadata()?.let { info ->
2021-07-16 10:03:52 +02:00
adapter.downloads.withIndex().associate { it.value to it.index }
.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
if (download.state != info.download?.state) {
withContext(Main) {
adapter.downloads[match.second] = info.apply {
this.download = download
}
2021-07-16 10:03:52 +02:00
adapter.notifyItemChanged(match.second)
}
}
}
}
}
private suspend fun refreshProgress() {
2022-06-21 10:03:46 +02:00
exoDownloadManager.downloadIndex.getDownloads()
.use { cursor ->
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)
}
2021-07-16 10:03:52 +02:00
}
}
}
2022-06-21 10:03:46 +02:00
}
}
}
inner class DownloadChangedListener : DownloadsAdapter.OnDownloadChangedListener {
override fun onItemRemoved(index: Int) {
adapter.downloads.removeAt(index)
adapter.notifyDataSetChanged()
}
}
2021-07-02 13:55:49 +02:00
}