2021-07-12 08:14:26 +00:00
|
|
|
package audio.funkwhale.ffa.repositories
|
2019-08-19 14:50:33 +00:00
|
|
|
|
|
|
|
import android.content.Context
|
2021-07-12 08:14:26 +00:00
|
|
|
import audio.funkwhale.ffa.FFA
|
2021-07-23 12:10:13 +00:00
|
|
|
import audio.funkwhale.ffa.utils.Cache
|
|
|
|
import audio.funkwhale.ffa.utils.FavoritedCache
|
|
|
|
import audio.funkwhale.ffa.utils.FavoritedResponse
|
|
|
|
import audio.funkwhale.ffa.utils.OAuth
|
|
|
|
import audio.funkwhale.ffa.utils.OtterResponse
|
|
|
|
import audio.funkwhale.ffa.utils.Settings
|
|
|
|
import audio.funkwhale.ffa.utils.Track
|
|
|
|
import audio.funkwhale.ffa.utils.TracksCache
|
|
|
|
import audio.funkwhale.ffa.utils.TracksResponse
|
|
|
|
import audio.funkwhale.ffa.utils.authorize
|
|
|
|
import audio.funkwhale.ffa.utils.maybeNormalizeUrl
|
|
|
|
import audio.funkwhale.ffa.utils.mustNormalizeUrl
|
|
|
|
import audio.funkwhale.ffa.utils.untilNetwork
|
2019-08-19 14:50:33 +00:00
|
|
|
import com.github.kittinunf.fuel.Fuel
|
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
|
|
|
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
|
|
|
import com.google.gson.Gson
|
|
|
|
import com.google.gson.reflect.TypeToken
|
2020-07-11 14:56:09 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2019-08-19 14:50:33 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2019-11-21 21:13:59 +00:00
|
|
|
import kotlinx.coroutines.launch
|
2020-06-13 17:50:02 +00:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2019-08-19 14:50:33 +00:00
|
|
|
import java.io.BufferedReader
|
|
|
|
|
2019-10-23 18:21:18 +00:00
|
|
|
class FavoritesRepository(override val context: Context?) : Repository<Track, TracksCache>() {
|
2021-07-23 12:10:13 +00:00
|
|
|
|
2019-10-23 18:21:18 +00:00
|
|
|
override val cacheId = "favorites.v2"
|
2021-07-23 12:10:13 +00:00
|
|
|
|
|
|
|
override val upstream = HttpUpstream<Track, OtterResponse<Track>>(
|
|
|
|
context!!,
|
|
|
|
HttpUpstream.Behavior.AtOnce,
|
|
|
|
"/api/v1/tracks/?favorites=true&playable=true&ordering=title",
|
|
|
|
object : TypeToken<TracksResponse>() {}.type
|
|
|
|
)
|
2019-08-19 14:50:33 +00:00
|
|
|
|
2019-10-23 18:21:18 +00:00
|
|
|
override fun cache(data: List<Track>) = TracksCache(data)
|
2021-07-23 12:10:13 +00:00
|
|
|
override fun uncache(reader: BufferedReader) =
|
|
|
|
gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
2019-08-19 14:50:33 +00:00
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
private val favoritedRepository = FavoritedRepository(context!!)
|
2020-07-10 13:08:08 +00:00
|
|
|
|
2020-06-13 17:50:02 +00:00
|
|
|
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)
|
2020-06-20 13:42:10 +00:00
|
|
|
|
|
|
|
track.bestUpload()?.let { upload ->
|
2020-06-21 12:16:30 +00:00
|
|
|
maybeNormalizeUrl(upload.listen_url)?.let { url ->
|
2021-07-21 07:11:07 +00:00
|
|
|
track.cached = FFA.get().exoCache.isCached(url, 0, upload.duration * 1000L)
|
2020-06-21 12:16:30 +00:00
|
|
|
}
|
2020-06-20 13:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 17:50:02 +00:00
|
|
|
track
|
|
|
|
}
|
2019-08-19 14:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fun addFavorite(id: Int) {
|
2021-07-23 12:10:13 +00:00
|
|
|
context?.let {
|
|
|
|
val body = mapOf("track" to id)
|
2019-08-19 14:50:33 +00:00
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
authorize(context)
|
|
|
|
header("Authorization", "Bearer ${OAuth.state().accessToken}")
|
|
|
|
}
|
2019-11-25 22:14:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
scope.launch(IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
2020-07-10 13:08:08 +00:00
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
favoritedRepository.update(context, scope)
|
|
|
|
}
|
2019-08-19 14:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteFavorite(id: Int) {
|
2021-07-23 12:10:13 +00:00
|
|
|
context?.let {
|
|
|
|
val body = mapOf("track" to id)
|
2019-08-19 14:50:33 +00:00
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/remove/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
authorize(context)
|
|
|
|
request.header("Authorization", "Bearer ${OAuth.state().accessToken}")
|
|
|
|
}
|
2019-11-25 22:14:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
scope.launch(IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
2020-07-10 13:08:08 +00:00
|
|
|
|
2021-07-23 12:10:13 +00:00
|
|
|
favoritedRepository.update(context, scope)
|
|
|
|
}
|
2019-08-19 14:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 18:21:18 +00:00
|
|
|
|
|
|
|
class FavoritedRepository(override val context: Context?) : Repository<Int, FavoritedCache>() {
|
|
|
|
override val cacheId = "favorited"
|
2021-07-23 12:10:13 +00:00
|
|
|
override val upstream = HttpUpstream<Int, OtterResponse<Int>>(
|
|
|
|
context,
|
|
|
|
HttpUpstream.Behavior.Single,
|
|
|
|
"/api/v1/favorites/tracks/all/?playable=true",
|
|
|
|
object : TypeToken<FavoritedResponse>() {}.type
|
|
|
|
)
|
2019-10-23 18:21:18 +00:00
|
|
|
|
|
|
|
override fun cache(data: List<Int>) = FavoritedCache(data)
|
2021-07-23 12:10:13 +00:00
|
|
|
override fun uncache(reader: BufferedReader) =
|
|
|
|
gsonDeserializerOf(FavoritedCache::class.java).deserialize(reader)
|
2020-07-11 14:56:09 +00:00
|
|
|
|
|
|
|
fun update(context: Context?, scope: CoroutineScope) {
|
|
|
|
fetch(Origin.Network.origin).untilNetwork(scope, IO) { favorites, _, _, _ ->
|
|
|
|
Cache.set(context, cacheId, Gson().toJson(cache(favorites)).toByteArray())
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 18:21:18 +00:00
|
|
|
}
|