Fix miscellaneous bugs to Flow implementation.
This commit is contained in:
parent
ba12854e6c
commit
a21cafdbe0
|
@ -82,8 +82,8 @@ class TracksAdapter(private val context: Context?, private val favoriteListener:
|
||||||
|
|
||||||
context?.let {
|
context?.let {
|
||||||
when (track.favorite) {
|
when (track.favorite) {
|
||||||
true -> holder.favorite.setColorFilter(context.resources.getColor(R.color.colorFavorite))
|
true -> holder.favorite.setColorFilter(context.getColor(R.color.colorFavorite))
|
||||||
false -> holder.favorite.setColorFilter(context.resources.getColor(R.color.colorSelected))
|
false -> holder.favorite.setColorFilter(context.getColor(R.color.colorSelected))
|
||||||
}
|
}
|
||||||
|
|
||||||
holder.favorite.setOnClickListener {
|
holder.favorite.setOnClickListener {
|
||||||
|
|
|
@ -10,7 +10,6 @@ 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.Cache
|
||||||
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.android.synthetic.main.fragment_artists.*
|
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 ->
|
(repository.upstream as? HttpUpstream<*, *>)?.let { upstream ->
|
||||||
if (upstream.behavior == HttpUpstream.Behavior.Progressive) {
|
if (upstream.behavior == HttpUpstream.Behavior.Progressive) {
|
||||||
recycler.setOnScrollChangeListener { _, _, y, _, _ ->
|
recycler.setOnScrollChangeListener { _, _, _, _, _ ->
|
||||||
if (y > 0 && !recycler.canScrollVertically(1)) {
|
if (recycler.computeVerticalScrollOffset() > 0 && !recycler.canScrollVertically(1)) {
|
||||||
fetch(Repository.Origin.Network.origin, adapter.data.size)
|
fetch(Repository.Origin.Network.origin, adapter.data.size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,19 +66,20 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
|
||||||
open fun onDataFetched(data: List<D>) {}
|
open fun onDataFetched(data: List<D>) {}
|
||||||
|
|
||||||
private fun fetch(upstreams: Int = (Repository.Origin.Network.origin and Repository.Origin.Cache.origin), size: Int = 0) {
|
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
|
swiper?.isRefreshing = true
|
||||||
|
|
||||||
repository.fetch(upstreams, size).untilNetwork(IO) { data, isCache, hasMore ->
|
repository.fetch(upstreams, size).untilNetwork(IO) { data, isCache, hasMore ->
|
||||||
|
GlobalScope.launch(Main) {
|
||||||
if (isCache) {
|
if (isCache) {
|
||||||
adapter.data = data.toMutableList()
|
adapter.data = data.toMutableList()
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
|
|
||||||
return@untilNetwork
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
if (first) {
|
if (first && data.isNotEmpty()) {
|
||||||
first = false
|
first = false
|
||||||
|
|
||||||
adapter.data.clear()
|
adapter.data.clear()
|
||||||
|
@ -92,6 +92,9 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
|
||||||
if (!hasMore) {
|
if (!hasMore) {
|
||||||
swiper?.isRefreshing = false
|
swiper?.isRefreshing = false
|
||||||
|
|
||||||
|
GlobalScope.launch(IO) {
|
||||||
|
if (adapter.data.isNotEmpty()) {
|
||||||
|
try {
|
||||||
repository.cacheId?.let { cacheId ->
|
repository.cacheId?.let { cacheId ->
|
||||||
Cache.set(
|
Cache.set(
|
||||||
context,
|
context,
|
||||||
|
@ -99,9 +102,11 @@ abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment
|
||||||
Gson().toJson(repository.cache(adapter.data)).toByteArray()
|
Gson().toJson(repository.cache(adapter.data)).toByteArray()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} catch (e: ConcurrentModificationException) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GlobalScope.launch(Main) {
|
|
||||||
when (first) {
|
when (first) {
|
||||||
true -> {
|
true -> {
|
||||||
adapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
|
|
|
@ -100,10 +100,12 @@ class PlaylistTracksFragment : FunkwhaleFragment<PlaylistTrack, PlaylistTracksAd
|
||||||
else -> cover_top_left
|
else -> cover_top_left
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageView?.let { view ->
|
||||||
GlobalScope.launch(Main) {
|
GlobalScope.launch(Main) {
|
||||||
Picasso.get()
|
Picasso.get()
|
||||||
.maybeLoad(maybeNormalizeUrl(url))
|
.maybeLoad(maybeNormalizeUrl(url))
|
||||||
.into(imageView)
|
.into(view)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -413,6 +413,7 @@ class PlayerService : Service() {
|
||||||
)
|
)
|
||||||
|
|
||||||
player.next()
|
player.next()
|
||||||
|
player.playWhenReady = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import com.google.gson.Gson
|
||||||
import com.preference.PowerPreference
|
import com.preference.PowerPreference
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import java.io.Reader
|
import java.io.Reader
|
||||||
|
@ -45,7 +46,7 @@ class HttpUpstream<D : Any, R : FunkwhaleResponse<D>>(val behavior: Behavior, pr
|
||||||
} else {
|
} else {
|
||||||
emit(Repository.Response(Repository.Origin.Network, data, true))
|
emit(Repository.Response(Repository.Origin.Network, data, true))
|
||||||
|
|
||||||
fetch(size + data.size)
|
fetch(size + data.size).collect { emit(it) }
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ error ->
|
{ error ->
|
||||||
|
|
Loading…
Reference in New Issue