From 38d6d7f7a8f9dca43158c73a00c6a1aa4f1a4a1e Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Wed, 23 Jan 2019 16:08:04 -0600 Subject: [PATCH] Improve MerchantRepository to perform merchants update only if at least one day has passed since the last update. Also, perform a full delete of merchants before inserting the list received from thewebservice to make sure merchants removed there are also removed from the app. --- .../database/daos/MerchantDao.kt | 3 ++ .../network/MerchantsWebservice.kt | 4 ++- .../repositories/MerchantRepository.kt | 35 ++++++++++++------- .../bitsybitshareswallet/utils/Constants.kt | 8 +++++ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/daos/MerchantDao.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/daos/MerchantDao.kt index fdb9083..2476684 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/database/daos/MerchantDao.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/database/daos/MerchantDao.kt @@ -17,4 +17,7 @@ interface MerchantDao { @Query("SELECT * FROM merchants") fun getAll(): LiveData> + + @Query("DELETE FROM merchants") + fun deleteAll() } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt index bf8f91f..f9b828a 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt @@ -14,5 +14,7 @@ interface MerchantsWebservice { Call> @GET("api/v2/tellers") - fun getTellers(@Query(value = "\$skip") skip: Int): Call> + fun getTellers(@Query(value = "\$skip") skip: Int, + @Query(value = "\$limit") limit: Int = 50): + Call> } \ No newline at end of file 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 9439679..a70966b 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt @@ -2,6 +2,7 @@ package cy.agorise.bitsybitshareswallet.repositories import android.content.Context import android.os.AsyncTask +import android.preference.PreferenceManager import androidx.lifecycle.LiveData import cy.agorise.bitsybitshareswallet.database.BitsyDatabase import cy.agorise.bitsybitshareswallet.database.daos.MerchantDao @@ -14,7 +15,7 @@ import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -class MerchantRepository internal constructor(context: Context) : retrofit2.Callback> { +class MerchantRepository internal constructor(val context: Context) : retrofit2.Callback> { private val mMerchantDao: MerchantDao @@ -23,23 +24,29 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call mMerchantDao = db!!.merchantDao() } - /** - * Returns a LiveData object directly from the database while the response from the WebService is obtained. - */ + /** Returns a LiveData object directly from the database while the response from the WebService is obtained. */ fun getAll(): LiveData> { refreshMerchants() return mMerchantDao.getAll() } + /** Refreshes the merchants information only if the MERCHANT_UPDATE_PERIOD has passed, otherwise it does nothing */ private fun refreshMerchants() { - val retrofit = Retrofit.Builder() - .baseUrl(Constants.MERCHANTS_WEBSERVICE_URL) - .addConverterFactory(GsonConverterFactory.create()) - .build() + val lastMerchantUpdate = PreferenceManager.getDefaultSharedPreferences(context) + .getLong(Constants.KEY_MERCHANTS_LAST_UPDATE, 0) - val ambassadorService = retrofit.create(MerchantsWebservice::class.java) - val call = ambassadorService.getMerchants(0) - call.enqueue(this) + val now = System.currentTimeMillis() + + if (lastMerchantUpdate + Constants.MERCHANTS_UPDATE_PERIOD < now) { + val retrofit = Retrofit.Builder() + .baseUrl(Constants.MERCHANTS_WEBSERVICE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + + val ambassadorService = retrofit.create(MerchantsWebservice::class.java) + val call = ambassadorService.getMerchants(0) + call.enqueue(this) + } } override fun onResponse(call: Call>, response: Response>) { @@ -47,6 +54,10 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call 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() } } @@ -56,7 +67,7 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call AsyncTask, Void, Void>() { override fun doInBackground(vararg merchants: List): Void? { - // TODO Delete all first + mAsyncTaskDao.deleteAll() mAsyncTaskDao.insertAll(merchants[0]) return null } 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 05d3041..4137f35 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/utils/Constants.kt @@ -87,4 +87,12 @@ object Constants { const val KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated" const val MERCHANTS_WEBSERVICE_URL = "https://websvc.palmpay.io/" + + /** Key used to store the last time in millis that the merchants info was refreshed */ + const val KEY_MERCHANTS_LAST_UPDATE = "key_merchants_last_update" + + /** Key used to store the last time in millis that the tellers info was refreshed */ + const val KEY_TELLERS_LAST_UPDATE = "key_tellers_last_update" + + const val MERCHANTS_UPDATE_PERIOD = 1000L * 60 * 60 + 24 // 1 day }