From 218b5952ac19e4ef606cda8a6178f5b4a7787ee3 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 31 Oct 2019 12:26:57 -0600 Subject: [PATCH] Fetch all tellers from the webservice. - Modified the method that fetches the tellers from the webservice to keep requesting the tellers list until nothing is returned, to make sure the whole list of tellers is obtained. --- .../repositories/TellerRepository.kt | 53 ++++++++++++------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TellerRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TellerRepository.kt index 6890b2c..9e757a2 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TellerRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TellerRepository.kt @@ -21,13 +21,27 @@ class TellerRepository internal constructor(val context: Context) : retrofit2.Ca companion object { private const val TAG = "TellerRepository" + + private const val TELLERS_QUERY_LIMIT = 50 } private val mTellerDao: TellerDao + private val tellersList = mutableListOf() + private var tellersSkip = 0 + + private val bitsyWebservice: BitsyWebservice + init { val db = BitsyDatabase.getDatabase(context) mTellerDao = db!!.tellerDao() + + val retrofit = Retrofit.Builder() + .baseUrl(Constants.BITSY_WEBSERVICE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + + bitsyWebservice = retrofit.create(BitsyWebservice::class.java) } /** Returns a LiveData object directly from the database while the response from the WebService is obtained. */ @@ -45,15 +59,8 @@ class TellerRepository internal constructor(val context: Context) : retrofit2.Ca if (lastTellerUpdate + Constants.MERCHANTS_UPDATE_PERIOD < now) { Log.d(TAG, "Updating tellers from webservice") - // TODO make sure it works when there are more tellers than those sent back in the first response - val retrofit = Retrofit.Builder() - .baseUrl(Constants.BITSY_WEBSERVICE_URL) - .addConverterFactory(GsonConverterFactory.create()) - .build() - - val bitsyWebservice = retrofit.create(BitsyWebservice::class.java) - val call = bitsyWebservice.getTellers(0) - call.enqueue(this) + val request = bitsyWebservice.getTellers(tellersSkip, TELLERS_QUERY_LIMIT) + request.enqueue(this) } } @@ -61,23 +68,29 @@ class TellerRepository internal constructor(val context: Context) : retrofit2.Ca if (response.isSuccessful) { val res: FeathersResponse? = response.body() val tellers = res?.data ?: return - insertAllAsyncTask(mTellerDao).execute(tellers) - val now = System.currentTimeMillis() - PreferenceManager.getDefaultSharedPreferences(context).edit() - .putLong(Constants.KEY_TELLERS_LAST_UPDATE, now).apply() + if (tellers.isNotEmpty()) { + tellersList.addAll(tellers) + tellersSkip += TELLERS_QUERY_LIMIT + + val request = bitsyWebservice.getTellers(tellersSkip, TELLERS_QUERY_LIMIT) + request.enqueue(this) + } else { + updateTellers(tellersList) + + val now = System.currentTimeMillis() + PreferenceManager.getDefaultSharedPreferences(context).edit() + .putLong(Constants.KEY_TELLERS_LAST_UPDATE, now).apply() + } } } override fun onFailure(call: Call>, t: Throwable) { /* Do nothing */ } - private class insertAllAsyncTask internal constructor(private val mAsyncTaskDao: TellerDao) : - AsyncTask, Void, Void>() { - - override fun doInBackground(vararg tellers: List): Void? { - mAsyncTaskDao.deleteAll() - mAsyncTaskDao.insertAll(tellers[0]) - return null + private fun updateTellers(tellers: List) { + AsyncTask.execute { + mTellerDao.deleteAll() + mTellerDao.insertAll(tellers) } }