Otter-App-Android-Funkwhale/app/src/main/java/com/github/apognu/otter/repositories/AlbumsRepository.kt

28 lines
882 B
Kotlin
Raw Normal View History

2019-08-19 16:50:33 +02:00
package com.github.apognu.otter.repositories
import android.content.Context
2020-07-13 23:32:42 +02:00
import com.github.apognu.otter.Otter
import com.github.apognu.otter.models.api.FunkwhaleAlbum
import com.github.apognu.otter.models.dao.toDao
2019-08-19 16:50:33 +02:00
2020-07-13 23:32:42 +02:00
class AlbumsRepository(override val context: Context?, artistId: Int? = null) : Repository<FunkwhaleAlbum>() {
override val upstream: Upstream<FunkwhaleAlbum> by lazy {
2019-08-19 16:50:33 +02:00
val url =
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
2020-07-13 23:32:42 +02:00
HttpUpstream(
2019-08-19 16:50:33 +02:00
HttpUpstream.Behavior.Progressive,
url,
2020-07-13 23:32:42 +02:00
FunkwhaleAlbum.serializer()
2019-08-19 16:50:33 +02:00
)
}
2020-07-13 23:32:42 +02:00
override fun onDataFetched(data: List<FunkwhaleAlbum>): List<FunkwhaleAlbum> {
data.forEach {
Otter.get().database.albums().insert(it.toDao())
}
return super.onDataFetched(data)
}
2019-08-19 16:50:33 +02:00
}