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