2019-08-19 16:50:33 +02:00
|
|
|
package com.github.apognu.otter.fragments
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2019-10-29 23:41:44 +01:00
|
|
|
import com.github.apognu.otter.repositories.HttpUpstream
|
2019-08-19 16:50:33 +02:00
|
|
|
import com.github.apognu.otter.repositories.Repository
|
2020-06-13 19:34:57 +02:00
|
|
|
import com.github.apognu.otter.utils.*
|
2019-10-29 23:41:44 +01:00
|
|
|
import com.google.gson.Gson
|
2019-08-19 16:50:33 +02:00
|
|
|
import kotlinx.android.synthetic.main.fragment_artists.*
|
2019-10-29 23:41:44 +01:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
|
|
|
import kotlinx.coroutines.GlobalScope
|
2020-06-13 19:34:57 +02:00
|
|
|
import kotlinx.coroutines.Job
|
|
|
|
import kotlinx.coroutines.flow.collect
|
2019-10-29 23:41:44 +01:00
|
|
|
import kotlinx.coroutines.launch
|
2020-06-13 19:34:57 +02:00
|
|
|
import kotlinx.coroutines.withContext
|
2019-08-19 16:50:33 +02:00
|
|
|
|
|
|
|
abstract class FunkwhaleAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>() {
|
|
|
|
var data: MutableList<D> = mutableListOf()
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class FunkwhaleFragment<D : Any, A : FunkwhaleAdapter<D, *>> : Fragment() {
|
|
|
|
abstract val viewRes: Int
|
|
|
|
abstract val recycler: RecyclerView
|
|
|
|
open val layoutManager: RecyclerView.LayoutManager get() = LinearLayoutManager(context)
|
|
|
|
|
|
|
|
lateinit var repository: Repository<D, *>
|
|
|
|
lateinit var adapter: A
|
|
|
|
|
2019-10-24 12:35:34 +02:00
|
|
|
private var initialFetched = false
|
2020-06-13 19:34:57 +02:00
|
|
|
private var listener: Job? = null
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2019-08-19 16:50:33 +02:00
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
return inflater.inflate(viewRes, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
recycler.layoutManager = layoutManager
|
|
|
|
recycler.adapter = adapter
|
|
|
|
|
2019-10-29 23:41:44 +01:00
|
|
|
(repository.upstream as? HttpUpstream<*, *>)?.let { upstream ->
|
|
|
|
if (upstream.behavior == HttpUpstream.Behavior.Progressive) {
|
2019-11-01 13:42:15 +01:00
|
|
|
recycler.setOnScrollChangeListener { _, _, _, _, _ ->
|
|
|
|
if (recycler.computeVerticalScrollOffset() > 0 && !recycler.canScrollVertically(1)) {
|
2019-10-29 23:41:44 +01:00
|
|
|
fetch(Repository.Origin.Network.origin, adapter.data.size)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 21:39:10 +01:00
|
|
|
fetch(Repository.Origin.Cache.origin)
|
|
|
|
|
|
|
|
if (adapter.data.isEmpty()) {
|
|
|
|
fetch(Repository.Origin.Network.origin)
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
|
|
|
|
swiper?.setOnRefreshListener {
|
2019-10-29 23:41:44 +01:00
|
|
|
fetch(Repository.Origin.Network.origin)
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
2020-06-13 19:34:57 +02:00
|
|
|
|
|
|
|
if (listener == null) {
|
|
|
|
listener = GlobalScope.launch(IO) {
|
|
|
|
EventBus.get().collect { event ->
|
|
|
|
if (event is Event.ListingsChanged) {
|
|
|
|
withContext(Main) {
|
|
|
|
swiper?.isRefreshing = true
|
|
|
|
fetch(Repository.Origin.Network.origin)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
open fun onDataFetched(data: List<D>) {}
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2019-11-25 21:39:10 +01:00
|
|
|
private fun fetch(upstreams: Int = Repository.Origin.Network.origin, size: Int = 0) {
|
2019-11-01 13:42:15 +01:00
|
|
|
var first = size == 0
|
2019-10-29 23:41:44 +01:00
|
|
|
|
2019-11-25 21:39:10 +01:00
|
|
|
if (upstreams == Repository.Origin.Network.origin) {
|
|
|
|
swiper?.isRefreshing = true
|
|
|
|
}
|
2019-10-29 23:41:44 +01:00
|
|
|
|
2019-10-31 11:20:12 +01:00
|
|
|
repository.fetch(upstreams, size).untilNetwork(IO) { data, isCache, hasMore ->
|
2019-11-01 13:42:15 +01:00
|
|
|
GlobalScope.launch(Main) {
|
|
|
|
if (isCache) {
|
|
|
|
adapter.data = data.toMutableList()
|
|
|
|
adapter.notifyDataSetChanged()
|
2019-10-31 16:17:37 +01:00
|
|
|
|
2019-11-01 13:42:15 +01:00
|
|
|
return@launch
|
|
|
|
}
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2019-11-01 13:42:15 +01:00
|
|
|
if (first && data.isNotEmpty()) {
|
|
|
|
adapter.data.clear()
|
|
|
|
}
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2019-11-01 13:42:15 +01:00
|
|
|
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()
|
|
|
|
)
|
|
|
|
}
|
2019-11-23 14:39:19 +01:00
|
|
|
} catch (e: ConcurrentModificationException) {
|
|
|
|
}
|
2019-11-01 13:42:15 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 23:41:44 +01:00
|
|
|
}
|
2019-10-24 12:35:34 +02:00
|
|
|
|
2019-10-31 16:17:37 +01:00
|
|
|
when (first) {
|
2019-10-29 23:41:44 +01:00
|
|
|
true -> {
|
|
|
|
adapter.notifyDataSetChanged()
|
2019-10-31 16:17:37 +01:00
|
|
|
first = false
|
2019-10-29 23:41:44 +01:00
|
|
|
}
|
2019-10-31 16:17:37 +01:00
|
|
|
|
2019-10-29 23:41:44 +01:00
|
|
|
false -> adapter.notifyItemRangeInserted(adapter.data.size, data.size)
|
|
|
|
}
|
2019-10-24 12:35:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 16:50:33 +02:00
|
|
|
}
|