2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.repositories
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-06-22 21:48:31 +02:00
|
|
|
import com.github.apognu.otter.Otter
|
2020-05-29 13:19:28 +02:00
|
|
|
import com.github.apognu.otter.utils.*
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
|
|
|
import com.google.gson.reflect.TypeToken
|
2019-10-31 16:17:37 +01:00
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
import kotlinx.coroutines.flow.toList
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import java.io.BufferedReader
|
|
|
|
|
2020-05-29 13:19:28 +02:00
|
|
|
class TracksSearchRepository(override val context: Context?, query: String) : Repository<Track, TracksCache>() {
|
2019-08-19 16:50:33 +02:00
|
|
|
override val cacheId: String? = null
|
2020-07-10 20:40:18 +02:00
|
|
|
override val upstream = HttpUpstream<Track, OtterResponse<Track>>(HttpUpstream.Behavior.AtOnce, "/api/v1/tracks/?playable=true&q=$query", object : TypeToken<TracksResponse>() {}.type)
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Track>) = TracksCache(data)
|
|
|
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
|
|
|
|
|
|
|
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
2020-07-10 15:08:08 +02:00
|
|
|
val favorites = FavoritedRepository(context).fetch(Origin.Cache.origin)
|
2019-10-31 16:17:37 +01:00
|
|
|
.map { it.data }
|
|
|
|
.toList()
|
|
|
|
.flatten()
|
2019-08-19 16:50:33 +02:00
|
|
|
|
2020-06-22 21:48:31 +02:00
|
|
|
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
data.map { track ->
|
2019-10-23 20:21:18 +02:00
|
|
|
track.favorite = favorites.contains(track.id)
|
2020-06-22 21:48:31 +02:00
|
|
|
track.downloaded = downloaded.contains(track.id)
|
|
|
|
|
|
|
|
track.bestUpload()?.let { upload ->
|
|
|
|
val url = mustNormalizeUrl(upload.listen_url)
|
|
|
|
|
|
|
|
track.cached = Otter.get().exoCache.isCached(url, 0, upload.duration * 1000L)
|
|
|
|
}
|
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
track
|
|
|
|
}
|
|
|
|
}
|
2020-05-29 13:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ArtistsSearchRepository(override val context: Context?, query: String) : Repository<Artist, ArtistsCache>() {
|
|
|
|
override val cacheId: String? = null
|
2020-07-10 20:40:18 +02:00
|
|
|
override val upstream = HttpUpstream<Artist, OtterResponse<Artist>>(HttpUpstream.Behavior.AtOnce, "/api/v1/artists/?playable=true&q=$query", object : TypeToken<ArtistsResponse>() {}.type)
|
2020-05-29 13:19:28 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Artist>) = ArtistsCache(data)
|
|
|
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(ArtistsCache::class.java).deserialize(reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
class AlbumsSearchRepository(override val context: Context?, query: String) : Repository<Album, AlbumsCache>() {
|
|
|
|
override val cacheId: String? = null
|
2020-07-10 20:40:18 +02:00
|
|
|
override val upstream = HttpUpstream<Album, OtterResponse<Album>>(HttpUpstream.Behavior.AtOnce, "/api/v1/albums/?playable=true&q=$query", object : TypeToken<AlbumsResponse>() {}.type)
|
2020-05-29 13:19:28 +02:00
|
|
|
|
|
|
|
override fun cache(data: List<Album>) = AlbumsCache(data)
|
|
|
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(AlbumsCache::class.java).deserialize(reader)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|