From 22c99d384cdc5902afd92b8277b48538618a2bc8 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Thu, 31 Oct 2019 11:20:12 +0100 Subject: [PATCH] Fixed issue #7 to display all search results properly. --- .../github/apognu/otter/activities/MainActivity.kt | 2 +- .../github/apognu/otter/activities/SearchActivity.kt | 7 +++---- .../apognu/otter/fragments/FunkwhaleFragment.kt | 8 +++++++- .../github/apognu/otter/repositories/Repository.kt | 11 ++--------- .../java/com/github/apognu/otter/utils/Extensions.kt | 4 ++-- 5 files changed, 15 insertions(+), 17 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 8642c55..568330a 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 @@ -256,7 +256,7 @@ class MainActivity : AppCompatActivity() { .centerCrop() .into(now_playing_details_cover) - favoriteCheckRepository.fetch().untilNetwork(IO) { favorites, _ -> + favoriteCheckRepository.fetch().untilNetwork(IO) { favorites, _, _ -> GlobalScope.launch(Main) { track.favorite = favorites.contains(track.id) diff --git a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt index 5cd6f45..7275d69 100644 --- a/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt +++ b/app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt @@ -44,16 +44,16 @@ class SearchActivity : AppCompatActivity() { adapter.data.clear() adapter.notifyDataSetChanged() - repository.fetch(Repository.Origin.Network.origin).untilNetwork { tracks, _ -> + repository.fetch(Repository.Origin.Network.origin).untilNetwork { tracks, _, _ -> search_spinner.visibility = View.GONE search_empty.visibility = View.GONE when (tracks.isEmpty()) { 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 - }) } } \ No newline at end of file diff --git a/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt index d2a446f..d3f90fa 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt @@ -75,7 +75,13 @@ abstract class FunkwhaleFragment> : Fragment 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) if (!hasMore) { diff --git a/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt b/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt index eda8774..bb50bb4 100644 --- a/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt +++ b/app/src/main/java/com/github/apognu/otter/repositories/Repository.kt @@ -3,6 +3,7 @@ package com.github.apognu.otter.repositories import android.content.Context import com.github.apognu.otter.utils.Cache import com.github.apognu.otter.utils.CacheItem +import com.github.apognu.otter.utils.log import com.github.apognu.otter.utils.untilNetwork import com.google.gson.Gson import kotlinx.coroutines.Dispatchers.IO @@ -60,17 +61,9 @@ abstract class Repository> { } private fun fromNetwork(size: Int) { - upstream.fetch(size)?.untilNetwork(IO) { data, hasMore -> + upstream.fetch(size)?.untilNetwork(IO) { data, _, hasMore -> val data = onDataFetched(data) - cacheId?.let { cacheId -> - Cache.set( - context, - cacheId, - Gson().toJson(cache(data)).toByteArray() - ) - } - channel.offer(Response(Origin.Network, data, hasMore)) } } diff --git a/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt b/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt index a387b9b..71bec1a 100644 --- a/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt +++ b/app/src/main/java/com/github/apognu/otter/utils/Extensions.kt @@ -28,10 +28,10 @@ inline fun Channel>.await(context: CoroutineContext = } } -inline fun Channel>.untilNetwork(context: CoroutineContext = Main, crossinline callback: (data: List, hasMore: Boolean) -> Unit) { +inline fun Channel>.untilNetwork(context: CoroutineContext = Main, crossinline callback: (data: List, isCache: Boolean, hasMore: Boolean) -> Unit) { GlobalScope.launch(context) { 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) { close()