Browse Source

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

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
b14b703f05
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 4
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  2. 2
      app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt
  3. 7
      app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt
  4. 6
      app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt
  5. 2
      app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt

4
app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt

@ -42,6 +42,8 @@ import kotlinx.coroutines.Dispatchers.Default @@ -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() { @@ -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)

2
app/src/main/java/com/github/apognu/otter/playback/RadioPlayer.kt

@ -121,7 +121,7 @@ class RadioPlayer(val context: Context, val scope: CoroutineScope) { @@ -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()

7
app/src/main/java/com/github/apognu/otter/repositories/FavoritesRepository.kt

@ -9,6 +9,7 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf @@ -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 @@ -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 @@ -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 @@ -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()) }
}
}
}

6
app/src/main/java/com/github/apognu/otter/repositories/HttpUpstream.kt

@ -42,18 +42,20 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr @@ -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))
}
}
)

2
app/src/main/java/com/github/apognu/otter/repositories/SearchRepository.kt

@ -18,7 +18,7 @@ class TracksSearchRepository(override val context: Context?, query: String) : Re @@ -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()

Loading…
Cancel
Save