funkwhale-app-android/app/src/main/java/audio/funkwhale/ffa/repositories/PlaylistsRepository.kt

141 lines
4.4 KiB
Kotlin
Raw Normal View History

package audio.funkwhale.ffa.repositories
2019-08-19 16:50:33 +02:00
import android.content.Context
import audio.funkwhale.ffa.utils.*
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
import com.google.gson.Gson
2019-08-19 16:50:33 +02:00
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.java.KoinJavaComponent.inject
2019-08-19 16:50:33 +02:00
import java.io.BufferedReader
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>() {
2021-07-23 14:10:13 +02:00
2019-08-19 16:50:33 +02:00
override val cacheId = "tracks-playlists"
2021-07-23 14:10:13 +02:00
private val oAuth: OAuth by inject(OAuth::class.java)
2021-07-23 14:10:13 +02:00
override val upstream = HttpUpstream<Playlist, OtterResponse<Playlist>>(
context!!,
HttpUpstream.Behavior.Progressive,
"/api/v1/playlists/?playable=true&ordering=name",
2021-07-30 10:57:49 +02:00
object : TypeToken<PlaylistsResponse>() {}.type,
oAuth
2021-07-23 14:10:13 +02:00
)
2019-08-19 16:50:33 +02:00
override fun cache(data: List<Playlist>) = PlaylistsCache(data)
2021-07-23 14:10:13 +02:00
override fun uncache(reader: BufferedReader) =
gsonDeserializerOf(PlaylistsCache::class.java).deserialize(reader)
}
2021-07-23 14:10:13 +02:00
class ManagementPlaylistsRepository(override val context: Context?) :
Repository<Playlist, PlaylistsCache>() {
private val oAuth: OAuth by inject(OAuth::class.java)
2021-07-30 10:57:49 +02:00
override val cacheId = "tracks-playlists-management"
2021-07-23 14:10:13 +02:00
override val upstream = HttpUpstream<Playlist, OtterResponse<Playlist>>(
context,
HttpUpstream.Behavior.AtOnce,
"/api/v1/playlists/?scope=me&ordering=name",
2021-07-30 10:57:49 +02:00
object : TypeToken<PlaylistsResponse>() {}.type,
oAuth
2021-07-23 14:10:13 +02:00
)
override fun cache(data: List<Playlist>) = PlaylistsCache(data)
2021-07-23 14:10:13 +02:00
override fun uncache(reader: BufferedReader) =
gsonDeserializerOf(PlaylistsCache::class.java).deserialize(reader)
suspend fun new(name: String): Int? {
2021-07-23 14:10:13 +02:00
context?.let {
2021-07-23 14:10:13 +02:00
val body = mapOf("name" to name, "privacy_level" to "me")
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/")).apply {
if (!Settings.isAnonymous()) {
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
}
}
2021-07-23 14:10:13 +02:00
val (_, response, result) = request
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitObjectResponseResult(gsonDeserializerOf(Playlist::class.java))
2021-07-23 14:10:13 +02:00
if (response.statusCode != 201) return null
2021-07-23 14:10:13 +02:00
return result.get().id
}
throw IllegalStateException("Illegal state: context is null")
}
fun add(id: Int, tracks: List<Track>) {
2021-07-23 14:10:13 +02:00
context?.let {
val body = PlaylistAdd(tracks.map { it.id }, false)
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/add/")).apply {
if (!Settings.isAnonymous()) {
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
}
}
2021-07-23 14:10:13 +02:00
scope.launch(Dispatchers.IO) {
request
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
}
}
2021-07-23 14:10:13 +02:00
throw IllegalStateException("Illegal state: context is null")
}
suspend fun remove(id: Int, track: Track, index: Int) {
context?.let {
val body = mapOf("index" to index)
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/remove/")).apply {
if (!Settings.isAnonymous()) {
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
}
}
request
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
}
2021-07-23 14:10:13 +02:00
throw IllegalStateException("Illegal state: context is null")
}
fun move(id: Int, from: Int, to: Int) {
2021-07-23 14:10:13 +02:00
context?.let {
val body = mapOf("from" to from, "to" to to)
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$id/move/")).apply {
if (!Settings.isAnonymous()) {
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
}
}
2021-07-23 14:10:13 +02:00
scope.launch(Dispatchers.IO) {
request
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
}
}
2021-07-23 14:10:13 +02:00
throw IllegalStateException("Illegal state: context is null")
}
2021-06-26 13:36:32 +02:00
}