Browse Source

Add downloaded indicators in favorites view.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
94fd3d51aa
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 6
      app/src/main/java/com/github/apognu/otter/adapters/FavoritesAdapter.kt
  2. 11
      app/src/main/java/com/github/apognu/otter/fragments/FavoritesFragment.kt
  3. 14
      app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt
  4. 12
      app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt
  5. 38
      app/src/main/java/com/github/apognu/otter/repositories/TracksRepository.kt

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

@ -74,6 +74,11 @@ class FavoritesAdapter(private val context: Context?, private val favoriteListen @@ -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 @@ -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))
}

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

@ -5,6 +5,7 @@ import androidx.recyclerview.widget.RecyclerView @@ -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>() { @@ -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()
}
}
}
}

14
app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt

@ -123,16 +123,14 @@ class TracksFragment : FunkwhaleFragment<Track, TracksAdapter>() { @@ -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()
}
}
}

12
app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt

@ -10,6 +10,7 @@ import com.google.gson.reflect.TypeToken @@ -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 @@ -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) {

38
app/src/main/java/com/github/apognu/otter/repositories/TracksRepository.kt

@ -18,6 +18,26 @@ class TracksRepository(override val context: Context?, albumId: Int) : Repositor @@ -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 @@ -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
}
}
}
Loading…
Cancel
Save