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 55e60fe..fdb9083 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 @@ -1,8 +1,10 @@ package cy.agorise.bitsybitshareswallet.database.daos +import androidx.lifecycle.LiveData import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy +import androidx.room.Query import cy.agorise.bitsybitshareswallet.database.entities.Merchant @Dao @@ -12,4 +14,7 @@ interface MerchantDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(merchants: List) + + @Query("SELECT * FROM merchants") + fun getAll(): LiveData> } \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt index 0ae2bbe..f316302 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/fragments/MerchantsFragment.kt @@ -12,17 +12,12 @@ import android.view.ViewGroup import com.google.android.gms.maps.GoogleMap import com.google.android.gms.maps.OnMapReadyCallback import com.google.android.gms.maps.SupportMapFragment -import com.google.gson.GsonBuilder import cy.agorise.bitsybitshareswallet.R -import cy.agorise.bitsybitshareswallet.network.MerchantsWebservice -import cy.agorise.bitsybitshareswallet.network.FeathersResponse -import retrofit2.Call -import retrofit2.Response -import retrofit2.Retrofit -import retrofit2.converter.gson.GsonConverterFactory import android.preference.PreferenceManager import androidx.core.content.ContextCompat +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProviders import com.google.android.gms.maps.CameraUpdateFactory import com.google.android.gms.maps.model.* import com.google.maps.android.clustering.Cluster @@ -31,10 +26,11 @@ import cy.agorise.bitsybitshareswallet.database.entities.Merchant import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.MerchantMarkerRenderer import cy.agorise.bitsybitshareswallet.utils.toast +import cy.agorise.bitsybitshareswallet.viewmodels.MerchantViewModel import java.lang.Exception -class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback>, +class MerchantsFragment : Fragment(), OnMapReadyCallback, ClusterManager.OnClusterClickListener, ClusterManager.OnClusterItemClickListener, ClusterManager.OnClusterItemInfoWindowClickListener{ @@ -48,6 +44,8 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback? = null override fun onCreateView( @@ -63,6 +61,8 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback(MerchantsWebservice::class.java) - val call = ambassadorService.getMerchants(0) - call.enqueue(this) + mMerchantViewModel.getAllMerchants().observe(this, Observer> {merchants -> + mClusterManager?.clearItems() + mClusterManager?.addItems(merchants) + mClusterManager?.cluster() + }) } private fun applyMapTheme() { @@ -155,19 +149,6 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, retrofit2.Callback>, response: Response>) { - if (response.isSuccessful) { - val res: FeathersResponse? = response.body() - val merchants = res?.data ?: return - mClusterManager?.addItems(merchants) - mClusterManager?.cluster() - } else { - Log.e("error_bitsy", response.errorBody()?.string()) - } - } - - override fun onFailure(call: Call>, t: Throwable) { /* Do nothing */ } - /** * Animates the camera update to focus on an area that shows all the items from the cluster that was tapped. */ 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 8eb8cc8..bf8f91f 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/network/MerchantsWebservice.kt @@ -9,7 +9,9 @@ import retrofit2.http.Query interface MerchantsWebservice { @GET("/api/v1/merchants") - fun getMerchants(@Query(value = "\$skip") skip: Int): Call> + fun getMerchants(@Query(value = "\$skip") skip: Int, + @Query(value = "\$limit") limit: Int = 50): + Call> @GET("api/v2/tellers") fun getTellers(@Query(value = "\$skip") skip: Int): Call> diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt new file mode 100644 index 0000000..9439679 --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/MerchantRepository.kt @@ -0,0 +1,64 @@ +package cy.agorise.bitsybitshareswallet.repositories + +import android.content.Context +import android.os.AsyncTask +import androidx.lifecycle.LiveData +import cy.agorise.bitsybitshareswallet.database.BitsyDatabase +import cy.agorise.bitsybitshareswallet.database.daos.MerchantDao +import cy.agorise.bitsybitshareswallet.database.entities.Merchant +import cy.agorise.bitsybitshareswallet.network.FeathersResponse +import cy.agorise.bitsybitshareswallet.network.MerchantsWebservice +import cy.agorise.bitsybitshareswallet.utils.Constants +import retrofit2.Call +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory + +class MerchantRepository internal constructor(context: Context) : retrofit2.Callback> { + + private val mMerchantDao: MerchantDao + + init { + val db = BitsyDatabase.getDatabase(context) + mMerchantDao = db!!.merchantDao() + } + + /** + * Returns a LiveData object directly from the database while the response from the WebService is obtained. + */ + fun getAll(): LiveData> { + refreshMerchants() + return mMerchantDao.getAll() + } + + private fun refreshMerchants() { + 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>) { + if (response.isSuccessful) { + val res: FeathersResponse? = response.body() + val merchants = res?.data ?: return + insertAllAsyncTask(mMerchantDao).execute(merchants) + } + } + + 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? { + // TODO Delete all first + mAsyncTaskDao.insertAll(merchants[0]) + return null + } + } +} \ No newline at end of file diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/MerchantViewModel.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/MerchantViewModel.kt new file mode 100644 index 0000000..edb555d --- /dev/null +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/viewmodels/MerchantViewModel.kt @@ -0,0 +1,15 @@ +package cy.agorise.bitsybitshareswallet.viewmodels + +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import cy.agorise.bitsybitshareswallet.database.entities.Merchant +import cy.agorise.bitsybitshareswallet.repositories.MerchantRepository + +class MerchantViewModel(application: Application) : AndroidViewModel(application) { + private var mMerchantRepository = MerchantRepository(application) + + internal fun getAllMerchants(): LiveData> { + return mMerchantRepository.getAll() + } +} \ No newline at end of file