Fix an issue where always retrieving favorites from the network could be really costly and introduce stack overflows (#60).

This commit is contained in:
Antoine POPINEAU 2020-07-10 15:08:08 +02:00
parent 4ecb607f45
commit b14b703f05
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
5 changed files with 17 additions and 4 deletions

View File

@ -42,6 +42,8 @@ import kotlinx.coroutines.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@ -76,6 +78,8 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
favoriteCheckRepository.fetch().map { Cache.set(this, favoriteCheckRepository.cacheId, Gson().toJson(favoriteCheckRepository.cache(it.data)).toByteArray()) }
startService(Intent(this, PlayerService::class.java))
DownloadService.start(this, PinService::class.java)

View File

@ -121,7 +121,7 @@ class RadioPlayer(val context: Context, val scope: CoroutineScope) {
.authorize()
.awaitObjectResult(gsonDeserializerOf(Track::class.java))
val favorites = favoritedRepository.fetch(Repository.Origin.Network.origin)
val favorites = favoritedRepository.fetch(Repository.Origin.Cache.origin)
.map { it.data }
.toList()
.flatten()

View File

@ -9,6 +9,7 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
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 uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader)
private val favoritedRepository = FavoritedRepository(context)
override fun onDataFetched(data: List<Track>): List<Track> = runBlocking {
val downloaded = TracksRepository.getDownloadedIds() ?: listOf()
@ -51,6 +54,8 @@ class FavoritesRepository(override val context: Context?) : Repository<Track, Tr
.header("Content-Type", "application/json")
.body(Gson().toJson(body))
.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")
.body(Gson().toJson(body))
.awaitByteArrayResponseResult()
favoritedRepository.fetch().map { Cache.set(context, favoritedRepository.cacheId, Gson().toJson(favoritedRepository.cache(it.data)).toByteArray()) }
}
}
}

View File

@ -42,18 +42,20 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
val data = response.getData()
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))
else -> {
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) }
}
}
},
{ error ->
when (error.exception) {
is RefreshError -> EventBus.send(Event.LogOut)
else -> emit(Repository.Response(Repository.Origin.Network, listOf(), page,false))
else -> emit(Repository.Response(Repository.Origin.Network, listOf(), page, false))
}
}
)

View File

@ -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 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 }
.toList()
.flatten()