Fix dynamically changing between own music and all music. Dynamically reload all tabs instead of only active ones.

This commit is contained in:
Antoine POPINEAU 2020-07-13 10:31:36 +02:00
parent 5c684b6e67
commit a7968e9a87
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
1 changed files with 25 additions and 25 deletions

View File

@ -32,7 +32,9 @@ abstract class OtterAdapter<D, VH : RecyclerView.ViewHolder> : RecyclerView.Adap
}
abstract class OtterFragment<D : Any, A : OtterAdapter<D, *>> : Fragment() {
val OFFSCREEN_PAGES = 20
companion object {
const val OFFSCREEN_PAGES = 20
}
abstract val viewRes: Int
abstract val recycler: RecyclerView
@ -70,6 +72,19 @@ abstract class OtterFragment<D : Any, A : OtterAdapter<D, *>> : Fragment() {
}
}
if (listener == null) {
listener = lifecycleScope.launch(IO) {
EventBus.get().collect { event ->
if (event is Event.ListingsChanged) {
withContext(Main) {
swiper?.isRefreshing = true
fetch(Repository.Origin.Network.origin)
}
}
}
}
}
fetch(Repository.Origin.Cache.origin)
if (alwaysRefresh && adapter.data.isEmpty()) {
@ -83,19 +98,6 @@ abstract class OtterFragment<D : Any, A : OtterAdapter<D, *>> : Fragment() {
swiper?.setOnRefreshListener {
fetch(Repository.Origin.Network.origin)
}
if (listener == null) {
listener = lifecycleScope.launch(IO) {
EventBus.get().collect { event ->
if (event is Event.ListingsChanged) {
withContext(Main) {
swiper?.isRefreshing = true
fetch(Repository.Origin.Network.origin)
}
}
}
}
}
}
open fun onDataFetched(data: List<D>) {}
@ -128,7 +130,7 @@ abstract class OtterFragment<D : Any, A : OtterAdapter<D, *>> : Fragment() {
return@launch
}
if (first && data.isNotEmpty()) {
if (first) {
adapter.data.clear()
}
@ -137,17 +139,15 @@ abstract class OtterFragment<D : Any, A : OtterAdapter<D, *>> : Fragment() {
adapter.data.addAll(data)
withContext(IO) {
if (adapter.data.isNotEmpty()) {
try {
repository.cacheId?.let { cacheId ->
Cache.set(
context,
cacheId,
Gson().toJson(repository.cache(adapter.data)).toByteArray()
)
}
} catch (e: ConcurrentModificationException) {
try {
repository.cacheId?.let { cacheId ->
Cache.set(
context,
cacheId,
Gson().toJson(repository.cache(adapter.data)).toByteArray()
)
}
} catch (e: ConcurrentModificationException) {
}
}