Add downloaded indicators in favorites view.

This commit is contained in:
Antoine POPINEAU 2020-06-13 19:50:02 +02:00
parent a2caba8bd1
commit 94fd3d51aa
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
5 changed files with 52 additions and 29 deletions

View File

@ -74,6 +74,11 @@ class FavoritesAdapter(private val context: Context?, private val favoriteListen
false -> holder.favorite.setColorFilter(context.getColor(R.color.colorSelected))
}
when (favorite.downloaded) {
true -> holder.title.setCompoundDrawablesWithIntrinsicBounds(R.drawable.downloaded, 0, 0, 0)
false -> holder.title.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
}
holder.favorite.setOnClickListener {
favoriteListener.onToggleFavorite(favorite.id, !favorite.favorite)
@ -91,6 +96,7 @@ class FavoritesAdapter(private val context: Context?, private val favoriteListen
when (it.itemId) {
R.id.track_add_to_queue -> CommandBus.send(Command.AddToQueue(listOf(favorite)))
R.id.track_play_next -> CommandBus.send(Command.PlayNext(favorite))
R.id.track_pin -> CommandBus.send(Command.PinTrack(favorite))
R.id.queue_remove -> CommandBus.send(Command.RemoveFromQueue(favorite))
}

View File

@ -5,6 +5,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.R
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 kotlinx.android.synthetic.main.fragment_favorites.*
import kotlinx.coroutines.Dispatchers.Main
@ -46,6 +47,16 @@ class FavoritesFragment : FunkwhaleFragment<Track, FavoritesAdapter>() {
when (message) {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> {
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
adapter.notifyDataSetChanged()
}
}
}
}

View File

@ -123,16 +123,14 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() {
is Event.TrackPlayed -> refreshCurrentTrack()
is Event.RefreshTrack -> refreshCurrentTrack()
is Event.DownloadChanged -> {
(repository as? TracksRepository)?.let { repository ->
val downloaded = repository.getDownloadedIds() ?: listOf()
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
adapter.data = adapter.data.map {
it.downloaded = downloaded.contains(it.id)
it
}.toMutableList()
adapter.notifyDataSetChanged()
}
adapter.notifyDataSetChanged()
}
}
}

View File

@ -10,6 +10,7 @@ import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.BufferedReader
class FavoritesRepository(override val context: Context?) : Repository<Track, TracksCache>() {
@ -19,9 +20,14 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
override fun cache(data: List<Track>) = TracksCache(data)
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
override fun onDataFetched(data: List<Track>) = data.map {
it.favorite = true
it
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
data.map { track ->
track.favorite = true
track.downloaded = downloaded.contains(track.id)
track
}
}
fun addFavorite(id: Int) {

View File

@ -18,6 +18,26 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor
override fun cache(data: List<Track>) = TracksCache(data)
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
companion object {
suspend fun getDownloadedIds(): List<Int>? {
return RequestBus.send(Request.GetDownloads).wait<com.github.apognu.otter.utils.Response.Downloads>()?.let { response ->
val ids: MutableList<Int> = mutableListOf()
while (response.cursor.moveToNext()) {
val download = response.cursor.download
Gson().fromJson(String(download.request.data), DownloadInfo::class.java)?.let {
if (download.state == Download.STATE_COMPLETED) {
ids.add(it.id)
}
}
}
ids
}
}
}
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
val favorites = FavoritedRepository(context).fetch(Origin.Network.origin)
.map { it.data }
@ -32,22 +52,4 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor
track
}.sortedBy { it.position }
}
suspend fun getDownloadedIds(): List<Int>? {
return RequestBus.send(Request.GetDownloads).wait<com.github.apognu.otter.utils.Response.Downloads>()?.let { response ->
val ids: MutableList<Int> = mutableListOf()
while (response.cursor.moveToNext()) {
val download = response.cursor.download
Gson().fromJson(String(download.request.data), DownloadInfo::class.java)?.let {
if (download.state == Download.STATE_COMPLETED) {
ids.add(it.id)
}
}
}
ids
}
}
}