From 5a14b3cfa4499d1892c9e97e92a7e1ce66fa8731 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Mon, 21 Oct 2019 20:26:26 +0200 Subject: [PATCH] Set page size to 50. Added favorite management on playlists. Fixed loop over loading of playlist tracks. --- .../otter/adapters/PlaylistTracksAdapter.kt | 24 ++++++++++++++++++- .../otter/fragments/PlaylistTracksFragment.kt | 15 +++++++++++- .../apognu/otter/fragments/TracksFragment.kt | 1 - .../apognu/otter/repositories/HttpUpstream.kt | 4 +++- .../repositories/PlaylistTracksRepository.kt | 24 +++++++++++++++---- .../github/apognu/otter/utils/AppContext.kt | 2 +- 6 files changed, 60 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt index f209266..58437a2 100644 --- a/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt +++ b/app/src/main/java/com/github/apognu/otter/adapters/PlaylistTracksAdapter.kt @@ -18,7 +18,11 @@ import jp.wasabeef.picasso.transformations.RoundedCornersTransformation import kotlinx.android.synthetic.main.row_track.view.* import java.util.* -class PlaylistTracksAdapter(private val context: Context?, val fromQueue: Boolean = false) : FunkwhaleAdapter() { +class PlaylistTracksAdapter(private val context: Context?, private val favoriteListener: OnFavoriteListener? = null, val fromQueue: Boolean = false) : FunkwhaleAdapter() { + interface OnFavoriteListener { + fun onToggleFavorite(id: Int, state: Boolean) + } + private lateinit var touchHelper: ItemTouchHelper var currentTrack: Track? = null @@ -77,6 +81,22 @@ class PlaylistTracksAdapter(private val context: Context?, val fromQueue: Boolea holder.artist.setTypeface(holder.artist.typeface, Typeface.BOLD) } + context?.let { + when (track.track.favorite) { + true -> holder.favorite.setColorFilter(context.getColor(R.color.colorFavorite)) + false -> holder.favorite.setColorFilter(context.getColor(R.color.colorSelected)) + } + + holder.favorite.setOnClickListener { + favoriteListener?.let { + favoriteListener.onToggleFavorite(track.track.id, !track.track.favorite) + + track.track.favorite = !track.track.favorite + notifyItemChanged(position) + } + } + } + holder.actions.setOnClickListener { context?.let { context -> PopupMenu(context, holder.actions, Gravity.START, R.attr.actionOverflowMenuStyle, 0).apply { @@ -130,6 +150,8 @@ class PlaylistTracksAdapter(private val context: Context?, val fromQueue: Boolea val cover = view.cover val title = view.title val artist = view.artist + + val favorite = view.favorite val actions = view.actions override fun onClick(view: View?) { diff --git a/app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt index 505b5b7..4260f1a 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/PlaylistTracksFragment.kt @@ -6,6 +6,7 @@ import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.apognu.otter.R import com.github.apognu.otter.adapters.PlaylistTracksAdapter +import com.github.apognu.otter.repositories.FavoritesRepository import com.github.apognu.otter.repositories.PlaylistTracksRepository import com.github.apognu.otter.utils.* import com.squareup.picasso.Picasso @@ -18,6 +19,8 @@ class PlaylistTracksFragment : FunkwhaleFragment favoritesRepository.addFavorite(id) + false -> favoritesRepository.deleteFavorite(id) + } + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt index 098b7c0..d793fe8 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/TracksFragment.kt @@ -117,6 +117,5 @@ class TracksFragment : FunkwhaleFragment() { false -> favoritesRepository.deleteFavorite(id) } } - } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt b/app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt index 66879f3..2ed4af5 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt @@ -20,7 +20,7 @@ import kotlin.math.ceil class HttpUpstream>(private val behavior: Behavior, private val url: String, private val type: Type) : Upstream { enum class Behavior { - AtOnce, Progressive + Single, AtOnce, Progressive } private var _channel: Channel>? = null @@ -34,6 +34,8 @@ class HttpUpstream>(private val behavior: Beha } override fun fetch(data: List): Channel>? { + if (behavior == Behavior.Single && data.isNotEmpty()) return null + val page = ceil(data.size / AppContext.PAGE_SIZE.toDouble()).toInt() + 1 GlobalScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/com/github/apognu/otter/repositories/PlaylistTracksRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/PlaylistTracksRepository.kt index 6456b57..18786a2 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/PlaylistTracksRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/PlaylistTracksRepository.kt @@ -1,18 +1,32 @@ package com.github.apognu.otter.repositories import android.content.Context -import com.github.apognu.otter.utils.FunkwhaleResponse -import com.github.apognu.otter.utils.PlaylistTrack -import com.github.apognu.otter.utils.PlaylistTracksCache -import com.github.apognu.otter.utils.PlaylistTracksResponse +import com.github.apognu.otter.utils.* import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.google.gson.reflect.TypeToken +import kotlinx.coroutines.runBlocking import java.io.BufferedReader class PlaylistTracksRepository(override val context: Context?, playlistId: Int) : Repository() { override val cacheId = "tracks-playlist-$playlistId" - override val upstream = HttpUpstream>(HttpUpstream.Behavior.AtOnce, "/api/v1/playlists/$playlistId/tracks?playable=true", object : TypeToken() {}.type) + override val upstream = HttpUpstream>(HttpUpstream.Behavior.Single, "/api/v1/playlists/$playlistId/tracks?playable=true", object : TypeToken() {}.type) override fun cache(data: List) = PlaylistTracksCache(data) override fun uncache(reader: BufferedReader) = gsonDeserializerOf(PlaylistTracksCache::class.java).deserialize(reader) + + override fun onDataFetched(data: List): List = runBlocking { + val favorites = FavoritesRepository(context).fetch(Origin.Network.origin).receive().data + + log(favorites.toString()) + + data.map { track -> + val favorite = favorites.find { it.track.id == track.track.id } + + if (favorite != null) { + track.track.favorite = true + } + + track + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/utils/AppContext.kt b/app/src/main/java/com/github/apognu/otter/utils/AppContext.kt index 2c41537..cb2c267 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/AppContext.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/AppContext.kt @@ -19,7 +19,7 @@ object AppContext { const val NOTIFICATION_MEDIA_CONTROL = 1 const val NOTIFICATION_CHANNEL_MEDIA_CONTROL = "mediacontrols" - const val PAGE_SIZE = 7 + const val PAGE_SIZE = 50 const val TRANSITION_DURATION = 300L fun init(context: Activity) {