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.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<Item>
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<Item>) {
items = adapterItems
}
}

View File

@ -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<Item>) -> Unit
) : ItemsAdapter<ItemCardAdapter.ViewHolder>() {
private val c: Context = app.baseContext
private val generator: ColorGenerator = ColorGenerator.MATERIAL

View File

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