Fix miscellaneous bugs to Flow implementation.

This commit is contained in:
Antoine POPINEAU 2019-11-01 13:42:15 +01:00
parent ba12854e6c
commit a21cafdbe0
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
5 changed files with 49 additions and 40 deletions

View File

@ -82,8 +82,8 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
context?.let {
when (track.favorite) {
true -> holder.favorite.setColorFilter(context.resources.getColor(R.color.colorFavorite))
false -> holder.favorite.setColorFilter(context.resources.getColor(R.color.colorSelected))
true -> holder.favorite.setColorFilter(context.getColor(R.color.colorFavorite))
false -> holder.favorite.setColorFilter(context.getColor(R.color.colorSelected))
}
holder.favorite.setOnClickListener {

View File

@ -10,7 +10,6 @@ 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.log
import com.github.apognu.otter.utils.untilNetwork
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_artists.*
@ -45,8 +44,8 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
(repository.upstream as? HttpUpstream<*, *>)?.let { upstream ->
if (upstream.behavior == HttpUpstream.Behavior.Progressive) {
recycler.setOnScrollChangeListener { _, _, y, _, _ ->
if (y > 0 && !recycler.canScrollVertically(1)) {
recycler.setOnScrollChangeListener { _, _, _, _, _ ->
if (recycler.computeVerticalScrollOffset() > 0 && !recycler.canScrollVertically(1)) {
fetch(Repository.Origin.Network.origin, adapter.data.size)
}
}
@ -67,41 +66,47 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
open fun onDataFetched(data: List<D>) {}
private fun fetch(upstreams: Int = (Repository.Origin.Network.origin and Repository.Origin.Cache.origin), size: Int = 0) {
var first = true
var first = size == 0
swiper?.isRefreshing = true
repository.fetch(upstreams, size).untilNetwork(IO) { data, isCache, hasMore ->
if (isCache) {
adapter.data = data.toMutableList()
adapter.notifyDataSetChanged()
return@untilNetwork
}
if (first) {
first = false
adapter.data.clear()
}
onDataFetched(data)
adapter.data.addAll(data)
if (!hasMore) {
swiper?.isRefreshing = false
repository.cacheId?.let { cacheId ->
Cache.set(
context,
cacheId,
Gson().toJson(repository.cache(adapter.data)).toByteArray()
)
}
}
GlobalScope.launch(Main) {
if (isCache) {
adapter.data = data.toMutableList()
adapter.notifyDataSetChanged()
return@launch
}
if (first && data.isNotEmpty()) {
first = false
adapter.data.clear()
}
onDataFetched(data)
adapter.data.addAll(data)
if (!hasMore) {
swiper?.isRefreshing = false
GlobalScope.launch(IO) {
if (adapter.data.isNotEmpty()) {
try {
repository.cacheId?.let { cacheId ->
Cache.set(
context,
cacheId,
Gson().toJson(repository.cache(adapter.data)).toByteArray()
)
}
} catch (e: ConcurrentModificationException) {}
}
}
}
when (first) {
true -> {
adapter.notifyDataSetChanged()

View File

@ -100,10 +100,12 @@ class PlaylistTracksFragment : FunkwhaleFragment<PlaylistTrack, PlaylistTracksAd
else -> cover_top_left
}
GlobalScope.launch(Main) {
Picasso.get()
.maybeLoad(maybeNormalizeUrl(url))
.into(imageView)
imageView?.let { view ->
GlobalScope.launch(Main) {
Picasso.get()
.maybeLoad(maybeNormalizeUrl(url))
.into(view)
}
}
}
}

View File

@ -413,6 +413,7 @@ class PlayerService : Service() {
)
player.next()
player.playWhenReady = true
}
}

View File

@ -12,6 +12,7 @@ import com.google.gson.Gson
import com.preference.PowerPreference
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import java.io.Reader
@ -45,7 +46,7 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
} else {
emit(Repository.Response(Repository.Origin.Network, data, true))
fetch(size + data.size)
fetch(size + data.size).collect { emit(it) }
}
},
{ error ->