From 0ae587a7aa413dd08f6445f0a37c1fe50f79fea0 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Thu, 31 Oct 2019 12:13:27 -0600 Subject: [PATCH] Fetch all merchants from the webservice. - Modified the method that fetches the merchants from the webservice to keep requesting the merchants list until nothing is returned, to make sure the whole list of merchants is obtained. - Modified the time the app waits to update the merchants and tellers list from 24 hours to just 3 hours. --- .../repositories/MerchantRepository.kt | 53 ++++++++++++------- .../bitsybitshareswallet/utils/Constants.kt | 2 +- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt index e29fa0c..eaab376 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt @@ -21,13 +21,27 @@ class MerchantRepository internal constructor(val context: Context) : retrofit2. companion object { private const val TAG = "MerchantRepository" + + private const val MERCHANTS_QUERY_LIMIT = 50 } private val mMerchantDao: MerchantDao + private val merchantsList = mutableListOf() + private var merchantsSkip = 0 + + private val bitsyWebservice: BitsyWebservice + init { val db = BitsyDatabase.getDatabase(context) mMerchantDao = db!!.merchantDao() + + 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 MerchantRepository internal constructor(val context: Context) : retrofit2. if (lastMerchantUpdate + Constants.MERCHANTS_UPDATE_PERIOD < now) { Log.d(TAG, "Updating merchants from webservice") - // TODO make sure it works when there are more merchants 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.getMerchants(0) - call.enqueue(this) + val request = bitsyWebservice.getMerchants(merchantsSkip, MERCHANTS_QUERY_LIMIT) + request.enqueue(this) } } @@ -61,23 +68,29 @@ class MerchantRepository internal constructor(val context: Context) : retrofit2. if (response.isSuccessful) { val res: FeathersResponse? = response.body() val merchants = res?.data ?: return - insertAllAsyncTask(mMerchantDao).execute(merchants) - val now = System.currentTimeMillis() - PreferenceManager.getDefaultSharedPreferences(context).edit() - .putLong(Constants.KEY_MERCHANTS_LAST_UPDATE, now).apply() + if (merchants.isNotEmpty()) { + merchantsList.addAll(merchants) + merchantsSkip += MERCHANTS_QUERY_LIMIT + + val request = bitsyWebservice.getMerchants(merchantsSkip, MERCHANTS_QUERY_LIMIT) + request.enqueue(this) + } else { + updateMerchants(merchantsList) + + val now = System.currentTimeMillis() + PreferenceManager.getDefaultSharedPreferences(context).edit() + .putLong(Constants.KEY_MERCHANTS_LAST_UPDATE, now).apply() + } } } override fun onFailure(call: Call>, t: Throwable) { /* Do nothing */ } - private class insertAllAsyncTask internal constructor(private val mAsyncTaskDao: MerchantDao) : - AsyncTask, Void, Void>() { - - override fun doInBackground(vararg merchants: List): Void? { - mAsyncTaskDao.deleteAll() - mAsyncTaskDao.insertAll(merchants[0]) - return null + private fun updateMerchants(merchants: List) { + AsyncTask.execute { + mMerchantDao.deleteAll() + mMerchantDao.insertAll(merchants) } } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt index 1d8a399..3d80977 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt @@ -117,7 +117,7 @@ object Constants { const val KEY_TELLERS_LAST_UPDATE = "key_tellers_last_update" /** Constant used to decide whether or not to update the tellers and merchants info from the webservice */ - const val MERCHANTS_UPDATE_PERIOD = 1000L * 60 * 60 + 24 // 1 day + const val MERCHANTS_UPDATE_PERIOD = 1000L * 60 * 60 + 3 // 3 hours /** Name of the external storage folder used to save files like PDF and CSV exports and Backups **/ const val EXTERNAL_STORAGE_FOLDER = "BiTSy"