From 2df5e52de096468c0d9879e07533f33459c36086 Mon Sep 17 00:00:00 2001 From: Amine Date: Wed, 7 Jun 2017 07:28:17 +0200 Subject: [PATCH] Fixes #6 --- .../bou/readerforselfoss/HomeActivity.kt | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 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 ec27546..619732a 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -463,7 +463,6 @@ class HomeActivity : AppCompatActivity() { } private fun getElementsAccordingToTab(maybeTagFilter: Tag? = null, maybeSourceFilter: Sources? = null) { - items = ArrayList() when (elementsShown) { UNREAD_SHOWN -> getUnRead(maybeTagFilter, maybeSourceFilter) @@ -477,13 +476,7 @@ class HomeActivity : AppCompatActivity() { elementsShown = UNREAD_SHOWN api!!.unreadItems(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong()).enqueue(object : Callback> { override fun onResponse(call: Call>, response: Response>) { - if (response.body() != null && response.body()!!.isNotEmpty()) { - items = response.body() as ArrayList - } else { - items = ArrayList() - } - handleListResult() - mSwipeRefreshLayout!!.isRefreshing = false + handleItemsResponse(response) } override fun onFailure(call: Call>, t: Throwable) { @@ -493,17 +486,26 @@ class HomeActivity : AppCompatActivity() { }) } + private fun handleItemsResponse(response: Response>) { + val didUpdate = (response.body() != items) + if (response.body() != null) { + if (response.body() != items) { + items = response.body() as ArrayList + } + } else { + items = ArrayList() + } + if (didUpdate) + handleListResult() + if (items.isEmpty()) Toast.makeText(this@HomeActivity, R.string.nothing_here, Toast.LENGTH_SHORT).show() + mSwipeRefreshLayout!!.isRefreshing = false + } + private fun getRead(maybeTagFilter: Tag? = null, maybeSourceFilter: Sources? = null) { elementsShown = READ_SHOWN api!!.readItems(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong()).enqueue(object : Callback> { override fun onResponse(call: Call>, response: Response>) { - if (response.body() != null && response.body()!!.isNotEmpty()) { - items = response.body() as ArrayList - } else { - items = ArrayList() - } - handleListResult() - mSwipeRefreshLayout!!.isRefreshing = false + handleItemsResponse(response) } override fun onFailure(call: Call>, t: Throwable) { @@ -517,13 +519,7 @@ class HomeActivity : AppCompatActivity() { elementsShown = FAV_SHOWN api!!.starredItems(maybeTagFilter?.tag, maybeSourceFilter?.id?.toLong()).enqueue(object : Callback> { override fun onResponse(call: Call>, response: Response>) { - if (response.body() != null && response.body()!!.isNotEmpty()) { - items = response.body() as ArrayList - } else { - items = ArrayList() - } - handleListResult() - mSwipeRefreshLayout!!.isRefreshing = false + handleItemsResponse(response) } override fun onFailure(call: Call>, t: Throwable) { @@ -545,8 +541,6 @@ class HomeActivity : AppCompatActivity() { mRecyclerView!!.adapter = mAdapter mAdapter.notifyDataSetChanged() - if (items.isEmpty()) Toast.makeText(this@HomeActivity, R.string.nothing_here, Toast.LENGTH_SHORT).show() - reloadBadges() } @@ -604,6 +598,7 @@ class HomeActivity : AppCompatActivity() { } }) items = ArrayList() + if (items.isEmpty()) Toast.makeText(this@HomeActivity, R.string.nothing_here, Toast.LENGTH_SHORT).show() handleListResult() } return true