diff --git a/app/src/main/java/com/github/apognu/otter/fragments/OtterFragment.kt b/app/src/main/java/com/github/apognu/otter/fragments/OtterFragment.kt index a5e3e1a..288cbd3 100644 --- a/app/src/main/java/com/github/apognu/otter/fragments/OtterFragment.kt +++ b/app/src/main/java/com/github/apognu/otter/fragments/OtterFragment.kt @@ -32,7 +32,9 @@ abstract class OtterAdapter : RecyclerView.Adap } abstract class OtterFragment> : 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> : 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> : 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) {} @@ -128,7 +130,7 @@ abstract class OtterFragment> : Fragment() { return@launch } - if (first && data.isNotEmpty()) { + if (first) { adapter.data.clear() } @@ -137,17 +139,15 @@ abstract class OtterFragment> : 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) { } }