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

109 lines
3.2 KiB
Kotlin
Raw Normal View History

2019-08-19 16:50:33 +02:00
package com.github.apognu.otter.repositories
import android.content.Context
import com.github.apognu.otter.Otter
2020-07-13 23:32:42 +02:00
import com.github.apognu.otter.models.api.Favorited
import com.github.apognu.otter.models.api.FunkwhaleTrack
import com.github.apognu.otter.models.dao.FavoriteEntity
import com.github.apognu.otter.utils.Settings
import com.github.apognu.otter.utils.mustNormalizeUrl
2019-08-19 16:50:33 +02:00
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers.IO
2020-07-13 23:32:42 +02:00
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
2019-08-19 16:50:33 +02:00
2020-07-13 23:32:42 +02:00
class FavoritesRepository(override val context: Context?) : Repository<FunkwhaleTrack>() {
override val upstream =
HttpUpstream(HttpUpstream.Behavior.AtOnce, "/api/v1/tracks/?favorites=true&playable=true&ordering=title", FunkwhaleTrack.serializer())
2019-08-19 16:50:33 +02:00
2020-07-13 23:32:42 +02:00
val favoritedRepository = FavoritedRepository(context)
2019-08-19 16:50:33 +02:00
2020-07-13 23:32:42 +02:00
override fun onDataFetched(data: List<FunkwhaleTrack>): List<FunkwhaleTrack> = runBlocking {
data.forEach {
Otter.get().database.tracks().insertWithAssocs(it)
Otter.get().database.favorites().insert(FavoriteEntity(it.id))
}
2020-07-13 23:32:42 +02:00
/* val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
data.map { track ->
track.favorite = true
track.downloaded = downloaded.contains(track.id)
track.bestUpload()?.let { upload ->
maybeNormalizeUrl(upload.listen_url)?.let { url ->
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
}
}
track
2020-07-13 23:32:42 +02:00
} */
data
2019-08-19 16:50:33 +02:00
}
2020-07-13 23:32:42 +02:00
fun addFavorite(id: Int) = scope.launch(IO) {
Otter.get().database.favorites().add(id)
2019-08-19 16:50:33 +02:00
val body = mapOf("track" to id)
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/")).apply {
if (!Settings.isAnonymous()) {
header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
scope.launch(IO) {
request
2019-08-19 16:50:33 +02:00
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
2020-07-13 23:32:42 +02:00
favoritedRepository.update()
2019-08-19 16:50:33 +02:00
}
}
2020-07-13 23:32:42 +02:00
fun deleteFavorite(id: Int) = scope.launch(IO) {
Otter.get().database.favorites().remove(id)
2019-08-19 16:50:33 +02:00
val body = mapOf("track" to id)
val request = Fuel.post(mustNormalizeUrl("/api/v1/favorites/tracks/remove/")).apply {
if (!Settings.isAnonymous()) {
request.header("Authorization", "Bearer ${Settings.getAccessToken()}")
}
}
scope.launch(IO) {
request
2019-08-19 16:50:33 +02:00
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
2020-07-13 23:32:42 +02:00
favoritedRepository.update()
2019-08-19 16:50:33 +02:00
}
}
}
2020-07-13 23:32:42 +02:00
class FavoritedRepository(override val context: Context?) : Repository<Favorited>() {
override val upstream =
HttpUpstream(HttpUpstream.Behavior.Single, "/api/v1/favorites/tracks/all/?playable=true", Favorited.serializer())
2020-07-13 23:32:42 +02:00
override fun onDataFetched(data: List<Favorited>): List<Favorited> {
scope.launch(IO) {
data.forEach {
Otter.get().database.favorites().insert(FavoriteEntity(it.track))
}
}
2020-07-13 23:32:42 +02:00
return super.onDataFetched(data)
}
fun update() = scope.launch(IO) {
fetch().collect()
}
}