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

143 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.model.*
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
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
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)
override val upstream = HttpUpstream<Playlist, FFAResponse<Playlist>>(
2021-07-23 14:10:13 +02:00
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)
override fun uncache(json: String) =
gsonDeserializerOf(PlaylistsCache::class.java).deserialize(json.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, FFAResponse<Playlist>>(
2021-07-23 14:10:13 +02:00
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)
override fun uncache(json: String) =
gsonDeserializerOf(PlaylistsCache::class.java).deserialize(json.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>) {
if (context != null) {
2021-07-23 14:10:13 +02:00
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()
}
} else {
throw IllegalStateException("Illegal state: context is null")
}
2021-07-23 14:10:13 +02:00
}
suspend fun remove(albumId: Int, index: Int) {
if (context != null) {
2021-07-23 14:10:13 +02:00
val body = mapOf("index" to index)
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/$albumId/remove/")).apply {
2021-07-23 14:10:13 +02:00
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()
} else {
throw IllegalStateException("Illegal state: context is null")
2022-03-04 09:30:03 +01:00
}
}
fun move(id: Int, from: Int, to: Int) {
if (context != null) {
2021-07-23 14:10:13 +02:00
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()
}
} else {
throw IllegalStateException("Illegal state: context is null")
}
}
2021-06-26 13:36:32 +02:00
}