mirror of
https://github.com/apognu/otter
synced 2025-02-18 19:50:35 +01:00
Fix an issue where always retrieving favorites from the network could be really costly and introduce stack overflows (#60).
This commit is contained in:
parent
4ecb607f45
commit
b14b703f05
@ -42,6 +42,8 @@ import kotlinx.coroutines.Dispatchers.Default
|
|||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Dispatchers.Main
|
import kotlinx.coroutines.Dispatchers.Main
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
import kotlinx.coroutines.flow.toList
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
@ -76,6 +78,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|
||||||
|
favoriteCheckRepository.fetch().map { Cache.set(this, favoriteCheckRepository.cacheId, Gson().toJson(favoriteCheckRepository.cache(it.data)).toByteArray()) }
|
||||||
|
|
||||||
startService(Intent(this, PlayerService::class.java))
|
startService(Intent(this, PlayerService::class.java))
|
||||||
DownloadService.start(this, PinService::class.java)
|
DownloadService.start(this, PinService::class.java)
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ class RadioPlayer(val context: Context, val scope: CoroutineScope) {
|
|||||||
.authorize()
|
.authorize()
|
||||||
.awaitObjectResult(gsonDeserializerOf(Track::class.java))
|
.awaitObjectResult(gsonDeserializerOf(Track::class.java))
|
||||||
|
|
||||||
val favorites = favoritedRepository.fetch(Repository.Origin.Network.origin)
|
val favorites = favoritedRepository.fetch(Repository.Origin.Cache.origin)
|
||||||
.map { it.data }
|
.map { it.data }
|
||||||
.toList()
|
.toList()
|
||||||
.flatten()
|
.flatten()
|
||||||
|
@ -9,6 +9,7 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
|||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
@ -20,6 +21,8 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
|
|||||||
override fun cache(data: List<Track>) = TracksCache(data)
|
override fun cache(data: List<Track>) = TracksCache(data)
|
||||||
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
||||||
|
|
||||||
|
private val favoritedRepository = FavoritedRepository(context)
|
||||||
|
|
||||||
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
||||||
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
|
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
|
||||||
|
|
||||||
@ -51,6 +54,8 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
|
|||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(Gson().toJson(body))
|
.body(Gson().toJson(body))
|
||||||
.awaitByteArrayResponseResult()
|
.awaitByteArrayResponseResult()
|
||||||
|
|
||||||
|
favoritedRepository.fetch().map { Cache.set(context, favoritedRepository.cacheId, Gson().toJson(favoritedRepository.cache(it.data)).toByteArray()) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,6 +73,8 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
|
|||||||
.header("Content-Type", "application/json")
|
.header("Content-Type", "application/json")
|
||||||
.body(Gson().toJson(body))
|
.body(Gson().toJson(body))
|
||||||
.awaitByteArrayResponseResult()
|
.awaitByteArrayResponseResult()
|
||||||
|
|
||||||
|
favoritedRepository.fetch().map { Cache.set(context, favoritedRepository.cacheId, Gson().toJson(favoritedRepository.cache(it.data)).toByteArray()) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,11 +42,13 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
|
|||||||
val data = response.getData()
|
val data = response.getData()
|
||||||
|
|
||||||
when (behavior) {
|
when (behavior) {
|
||||||
|
Behavior.Single -> emit(Repository.Response(Repository.Origin.Network, data, page, false))
|
||||||
Behavior.Progressive -> emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
Behavior.Progressive -> emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
emit(Repository.Response(Repository.Origin.Network, data, page, response.next != null))
|
||||||
|
|
||||||
fetch(size + data.size).collect { emit(it) }
|
if (response.next != null) fetch(size + data.size).collect { emit(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -18,7 +18,7 @@ class TracksSearchRepository(override val context: Context?, query: String) : Re
|
|||||||
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
|
||||||
|
|
||||||
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
|
||||||
val favorites = FavoritedRepository(context).fetch(Origin.Network.origin)
|
val favorites = FavoritedRepository(context).fetch(Origin.Cache.origin)
|
||||||
.map { it.data }
|
.map { it.data }
|
||||||
.toList()
|
.toList()
|
||||||
.flatten()
|
.flatten()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user