Browse Source

Fixed issue #7 to display all search results properly.

housekeeping/remove-warnings
Antoine POPINEAU 5 years ago
parent
commit
22c99d384c
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
  1. 2
      app/src/main/java/com/github/apognu/otter/activities/MainActivity.kt
  2. 7
      app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt
  3. 8
      app/src/main/java/com/github/apognu/otter/fragments/FunkwhaleFragment.kt
  4. 11
      app/src/main/java/com/github/apognu/otter/repositories/Repository.kt
  5. 4
      app/src/main/java/com/github/apognu/otter/utils/Extensions.kt

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

@ -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)

7
app/src/main/java/com/github/apognu/otter/activities/SearchActivity.kt

@ -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
}) })
} }
} }

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

@ -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) {

11
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 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))
} }
} }

4
app/src/main/java/com/github/apognu/otter/utils/Extensions.kt

@ -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()

Loading…
Cancel
Save