Infinite scroll should be working as expected. Fixes #116.

This commit is contained in:
Amine 2017-11-18 15:09:44 +01:00
parent 8fb339034f
commit 687839b5f8

View File

@ -71,7 +71,6 @@ import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
import kotlinx.android.synthetic.main.activity_home.* import kotlinx.android.synthetic.main.activity_home.*
class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private val MENU_PREFERENCES = 12302 private val MENU_PREFERENCES = 12302
@ -209,8 +208,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
else -> 0 else -> 0
} }
if (lastVisibleItem == (items.size - 1)) { if (lastVisibleItem === items.size && items.size <= maxItemNumber()) {
getElementsAccordingToTab(appendResults = true) getElementsAccordingToTab(appendResults = true, offsetOverride = lastVisibleItem)
} }
} catch (e: IndexOutOfBoundsException) { } catch (e: IndexOutOfBoundsException) {
@ -634,7 +633,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
else -> 0 else -> 0
} }
if (lastVisibleItem == (items.size - 1)) { if (lastVisibleItem == (items.size - 1) && items.size < maxItemNumber()) {
getElementsAccordingToTab(appendResults = true) getElementsAccordingToTab(appendResults = true)
} }
} }
@ -645,7 +644,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
recyclerView.addOnScrollListener(recyclerViewScrollListener) recyclerView.addOnScrollListener(recyclerViewScrollListener)
} }
fun mayBeEmpty() = private fun mayBeEmpty() =
if (items.isEmpty()) { if (items.isEmpty()) {
emptyText.visibility = View.VISIBLE emptyText.visibility = View.VISIBLE
recyclerView.visibility = View.GONE recyclerView.visibility = View.GONE
@ -654,13 +653,21 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
recyclerView.visibility = View.VISIBLE recyclerView.visibility = View.VISIBLE
} }
private fun getElementsAccordingToTab(appendResults: Boolean = false) = private fun getElementsAccordingToTab(appendResults: Boolean = false, offsetOverride: Int? = null) {
when (elementsShown) { offset = if (appendResults && offsetOverride === null) {
UNREAD_SHOWN -> getUnRead(appendResults) (offset + itemsNumber)
READ_SHOWN -> getRead(appendResults) } else {
FAV_SHOWN -> getStarred(appendResults) offsetOverride ?: 0
else -> getUnRead(appendResults) }
} firstVisible = if (appendResults) firstVisible else 0
when (elementsShown) {
UNREAD_SHOWN -> getUnRead(appendResults)
READ_SHOWN -> getRead(appendResults)
FAV_SHOWN -> getStarred(appendResults)
else -> getUnRead(appendResults)
}
}
private fun doCallTo(appendResults: Boolean, toastMessage: Int, call: (String?, Long?, String?) -> Call<List<Item>>) { private fun doCallTo(appendResults: Boolean, toastMessage: Int, call: (String?, Long?, String?) -> Call<List<Item>>) {
fun handleItemsResponse(response: Response<List<Item>>) { fun handleItemsResponse(response: Response<List<Item>>) {
@ -700,25 +707,16 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
private fun getUnRead(appendResults: Boolean = false) { private fun getUnRead(appendResults: Boolean = false) {
offset = if (appendResults) (offset + itemsNumber)
else 0
firstVisible = if (appendResults) firstVisible else 0
elementsShown = UNREAD_SHOWN elementsShown = UNREAD_SHOWN
doCallTo(appendResults, R.string.cant_get_new_elements){t, id, f -> api.newItems(t, id, f, itemsNumber, offset)} doCallTo(appendResults, R.string.cant_get_new_elements){t, id, f -> api.newItems(t, id, f, itemsNumber, offset)}
} }
private fun getRead(appendResults: Boolean = false) { private fun getRead(appendResults: Boolean = false) {
offset = if (appendResults) (offset + itemsNumber)
else 0
firstVisible = if (appendResults) firstVisible else 0
elementsShown = READ_SHOWN elementsShown = READ_SHOWN
doCallTo(appendResults, R.string.cant_get_read){t, id, f -> api.readItems(t, id, f, itemsNumber, offset)} doCallTo(appendResults, R.string.cant_get_read){t, id, f -> api.readItems(t, id, f, itemsNumber, offset)}
} }
private fun getStarred(appendResults: Boolean = false) { private fun getStarred(appendResults: Boolean = false) {
offset = if (appendResults) (offset + itemsNumber)
else 0
firstVisible = if (appendResults) firstVisible else 0
elementsShown = FAV_SHOWN elementsShown = FAV_SHOWN
doCallTo(appendResults, R.string.cant_get_favs){t, id, f -> api.starredItems(t, id, f, itemsNumber, offset)} doCallTo(appendResults, R.string.cant_get_favs){t, id, f -> api.starredItems(t, id, f, itemsNumber, offset)}
} }
@ -937,4 +935,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
else -> return super.onOptionsItemSelected(item) else -> return super.onOptionsItemSelected(item)
} }
} }
private fun maxItemNumber(): Int =
when (elementsShown) {
UNREAD_SHOWN -> badgeNew
READ_SHOWN -> badgeAll
FAV_SHOWN -> badgeFavs
else -> badgeNew // if !elementsShown then unread are fetched.
}
} }