Browse Source

Load data from network on resume if cached data is empty.

housekeeping/remove-warnings
Antoine POPINEAU 4 years ago
parent
commit
37d5c7b7be
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 9
      app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt
  2. 4
      app/src/main/java/com/github/apognu/otter/repositories/Repository.kt

9
app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt

@ -10,10 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager @@ -10,10 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.github.apognu.otter.repositories.HttpUpstream
import com.github.apognu.otter.repositories.Repository
import com.github.apognu.otter.utils.Cache
import com.github.apognu.otter.utils.Event
import com.github.apognu.otter.utils.EventBus
import com.github.apognu.otter.utils.untilNetwork
import com.github.apognu.otter.utils.*
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_artists.*
import kotlinx.coroutines.Dispatchers.IO
@ -105,6 +102,10 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment @@ -105,6 +102,10 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
}
repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore ->
if (isCache && data.isEmpty()) {
fetch(Repository.Origin.Network.origin)
}
lifecycleScope.launch(Main) {
if (isCache) {
adapter.data = data.toMutableList()

4
app/src/main/java/com/github/apognu/otter/repositories/Repository.kt

@ -41,9 +41,11 @@ abstract class Repository<D : Any, C : CacheItem<D>> { @@ -41,9 +41,11 @@ abstract class Repository<D : Any, C : CacheItem<D>> {
cacheId?.let { cacheId ->
Cache.get(context, cacheId)?.let { reader ->
uncache(reader)?.let { cache ->
emit(Response(Origin.Cache, cache.data, ceil(cache.data.size / AppContext.PAGE_SIZE.toDouble()).toInt(), false))
return@flow emit(Response(Origin.Cache, cache.data, ceil(cache.data.size / AppContext.PAGE_SIZE.toDouble()).toInt(), false))
}
}
return@flow emit(Response(Origin.Cache, listOf(), 1, false))
}
}.flowOn(IO)

Loading…
Cancel
Save