mirror of
https://github.com/apognu/otter
synced 2025-02-21 13:00:34 +01:00
Load data from network on resume if cached data is empty.
This commit is contained in:
parent
97bb621d7f
commit
37d5c7b7be
@ -10,10 +10,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import com.github.apognu.otter.repositories.HttpUpstream
|
import com.github.apognu.otter.repositories.HttpUpstream
|
||||||
import com.github.apognu.otter.repositories.Repository
|
import com.github.apognu.otter.repositories.Repository
|
||||||
import com.github.apognu.otter.utils.Cache
|
import com.github.apognu.otter.utils.*
|
||||||
import com.github.apognu.otter.utils.Event
|
|
||||||
import com.github.apognu.otter.utils.EventBus
|
|
||||||
import com.github.apognu.otter.utils.untilNetwork
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import kotlinx.android.synthetic.main.fragment_artists.*
|
import kotlinx.android.synthetic.main.fragment_artists.*
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
@ -105,6 +102,10 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore ->
|
repository.fetch(upstreams, size).untilNetwork(lifecycleScope, IO) { data, isCache, page, hasMore ->
|
||||||
|
if (isCache && data.isEmpty()) {
|
||||||
|
fetch(Repository.Origin.Network.origin)
|
||||||
|
}
|
||||||
|
|
||||||
lifecycleScope.launch(Main) {
|
lifecycleScope.launch(Main) {
|
||||||
if (isCache) {
|
if (isCache) {
|
||||||
adapter.data = data.toMutableList()
|
adapter.data = data.toMutableList()
|
||||||
|
@ -41,9 +41,11 @@ abstract class Repository<D : Any, C : CacheItem<D>> {
|
|||||||
cacheId?.let { cacheId ->
|
cacheId?.let { cacheId ->
|
||||||
Cache.get(context, cacheId)?.let { reader ->
|
Cache.get(context, cacheId)?.let { reader ->
|
||||||
uncache(reader)?.let { cache ->
|
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)
|
}.flowOn(IO)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user