2021-07-12 10:14:26 +02:00
|
|
|
package audio.funkwhale.ffa.repositories
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
2022-06-16 15:08:29 +02:00
|
|
|
import audio.funkwhale.ffa.model.*
|
|
|
|
import audio.funkwhale.ffa.utils.*
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.kittinunf.fuel.Fuel
|
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
|
2021-08-09 06:50:46 +02:00
|
|
|
import com.google.android.exoplayer2.offline.DownloadManager
|
|
|
|
import com.google.android.exoplayer2.upstream.cache.Cache
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.google.gson.Gson
|
|
|
|
import com.google.gson.reflect.TypeToken
|
2020-07-11 16:56:09 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2019-11-21 22:13:59 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2020-06-13 19:50:02 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.core.qualifier.named
|
|
|
|
import org.koin.java.KoinJavaComponent.inject
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
class FavoritesRepository(override val context: Context?) : Repository<Track, TracksCache>() {
|
2021-07-23 14:10:13 +02:00
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
private val exoDownloadManager: DownloadManager by inject(DownloadManager::class.java)
|
|
|
|
private val exoCache: Cache by inject(Cache::class.java, named("exoCache"))
|
|
|
|
private val oAuth: OAuth by inject(OAuth::class.java)
|
2021-07-30 10:57:49 +02:00
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
override val cacheId = "favorites.v2"
|
2021-07-23 14:10:13 +02:00
|
|
|
|
2021-08-22 09:48:33 +02:00
|
|
|
override val upstream = HttpUpstream<Track, FFAResponse<Track>>(
|
2021-07-23 14:10:13 +02:00
|
|
|
context!!,
|
|
|
|
HttpUpstream.Behavior.AtOnce,
|
|
|
|
"/api/v1/tracks/?favorites=true&playable=true&ordering=title",
|
2021-07-30 10:57:49 +02:00
|
|
|
object : TypeToken<TracksResponse>() {}.type,
|
|
|
|
oAuth
|
2021-07-23 14:10:13 +02:00
|
|
|
)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
override fun cache(data: List<Track>) = TracksCache(data)
|
2022-06-12 14:48:32 +02:00
|
|
|
override fun uncache(json: String) =
|
2022-07-14 13:12:30 +02:00
|
|
|
gsonDeserializerOf(TracksCache::class.java).deserialize(json.reader())
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
private val favoritedRepository = FavoritedRepository(context!!)
|
2020-07-10 15:08:08 +02:00
|
|
|
|
2020-06-13 19:50:02 +02:00
|
|
|
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
2021-08-09 06:50:46 +02:00
|
|
|
val downloaded = TracksRepository.getDownloadedIds(exoDownloadManager) ?: listOf()
|
2020-06-13 19:50:02 +02:00
|
|
|
|
|
|
|
data.map { track ->
|
|
|
|
track.favorite = true
|
|
|
|
track.downloaded = downloaded.contains(track.id)
|
2020-06-20 15:42:10 +02:00
|
|
|
|
|
|
|
track.bestUpload()?.let { upload ->
|
2020-06-21 14:16:30 +02:00
|
|
|
maybeNormalizeUrl(upload.listen_url)?.let { url ->
|
2021-08-09 06:50:46 +02:00
|
|
|
track.cached = exoCache.isCached(url, 0, upload.duration * 1000L)
|
2020-06-21 14:16:30 +02:00
|
|
|
}
|
2020-06-20 15:42:10 +02:00
|
|
|
}
|
|
|
|
|
2020-06-13 19:50:02 +02:00
|
|
|
track
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun addFavorite(id: Int) {
|
2021-07-23 14:10:13 +02:00
|
|
|
context?.let {
|
|
|
|
val body = mapOf("track" to id)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
2021-08-09 06:50:46 +02:00
|
|
|
authorize(context, oAuth)
|
2021-07-30 10:57:49 +02:00
|
|
|
header("Authorization", "Bearer ${oAuth.state().accessToken}")
|
2021-07-23 14:10:13 +02:00
|
|
|
}
|
2019-11-25 23:14:16 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
scope.launch(IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
2020-07-10 15:08:08 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
favoritedRepository.update(context, scope)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun deleteFavorite(id: Int) {
|
2021-07-23 14:10:13 +02:00
|
|
|
context?.let {
|
|
|
|
val body = mapOf("track" to id)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/remove/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
2021-08-09 06:50:46 +02:00
|
|
|
authorize(context, oAuth)
|
2021-07-30 10:57:49 +02:00
|
|
|
request.header("Authorization", "Bearer ${oAuth.state().accessToken}")
|
2021-07-23 14:10:13 +02:00
|
|
|
}
|
2019-11-25 23:14:16 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
scope.launch(IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
2020-07-10 15:08:08 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
favoritedRepository.update(context, scope)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 20:21:18 +02:00
|
|
|
|
|
|
|
class FavoritedRepository(override val context: Context?) : Repository<Int, FavoritedCache>() {
|
2021-07-30 10:57:49 +02:00
|
|
|
|
2021-08-09 06:50:46 +02:00
|
|
|
private val oAuth: OAuth by inject(OAuth::class.java)
|
2021-07-30 10:57:49 +02:00
|
|
|
|
2019-10-23 20:21:18 +02:00
|
|
|
override val cacheId = "favorited"
|
2021-08-22 09:48:33 +02:00
|
|
|
override val upstream = HttpUpstream<Int, FFAResponse<Int>>(
|
2021-07-23 14:10:13 +02:00
|
|
|
context,
|
|
|
|
HttpUpstream.Behavior.Single,
|
|
|
|
"/api/v1/favorites/tracks/all/?playable=true",
|
2021-07-30 10:57:49 +02:00
|
|
|
object : TypeToken<FavoritedResponse>() {}.type,
|
|
|
|
oAuth
|
2021-07-23 14:10:13 +02:00
|
|
|
)
|
2019-10-23 20:21:18 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Int>) = FavoritedCache(data)
|
2022-06-12 14:48:32 +02:00
|
|
|
override fun uncache(json: String) =
|
2022-07-14 13:12:30 +02:00
|
|
|
gsonDeserializerOf(FavoritedCache::class.java).deserialize(json.reader())
|
2020-07-11 16:56:09 +02:00
|
|
|
|
|
|
|
fun update(context: Context?, scope: CoroutineScope) {
|
|
|
|
fetch(Origin.Network.origin).untilNetwork(scope, IO) { favorites, _, _, _ ->
|
2022-06-12 14:48:32 +02:00
|
|
|
FFACache.set(context, cacheId, Gson().toJson(cache(favorites)).toString())
|
2020-07-11 16:56:09 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-23 20:21:18 +02:00
|
|
|
}
|