Use the NetworkService's GetAssets call to obtain the information of missing Assets and store it into the database, this enables us to remove the DatabaseLoad Activity and new assets information is downloaded as needed.

This commit is contained in:
Severiano Jaramillo 2018-12-19 20:25:58 -06:00
parent 13662c29d5
commit 49f920877a
2 changed files with 36 additions and 14 deletions

View file

@ -15,6 +15,7 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import cy.agorise.bitsybitshareswallet.database.entities.Balance import cy.agorise.bitsybitshareswallet.database.entities.Balance
import cy.agorise.bitsybitshareswallet.processors.TransfersLoader import cy.agorise.bitsybitshareswallet.processors.TransfersLoader
import cy.agorise.bitsybitshareswallet.repositories.AssetRepository
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
import cy.agorise.bitsybitshareswallet.utils.Constants import cy.agorise.bitsybitshareswallet.utils.Constants
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel
@ -25,16 +26,12 @@ import cy.agorise.graphenej.UserAccount
import cy.agorise.graphenej.api.ConnectionStatusUpdate import cy.agorise.graphenej.api.ConnectionStatusUpdate
import cy.agorise.graphenej.api.android.NetworkService import cy.agorise.graphenej.api.android.NetworkService
import cy.agorise.graphenej.api.android.RxBus import cy.agorise.graphenej.api.android.RxBus
import cy.agorise.graphenej.api.calls.GetAccountBalances import cy.agorise.graphenej.api.calls.*
import cy.agorise.graphenej.api.calls.GetAccounts
import cy.agorise.graphenej.api.calls.GetFullAccounts
import cy.agorise.graphenej.api.calls.GetObjects
import cy.agorise.graphenej.models.AccountProperties import cy.agorise.graphenej.models.AccountProperties
import cy.agorise.graphenej.models.FullAccountDetails import cy.agorise.graphenej.models.FullAccountDetails
import cy.agorise.graphenej.models.JsonRpcResponse import cy.agorise.graphenej.models.JsonRpcResponse
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
/** /**
@ -47,6 +44,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
private lateinit var mBalanceViewModel: BalanceViewModel private lateinit var mBalanceViewModel: BalanceViewModel
private lateinit var mBalanceRepository: BalanceRepository private lateinit var mBalanceRepository: BalanceRepository
private lateinit var mAssetRepository: AssetRepository
/* Current user account */ /* Current user account */
protected var mCurrentAccount: UserAccount? = null protected var mCurrentAccount: UserAccount? = null
@ -59,7 +57,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
private var storedOpCount: Long = -1 private var storedOpCount: Long = -1
private var missingUserAccounts = ArrayList<UserAccount>() private var missingUserAccounts = ArrayList<UserAccount>()
private var missingAssets = ArrayList<String>() private var missingAssets = ArrayList<Asset>()
/* Network service connection */ /* Network service connection */
protected var mNetworkService: NetworkService? = null protected var mNetworkService: NetworkService? = null
@ -78,6 +76,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
mCurrentAccount = UserAccount(userId) mCurrentAccount = UserAccount(userId)
mBalanceRepository = BalanceRepository(this) mBalanceRepository = BalanceRepository(this)
mAssetRepository = AssetRepository(this)
// Configure UserAccountViewModel to obtain the missing account ids // Configure UserAccountViewModel to obtain the missing account ids
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java) mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
@ -98,7 +97,8 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
mBalanceViewModel.getMissingAssetIds().observe(this, Observer<List<String>>{ assetIds -> mBalanceViewModel.getMissingAssetIds().observe(this, Observer<List<String>>{ assetIds ->
if (assetIds.isNotEmpty()) { if (assetIds.isNotEmpty()) {
missingAssets.clear() missingAssets.clear()
missingAssets.addAll(assetIds) for (assetId in assetIds)
missingAssets.add(Asset(assetId))
mHandler.postDelayed(mRequestMissingAssetsTask, Constants.NETWORK_SERVICE_RETRY_PERIOD) mHandler.postDelayed(mRequestMissingAssetsTask, Constants.NETWORK_SERVICE_RETRY_PERIOD)
} }
@ -165,6 +165,10 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
} }
} }
/**
* Receives a list of missing [AccountProperties] from which it extracts the required information to
* create a list of BiTSy's UserAccount objects and stores them into the database
*/
private fun handleAccountProperties(accountPropertiesList: List<AccountProperties>) { private fun handleAccountProperties(accountPropertiesList: List<AccountProperties>) {
val userAccounts = ArrayList<cy.agorise.bitsybitshareswallet.database.entities.UserAccount>() val userAccounts = ArrayList<cy.agorise.bitsybitshareswallet.database.entities.UserAccount>()
@ -198,8 +202,27 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
mBalanceRepository.insertAll(balances) mBalanceRepository.insertAll(balances)
} }
private fun handleAssets(assets: List<Asset>) { /**
Log.d(TAG, "handleAssets") * Receives a list of missing [Asset] from which it extracts the required information to
* create a list of BiTSy's Asset objects and stores them into the database
*/
private fun handleAssets(_assets: List<Asset>) {
val assets = ArrayList<cy.agorise.bitsybitshareswallet.database.entities.Asset>()
for (_asset in _assets) {
val asset = cy.agorise.bitsybitshareswallet.database.entities.Asset(
_asset.objectId,
_asset.symbol,
_asset.precision,
_asset.description ?: "",
_asset.bitassetId ?: ""
)
assets.add(asset)
}
mAssetRepository.insertAll(assets)
missingAssets.clear()
} }
private fun updateBalances() { private fun updateBalances() {
@ -230,8 +253,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
private val mRequestMissingAssetsTask = object : Runnable { private val mRequestMissingAssetsTask = object : Runnable {
override fun run() { override fun run() {
if (mNetworkService!!.isConnected) { if (mNetworkService!!.isConnected) {
// TODO use GetAssets to obtain the missing assets and save them into the db mNetworkService!!.sendMessage(GetAssets(missingAssets), GetAssets.REQUIRED_API)
mNetworkService!!.sendMessage(GetObjects(missingAssets), GetAccounts.REQUIRED_API)
} else if (missingAssets.isNotEmpty()){ } else if (missingAssets.isNotEmpty()){
mHandler.postDelayed(this, Constants.NETWORK_SERVICE_RETRY_PERIOD) mHandler.postDelayed(this, Constants.NETWORK_SERVICE_RETRY_PERIOD)
} }

View file

@ -1,18 +1,18 @@
package cy.agorise.bitsybitshareswallet.repositories package cy.agorise.bitsybitshareswallet.repositories
import android.app.Application import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import cy.agorise.bitsybitshareswallet.database.daos.AssetDao import cy.agorise.bitsybitshareswallet.database.daos.AssetDao
import cy.agorise.bitsybitshareswallet.database.BitsyDatabase import cy.agorise.bitsybitshareswallet.database.BitsyDatabase
import cy.agorise.bitsybitshareswallet.database.entities.Asset import cy.agorise.bitsybitshareswallet.database.entities.Asset
class AssetRepository internal constructor(application: Application) { class AssetRepository internal constructor(context: Context) {
private val mAssetDao: AssetDao private val mAssetDao: AssetDao
init { init {
val db = BitsyDatabase.getDatabase(application) val db = BitsyDatabase.getDatabase(context)
mAssetDao = db!!.assetDao() mAssetDao = db!!.assetDao()
} }