Browse Source

Fix performance issue on download tracking from the Favorites section.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
abff279df9
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 44
      app/src/main/java/com/github/apognu/otter/fragments/FavoritesFragment.kt

44
app/src/main/java/com/github/apognu/otter/fragments/FavoritesFragment.kt

@ -7,11 +7,14 @@ import com.github.apognu.otter.adapters.FavoritesAdapter @@ -7,11 +7,14 @@ import com.github.apognu.otter.adapters.FavoritesAdapter
import com.github.apognu.otter.repositories.FavoritesRepository
import com.github.apognu.otter.repositories.TracksRepository
import com.github.apognu.otter.utils.*
import com.google.android.exoplayer2.offline.Download
import kotlinx.android.synthetic.main.fragment_favorites.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
override val viewRes = R.layout.fragment_favorites
@ -29,11 +32,15 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() { @@ -29,11 +32,15 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
override fun onResume() {
super.onResume()
GlobalScope.launch(Main) {
GlobalScope.launch(IO) {
RequestBus.send(Request.GetCurrentTrack).wait<Response.CurrentTrack>()?.let { response ->
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
withContext(Main) {
adapter.currentTrack = response.track
adapter.notifyDataSetChanged()
}
}
refreshDownloadedTracks()
}
play.setOnClickListener {
@ -47,15 +54,32 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() { @@ -47,15 +54,32 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
when (message) {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> {
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
is Event.DownloadChanged -> refreshDownloadedTrack(message.download)
}
}
}
}
private suspend fun refreshDownloadedTracks() {
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
withContext(Main) {
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
adapter.notifyDataSetChanged()
}
}
adapter.notifyDataSetChanged()
private suspend fun refreshDownloadedTrack(download: Download) {
if (download.state == Download.STATE_COMPLETED) {
download.getMetadata()?.let { info ->
adapter.data.withIndex().associate { it.value to it.index }.filter { it.key.id == info.id }.toList().getOrNull(0)?.let { match ->
withContext(Main) {
adapter.data[match.second].downloaded = true
adapter.notifyItemChanged(match.second)
}
}
}

Loading…
Cancel
Save