From b14b703f0581d7a3a7d14e3f7af941225c838088 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Fri, 10 Jul 2020 15:08:08 +0200 Subject: [PATCH] Fix an issue where always retrieving favorites from the network could be really costly and introduce stack overflows (#60). --- .../com/github/apognu/otter/activities/MainActivity.kt | 4 ++++ .../java/com/github/apognu/otter/playback/RadioPlayer.kt | 2 +- .../apognu/otter/repositories/FavoritesRepository.kt | 7 +++++++ .../com/github/apognu/otter/repositories/HttpUpstream.kt | 6 ++++-- .../github/apognu/otter/repositories/SearchRepository.kt | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt index 1c2cfa2..f885583 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt @@ -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) diff --git a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt index ee549da..a66536a 100644 --- a/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt +++ b/app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt @@ -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() diff --git a/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt index d904754..6dc8e82 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt @@ -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) = TracksCache(data) override fun uncache(reader: BufferedReader) = gsonDeserializerOf(TracksCache::class.java).deserialize(reader) + private val favoritedRepository = FavoritedRepository(context) + override fun onDataFetched(data: List): List = runBlocking { val downloaded = TracksRepository.getDownloadedIds() ?: listOf() @@ -51,6 +54,8 @@ class FavoritesRepository(override val context: Context?) : Repository>(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)) } } ) diff --git a/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt b/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt index 14c4538..8b57be2 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt @@ -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): List = runBlocking { - val favorites = FavoritedRepository(context).fetch(Origin.Network.origin) + val favorites = FavoritedRepository(context).fetch(Origin.Cache.origin) .map { it.data } .toList() .flatten()