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
|
2021-08-22 09:48:33 +02:00
|
|
|
import audio.funkwhale.ffa.model.Album
|
|
|
|
import audio.funkwhale.ffa.model.AlbumsCache
|
|
|
|
import audio.funkwhale.ffa.model.AlbumsResponse
|
2021-08-09 06:50:46 +02:00
|
|
|
import audio.funkwhale.ffa.utils.OAuth
|
2022-06-16 15:08:29 +02:00
|
|
|
import audio.funkwhale.ffa.utils.gsonDeserializerOf
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.google.gson.reflect.TypeToken
|
2021-08-09 06:50:46 +02:00
|
|
|
import org.koin.java.KoinJavaComponent.inject
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
class AlbumsRepository(override val context: Context?, artistId: Int? = null) :
|
|
|
|
Repository<Album, AlbumsCache>() {
|
|
|
|
|
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-08-19 16:50:33 +02:00
|
|
|
override val cacheId: String by lazy {
|
|
|
|
if (artistId == null) "albums"
|
|
|
|
else "albums-artist-$artistId"
|
|
|
|
}
|
|
|
|
|
|
|
|
override val upstream: Upstream<Album> by lazy {
|
|
|
|
val url =
|
2020-07-08 13:19:03 +02:00
|
|
|
if (artistId == null) "/api/v1/albums/?playable=true&ordering=title"
|
|
|
|
else "/api/v1/albums/?playable=true&artist=$artistId&ordering=release_date"
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2021-07-23 14:10:13 +02:00
|
|
|
HttpUpstream(
|
|
|
|
context!!,
|
2019-08-19 16:50:33 +02:00
|
|
|
HttpUpstream.Behavior.Progressive,
|
|
|
|
url,
|
2021-07-30 10:57:49 +02:00
|
|
|
object : TypeToken<AlbumsResponse>() {}.type,
|
|
|
|
oAuth
|
2019-08-19 16:50:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun cache(data: List<Album>) = AlbumsCache(data)
|
2022-06-12 14:48:32 +02:00
|
|
|
override fun uncache(json: String) =
|
|
|
|
gsonDeserializerOf(AlbumsCache::class.java).deserialize(json)
|
2021-07-02 13:55:49 +02:00
|
|
|
}
|