From 2a03783623ef834a29a7ccb84a2edcc3e36d3427 Mon Sep 17 00:00:00 2001 From: Amine Bou Date: Thu, 10 May 2018 12:57:57 +0200 Subject: [PATCH] Trying to fix some swipe and reload items issues. --- .../bou/readerforselfoss/HomeActivity.kt | 32 +++++++++++++++---- .../adapters/ItemCardAdapter.kt | 3 +- .../adapters/ItemListAdapter.kt | 3 +- .../readerforselfoss/adapters/ItemsAdapter.kt | 12 +++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index aa91d56..3299bb0 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -18,6 +18,7 @@ import android.support.v7.widget.RecyclerView import android.support.v7.widget.SearchView import android.support.v7.widget.StaggeredGridLayoutManager import android.support.v7.widget.helper.ItemTouchHelper +import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -76,7 +77,6 @@ import kotlinx.android.synthetic.main.activity_home.* import retrofit2.Call import retrofit2.Callback import retrofit2.Response - class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private val MENU_PREFERENCES = 12302 @@ -187,6 +187,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { R.color.refresh_progress_3 ) swipeRefreshLayout.setOnRefreshListener { + allItems = ArrayList() + lastFetchDone = false handleDrawerItems() getElementsAccordingToTab() } @@ -253,11 +255,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { else -> 0 } + Log.e("TAGTAG", "$lastVisibleItem === ${items.size} && ${items.size} <= ${maxItemNumber()} && ${!lastFetchDone}x") if (lastVisibleItem === items.size && items.size <= maxItemNumber() && - (maxItemNumber() >= itemsNumber || !lastFetchDone) + !lastFetchDone ) { - if (maxItemNumber() < itemsNumber) { + if (maxItemNumber() <= itemsNumber) { lastFetchDone = true } getElementsAccordingToTab( @@ -823,8 +826,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { if (shouldUpdate) { items = response.body() as ArrayList - items.forEach { - if (!allItems.contains(it)) allItems.add(it) + if (allItems.isEmpty()) { + allItems = items + } else { + items.forEach { + if (!allItems.contains(it)) allItems.add(it) + } } } } else { @@ -929,7 +936,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { fullHeightCards, appColors, debugReadingItems, - userIdentifier + userIdentifier, + { + updateItems(it) + } ) } else { recyclerAdapter = @@ -943,7 +953,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { articleViewer, debugReadingItems, userIdentifier, - appColors + appColors, + { + updateItems(it) + } ) recyclerView.addItemDecoration( @@ -1227,4 +1240,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { FAV_SHOWN -> badgeFavs else -> badgeNew // if !elementsShown then unread are fetched. } + + fun updateItems(adapterItems: ArrayList) { + items = adapterItems + } } + diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index 5c4cc68..ad9c2f3 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -44,7 +44,8 @@ class ItemCardAdapter( private val fullHeightCards: Boolean, override val appColors: AppColors, override val debugReadingItems: Boolean, - override val userIdentifier: String + override val userIdentifier: String, + override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val c: Context = app.baseContext private val generator: ColorGenerator = ColorGenerator.MATERIAL diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index c169254..5870145 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -45,7 +45,8 @@ class ItemListAdapter( private val articleViewer: Boolean, override val debugReadingItems: Boolean, override val userIdentifier: String, - override val appColors: AppColors + override val appColors: AppColors, + override val updateItems: (ArrayList) -> Unit ) : ItemsAdapter() { private val generator: ColorGenerator = ColorGenerator.MATERIAL private val c: Context = app.baseContext diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt index 0472c7b..1d275c1 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemsAdapter.kt @@ -24,10 +24,12 @@ abstract class ItemsAdapter : RecyclerView.Adapte abstract val userIdentifier: String abstract val app: Activity abstract val appColors: AppColors + abstract val updateItems: (ArrayList) -> Unit fun updateAllItems(newItems: ArrayList) { items = newItems notifyDataSetChanged() + updateItems(items) } private fun doUnmark(i: Item, position: Int) { @@ -40,6 +42,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte .setAction(R.string.undo_string) { items.add(position, i) notifyItemInserted(position) + updateItems(items) api.unmarkItem(i.id).enqueue(object : Callback { override fun onResponse( @@ -51,6 +54,7 @@ abstract class ItemsAdapter : RecyclerView.Adapte override fun onFailure(call: Call, t: Throwable) { items.remove(i) notifyItemRemoved(position) + updateItems(items) doUnmark(i, position) } }) @@ -68,6 +72,8 @@ abstract class ItemsAdapter : RecyclerView.Adapte items.remove(i) notifyItemRemoved(position) + updateItems(items) + api.markItem(i.id).enqueue(object : Callback { override fun onResponse( @@ -106,6 +112,8 @@ abstract class ItemsAdapter : RecyclerView.Adapte ).show() items.add(i) notifyItemInserted(position) + updateItems(items) + } }) } @@ -113,11 +121,15 @@ abstract class ItemsAdapter : RecyclerView.Adapte fun addItemAtIndex(item: Item, position: Int) { items.add(position, item) notifyItemInserted(position) + updateItems(items) + } fun addItemsAtEnd(newItems: List) { val oldSize = items.size items.addAll(newItems) notifyItemRangeInserted(oldSize, newItems.size) + updateItems(items) + } } \ No newline at end of file