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")
fun getAll(): LiveData<List<Merchant>>
@Query("DELETE FROM merchants")
fun deleteAll()
}

View file

@ -14,5 +14,7 @@ interface MerchantsWebservice {
Call<FeathersResponse<Merchant>>
@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.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<FeathersResponse<Merchant>> {
class MerchantRepository internal constructor(val context: Context) : retrofit2.Callback<FeathersResponse<Merchant>> {
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<List<Merchant>> {
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>(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>(MerchantsWebservice::class.java)
val call = ambassadorService.getMerchants(0)
call.enqueue(this)
}
}
override fun onResponse(call: Call<FeathersResponse<Merchant>>, response: Response<FeathersResponse<Merchant>>) {
@ -47,6 +54,10 @@ class MerchantRepository internal constructor(context: Context) : retrofit2.Call
val res: FeathersResponse<Merchant>? = 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<List<Merchant>, Void, Void>() {
override fun doInBackground(vararg merchants: List<Merchant>): Void? {
// TODO Delete all first
mAsyncTaskDao.deleteAll()
mAsyncTaskDao.insertAll(merchants[0])
return null
}

View file

@ -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
}