Fixed issue #7 to display all search results properly.

This commit is contained in:
Antoine POPINEAU 2019-10-31 11:20:12 +01:00
parent 657c72480e
commit 22c99d384c
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
5 changed files with 15 additions and 17 deletions

View File

@ -256,7 +256,7 @@ class MainActivity : AppCompatActivity() {
.centerCrop() .centerCrop()
.into(now_playing_details_cover) .into(now_playing_details_cover)
favoriteCheckRepository.fetch().untilNetwork(IO) { favorites, _ -> favoriteCheckRepository.fetch().untilNetwork(IO) { favorites, _, _ ->
GlobalScope.launch(Main) { GlobalScope.launch(Main) {
track.favorite = favorites.contains(track.id) track.favorite = favorites.contains(track.id)

View File

@ -44,16 +44,16 @@ class SearchActivity : AppCompatActivity() {
adapter.data.clear() adapter.data.clear()
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
repository.fetch(Repository.Origin.Network.origin).untilNetwork { tracks, _ -> repository.fetch(Repository.Origin.Network.origin).untilNetwork { tracks, _, _ ->
search_spinner.visibility = View.GONE search_spinner.visibility = View.GONE
search_empty.visibility = View.GONE search_empty.visibility = View.GONE
when (tracks.isEmpty()) { when (tracks.isEmpty()) {
true -> search_no_results.visibility = View.VISIBLE true -> search_no_results.visibility = View.VISIBLE
false -> adapter.data = tracks.toMutableList() false -> adapter.data.addAll(tracks)
} }
adapter.notifyDataSetChanged() adapter.notifyItemRangeInserted(adapter.data.size, tracks.size)
} }
} }
@ -61,7 +61,6 @@ class SearchActivity : AppCompatActivity() {
} }
override fun onQueryTextChange(newText: String?) = true override fun onQueryTextChange(newText: String?) = true
}) })
} }
} }

View File

@ -75,7 +75,13 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
adapter.data.clear() adapter.data.clear()
} }
repository.fetch(upstreams, size).untilNetwork(IO) { data, hasMore -> repository.fetch(upstreams, size).untilNetwork(IO) { data, isCache, hasMore ->
if (isCache) {
adapter.data = data.toMutableList()
return@untilNetwork
}
onDataFetched(data) onDataFetched(data)
if (!hasMore) { if (!hasMore) {

View File

@ -3,6 +3,7 @@ package com.github.apognu.otter.repositories
import android.content.Context import android.content.Context
import com.github.apognu.otter.utils.Cache import com.github.apognu.otter.utils.Cache
import com.github.apognu.otter.utils.CacheItem import com.github.apognu.otter.utils.CacheItem
import com.github.apognu.otter.utils.log
import com.github.apognu.otter.utils.untilNetwork import com.github.apognu.otter.utils.untilNetwork
import com.google.gson.Gson import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@ -60,17 +61,9 @@ abstract class Repository<D : Any, C : CacheItem<D>> {
} }
private fun fromNetwork(size: Int) { private fun fromNetwork(size: Int) {
upstream.fetch(size)?.untilNetwork(IO) { data, hasMore -> upstream.fetch(size)?.untilNetwork(IO) { data, _, hasMore ->
val data = onDataFetched(data) val data = onDataFetched(data)
cacheId?.let { cacheId ->
Cache.set(
context,
cacheId,
Gson().toJson(cache(data)).toByteArray()
)
}
channel.offer(Response(Origin.Network, data, hasMore)) channel.offer(Response(Origin.Network, data, hasMore))
} }
} }

View File

@ -28,10 +28,10 @@ inline fun <D> Channel<Repository.Response<D>>.await(context: CoroutineContext =
} }
} }
inline fun <D> Channel<Repository.Response<D>>.untilNetwork(context: CoroutineContext = Main, crossinline callback: (data: List<D>, hasMore: Boolean) -> Unit) { inline fun <D> Channel<Repository.Response<D>>.untilNetwork(context: CoroutineContext = Main, crossinline callback: (data: List<D>, isCache: Boolean, hasMore: Boolean) -> Unit) {
GlobalScope.launch(context) { GlobalScope.launch(context) {
for (data in this@untilNetwork) { for (data in this@untilNetwork) {
callback(data.data, data.hasMore) callback(data.data, data.origin == Repository.Origin.Cache, data.hasMore)
if (data.origin == Repository.Origin.Network && !data.hasMore) { if (data.origin == Repository.Origin.Network && !data.hasMore) {
close() close()