2021-07-02 13:55:49 +02:00
|
|
|
package org.funkwhale.ffa.repositories
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
2021-07-02 13:55:49 +02:00
|
|
|
import org.funkwhale.ffa.utils.*
|
2020-09-04 13:26:08 +02:00
|
|
|
import com.github.kittinunf.fuel.Fuel
|
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
|
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
2020-09-04 13:26:08 +02:00
|
|
|
import com.google.gson.Gson
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.google.gson.reflect.TypeToken
|
2020-09-04 13:26:08 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2019-08-19 16:50:33 +02:00
|
|
|
import java.io.BufferedReader
|
|
|
|
|
2020-09-04 13:26:08 +02:00
|
|
|
data class PlaylistAdd(val tracks: List<Int>, val allow_duplicates: Boolean)
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
class PlaylistsRepository(override val context: Context?) : Repository<Playlist, PlaylistsCache>() {
|
|
|
|
override val cacheId = "tracks-playlists"
|
2020-07-10 20:40:18 +02:00
|
|
|
override val upstream = HttpUpstream<Playlist, OtterResponse<Playlist>>(HttpUpstream.Behavior.Progressive, "/api/v1/playlists/?playable=true&ordering=name", object : TypeToken<PlaylistsResponse>() {}.type)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Playlist>) = PlaylistsCache(data)
|
|
|
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(PlaylistsCache::class.java).deserialize(reader)
|
2020-09-04 13:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ManagementPlaylistsRepository(override val context: Context?) : Repository<Playlist, PlaylistsCache>() {
|
|
|
|
override val cacheId = "tracks-playlists-management"
|
2020-09-04 19:56:09 +02:00
|
|
|
override val upstream = HttpUpstream<Playlist, OtterResponse<Playlist>>(HttpUpstream.Behavior.AtOnce, "/api/v1/playlists/?scope=me&ordering=name", object : TypeToken<PlaylistsResponse>() {}.type)
|
2020-09-04 13:26:08 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Playlist>) = PlaylistsCache(data)
|
|
|
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(PlaylistsCache::class.java).deserialize(reader)
|
|
|
|
|
|
|
|
suspend fun new(name: String): Int? {
|
|
|
|
val body = mapOf("name" to name, "privacy_level" to "me")
|
|
|
|
|
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val (_, response, result) = request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitObjectResponseResult(gsonDeserializerOf(Playlist::class.java))
|
|
|
|
|
|
|
|
if (response.statusCode != 201) return null
|
|
|
|
|
|
|
|
return result.get().id
|
|
|
|
}
|
|
|
|
|
2020-09-26 17:58:06 +02:00
|
|
|
fun add(id: Int, tracks: List<Track>) {
|
|
|
|
val body = PlaylistAdd(tracks.map { it.id }, false)
|
2020-09-04 13:26:08 +02:00
|
|
|
|
2021-06-26 13:36:32 +02:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/add/")).apply {
|
2020-09-04 13:26:08 +02:00
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.launch(Dispatchers.IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
|
|
|
}
|
|
|
|
}
|
2020-09-04 19:56:09 +02:00
|
|
|
|
|
|
|
suspend fun remove(id: Int, track: Track, index: Int) {
|
|
|
|
val body = mapOf("index" to index)
|
|
|
|
|
2021-06-26 13:36:32 +02:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/remove/")).apply {
|
2020-09-04 19:56:09 +02:00
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
|
|
|
}
|
2020-09-05 00:56:39 +02:00
|
|
|
|
|
|
|
fun move(id: Int, from: Int, to: Int) {
|
|
|
|
val body = mapOf("from" to from, "to" to to)
|
|
|
|
|
2021-06-26 13:36:32 +02:00
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/move/")).apply {
|
2020-09-05 00:56:39 +02:00
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.launch(Dispatchers.IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
|
|
|
}
|
|
|
|
}
|
2021-06-26 13:36:32 +02:00
|
|
|
}
|