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.

This commit is contained in:
Severiano Jaramillo 2019-01-23 16:08:04 -06:00
parent 4bb53de009
commit 38d6d7f7a8
4 changed files with 37 additions and 13 deletions

View file

@ -17,4 +17,7 @@ interface MerchantDao {
@Query("SELECT * FROM merchants") @Query("SELECT * FROM merchants")
fun getAll(): LiveData<List<Merchant>> fun getAll(): LiveData<List<Merchant>>
@Query("DELETE FROM merchants")
fun deleteAll()
} }

View file

@ -14,5 +14,7 @@ interface MerchantsWebservice {
Call<FeathersResponse<Merchant>> Call<FeathersResponse<Merchant>>
@GET("api/v2/tellers") @GET("api/v2/tellers")
fun getTellers(@Query(value = "\$skip") skip: Int): Call<FeathersResponse<Teller>> fun getTellers(@Query(value = "\$skip") skip: Int,
@Query(value = "\$limit") limit: Int = 50):
Call<FeathersResponse<Teller>>
} }

View file

@ -2,6 +2,7 @@ package cy.agorise.bitsybitshareswallet.repositories
import android.content.Context import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import android.preference.PreferenceManager
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
import cy.agorise.bitsybitshareswallet.database.daos.MerchantDao import cy.agorise.bitsybitshareswallet.database.daos.MerchantDao
@ -14,7 +15,7 @@ import retrofit2.Response
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
class MerchantRepository internal constructor(context: Context) : retrofit2.Callback<FeathersResponse<Merchant>> { class MerchantRepository internal constructor(val context: Context) : retrofit2.Callback<FeathersResponse<Merchant>> {
private val mMerchantDao: MerchantDao private val mMerchantDao: MerchantDao
@ -23,15 +24,20 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call
mMerchantDao = db!!.merchantDao() 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<List<Merchant>> { fun getAll(): LiveData<List<Merchant>> {
refreshMerchants() refreshMerchants()
return mMerchantDao.getAll() return mMerchantDao.getAll()
} }
/** Refreshes the merchants information only if the MERCHANT_UPDATE_PERIOD has passed, otherwise it does nothing */
private fun refreshMerchants() { private fun refreshMerchants() {
val lastMerchantUpdate = PreferenceManager.getDefaultSharedPreferences(context)
.getLong(Constants.KEY_MERCHANTS_LAST_UPDATE, 0)
val now = System.currentTimeMillis()
if (lastMerchantUpdate + Constants.MERCHANTS_UPDATE_PERIOD < now) {
val retrofit = Retrofit.Builder() val retrofit = Retrofit.Builder()
.baseUrl(Constants.MERCHANTS_WEBSERVICE_URL) .baseUrl(Constants.MERCHANTS_WEBSERVICE_URL)
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
@ -41,12 +47,17 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call
val call = ambassadorService.getMerchants(0) val call = ambassadorService.getMerchants(0)
call.enqueue(this) call.enqueue(this)
} }
}
override fun onResponse(call: Call<FeathersResponse<Merchant>>, response: Response<FeathersResponse<Merchant>>) { override fun onResponse(call: Call<FeathersResponse<Merchant>>, response: Response<FeathersResponse<Merchant>>) {
if (response.isSuccessful) { if (response.isSuccessful) {
val res: FeathersResponse<Merchant>? = response.body() val res: FeathersResponse<Merchant>? = response.body()
val merchants = res?.data ?: return val merchants = res?.data ?: return
insertAllAsyncTask(mMerchantDao).execute(merchants) 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<List<Merchant>, Void, Void>() { AsyncTask<List<Merchant>, Void, Void>() {
override fun doInBackground(vararg merchants: List<Merchant>): Void? { override fun doInBackground(vararg merchants: List<Merchant>): Void? {
// TODO Delete all first mAsyncTaskDao.deleteAll()
mAsyncTaskDao.insertAll(merchants[0]) mAsyncTaskDao.insertAll(merchants[0])
return null return null
} }

View file

@ -87,4 +87,12 @@ object Constants {
const val KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated" const val KEY_NIGHT_MODE_ACTIVATED = "key_night_mode_activated"
const val MERCHANTS_WEBSERVICE_URL = "https://websvc.palmpay.io/" 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
} }