Trying to fix some swipe and reload items issues.

This commit is contained in:
Amine Bou 2018-05-10 12:57:57 +02:00
parent 9f2a4438b1
commit 2a03783623
4 changed files with 41 additions and 9 deletions

View File

@ -18,6 +18,7 @@ import android.support.v7.widget.RecyclerView
import android.support.v7.widget.SearchView import android.support.v7.widget.SearchView
import android.support.v7.widget.StaggeredGridLayoutManager import android.support.v7.widget.StaggeredGridLayoutManager
import android.support.v7.widget.helper.ItemTouchHelper import android.support.v7.widget.helper.ItemTouchHelper
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
@ -76,7 +77,6 @@ import kotlinx.android.synthetic.main.activity_home.*
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private val MENU_PREFERENCES = 12302 private val MENU_PREFERENCES = 12302
@ -187,6 +187,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
R.color.refresh_progress_3 R.color.refresh_progress_3
) )
swipeRefreshLayout.setOnRefreshListener { swipeRefreshLayout.setOnRefreshListener {
allItems = ArrayList()
lastFetchDone = false
handleDrawerItems() handleDrawerItems()
getElementsAccordingToTab() getElementsAccordingToTab()
} }
@ -253,11 +255,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
else -> 0 else -> 0
} }
Log.e("TAGTAG", "$lastVisibleItem === ${items.size} && ${items.size} <= ${maxItemNumber()} && ${!lastFetchDone}x")
if (lastVisibleItem === items.size && if (lastVisibleItem === items.size &&
items.size <= maxItemNumber() && items.size <= maxItemNumber() &&
(maxItemNumber() >= itemsNumber || !lastFetchDone) !lastFetchDone
) { ) {
if (maxItemNumber() < itemsNumber) { if (maxItemNumber() <= itemsNumber) {
lastFetchDone = true lastFetchDone = true
} }
getElementsAccordingToTab( getElementsAccordingToTab(
@ -823,8 +826,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
if (shouldUpdate) { if (shouldUpdate) {
items = response.body() as ArrayList<Item> items = response.body() as ArrayList<Item>
items.forEach { if (allItems.isEmpty()) {
if (!allItems.contains(it)) allItems.add(it) allItems = items
} else {
items.forEach {
if (!allItems.contains(it)) allItems.add(it)
}
} }
} }
} else { } else {
@ -929,7 +936,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
fullHeightCards, fullHeightCards,
appColors, appColors,
debugReadingItems, debugReadingItems,
userIdentifier userIdentifier,
{
updateItems(it)
}
) )
} else { } else {
recyclerAdapter = recyclerAdapter =
@ -943,7 +953,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
articleViewer, articleViewer,
debugReadingItems, debugReadingItems,
userIdentifier, userIdentifier,
appColors appColors,
{
updateItems(it)
}
) )
recyclerView.addItemDecoration( recyclerView.addItemDecoration(
@ -1227,4 +1240,9 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
FAV_SHOWN -> badgeFavs FAV_SHOWN -> badgeFavs
else -> badgeNew // if !elementsShown then unread are fetched. else -> badgeNew // if !elementsShown then unread are fetched.
} }
fun updateItems(adapterItems: ArrayList<Item>) {
items = adapterItems
}
} }

View File

@ -44,7 +44,8 @@ class ItemCardAdapter(
private val fullHeightCards: Boolean, private val fullHeightCards: Boolean,
override val appColors: AppColors, override val appColors: AppColors,
override val debugReadingItems: Boolean, override val debugReadingItems: Boolean,
override val userIdentifier: String override val userIdentifier: String,
override val updateItems: (ArrayList<Item>) -> Unit
) : ItemsAdapter<ItemCardAdapter.ViewHolder>() { ) : ItemsAdapter<ItemCardAdapter.ViewHolder>() {
private val c: Context = app.baseContext private val c: Context = app.baseContext
private val generator: ColorGenerator = ColorGenerator.MATERIAL private val generator: ColorGenerator = ColorGenerator.MATERIAL

View File

@ -45,7 +45,8 @@ class ItemListAdapter(
private val articleViewer: Boolean, private val articleViewer: Boolean,
override val debugReadingItems: Boolean, override val debugReadingItems: Boolean,
override val userIdentifier: String, override val userIdentifier: String,
override val appColors: AppColors override val appColors: AppColors,
override val updateItems: (ArrayList<Item>) -> Unit
) : ItemsAdapter<ItemListAdapter.ViewHolder>() { ) : ItemsAdapter<ItemListAdapter.ViewHolder>() {
private val generator: ColorGenerator = ColorGenerator.MATERIAL private val generator: ColorGenerator = ColorGenerator.MATERIAL
private val c: Context = app.baseContext private val c: Context = app.baseContext

View File

@ -24,10 +24,12 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
abstract val userIdentifier: String abstract val userIdentifier: String
abstract val app: Activity abstract val app: Activity
abstract val appColors: AppColors abstract val appColors: AppColors
abstract val updateItems: (ArrayList<Item>) -> Unit
fun updateAllItems(newItems: ArrayList<Item>) { fun updateAllItems(newItems: ArrayList<Item>) {
items = newItems items = newItems
notifyDataSetChanged() notifyDataSetChanged()
updateItems(items)
} }
private fun doUnmark(i: Item, position: Int) { private fun doUnmark(i: Item, position: Int) {
@ -40,6 +42,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
.setAction(R.string.undo_string) { .setAction(R.string.undo_string) {
items.add(position, i) items.add(position, i)
notifyItemInserted(position) notifyItemInserted(position)
updateItems(items)
api.unmarkItem(i.id).enqueue(object : Callback<SuccessResponse> { api.unmarkItem(i.id).enqueue(object : Callback<SuccessResponse> {
override fun onResponse( override fun onResponse(
@ -51,6 +54,7 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
override fun onFailure(call: Call<SuccessResponse>, t: Throwable) { override fun onFailure(call: Call<SuccessResponse>, t: Throwable) {
items.remove(i) items.remove(i)
notifyItemRemoved(position) notifyItemRemoved(position)
updateItems(items)
doUnmark(i, position) doUnmark(i, position)
} }
}) })
@ -68,6 +72,8 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
items.remove(i) items.remove(i)
notifyItemRemoved(position) notifyItemRemoved(position)
updateItems(items)
api.markItem(i.id).enqueue(object : Callback<SuccessResponse> { api.markItem(i.id).enqueue(object : Callback<SuccessResponse> {
override fun onResponse( override fun onResponse(
@ -106,6 +112,8 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
).show() ).show()
items.add(i) items.add(i)
notifyItemInserted(position) notifyItemInserted(position)
updateItems(items)
} }
}) })
} }
@ -113,11 +121,15 @@ abstract class ItemsAdapter<VH : RecyclerView.ViewHolder?> : RecyclerView.Adapte
fun addItemAtIndex(item: Item, position: Int) { fun addItemAtIndex(item: Item, position: Int) {
items.add(position, item) items.add(position, item)
notifyItemInserted(position) notifyItemInserted(position)
updateItems(items)
} }
fun addItemsAtEnd(newItems: List<Item>) { fun addItemsAtEnd(newItems: List<Item>) {
val oldSize = items.size val oldSize = items.size
items.addAll(newItems) items.addAll(newItems)
notifyItemRangeInserted(oldSize, newItems.size) notifyItemRangeInserted(oldSize, newItems.size)
updateItems(items)
} }
} }