2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.repositories
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-09-04 13:26:08 +02:00
|
|
|
import com.github.apognu.otter.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
|
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"
|
|
|
|
override val upstream = HttpUpstream<Playlist, OtterResponse<Playlist>>(HttpUpstream.Behavior.AtOnce, "/api/v1/playlists/?ordering=name", object : TypeToken<PlaylistsResponse>() {}.type)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fun add(id: Int, track: Track) {
|
|
|
|
val body = PlaylistAdd(listOf(track.id), false)
|
|
|
|
|
|
|
|
val request = Fuel.post(mustNormalizeUrl("/api/v1/playlists/${id}/add/")).apply {
|
|
|
|
if (!Settings.isAnonymous()) {
|
|
|
|
header("Authorization", "Bearer ${Settings.getAccessToken()}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scope.launch(Dispatchers.IO) {
|
|
|
|
request
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.body(Gson().toJson(body))
|
|
|
|
.awaitByteArrayResponseResult()
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|