Add BalanceView model and a method into it that is always observing the database and when it detects that there is missing assets information creates a list of such missing assets and fires an event to obtain the said information.
This commit is contained in:
parent
5c20e2470d
commit
720b4f7b1e
4 changed files with 72 additions and 5 deletions
|
@ -17,7 +17,9 @@ import cy.agorise.bitsybitshareswallet.database.entities.Balance
|
|||
import cy.agorise.bitsybitshareswallet.processors.TransfersLoader
|
||||
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
|
||||
import cy.agorise.bitsybitshareswallet.utils.Constants
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.BalanceViewModel
|
||||
import cy.agorise.bitsybitshareswallet.viewmodels.UserAccountViewModel
|
||||
import cy.agorise.graphenej.Asset
|
||||
import cy.agorise.graphenej.AssetAmount
|
||||
import cy.agorise.graphenej.UserAccount
|
||||
import cy.agorise.graphenej.api.ConnectionStatusUpdate
|
||||
|
@ -26,12 +28,14 @@ import cy.agorise.graphenej.api.android.RxBus
|
|||
import cy.agorise.graphenej.api.calls.GetAccountBalances
|
||||
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.FullAccountDetails
|
||||
import cy.agorise.graphenej.models.JsonRpcResponse
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.Disposable
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
/**
|
||||
* Class in charge of managing the connection to graphenej's NetworkService
|
||||
|
@ -40,8 +44,9 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
private val TAG = this.javaClass.simpleName
|
||||
|
||||
private lateinit var mUserAccountViewModel: UserAccountViewModel
|
||||
private lateinit var mBalanceViewModel: BalanceViewModel
|
||||
|
||||
private var mBalanceRepository: BalanceRepository? = null
|
||||
private lateinit var mBalanceRepository: BalanceRepository
|
||||
|
||||
/* Current user account */
|
||||
protected var mCurrentAccount: UserAccount? = null
|
||||
|
@ -54,6 +59,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
private var storedOpCount: Long = -1
|
||||
|
||||
private var missingUserAccounts = ArrayList<UserAccount>()
|
||||
private var missingAssets = ArrayList<String>()
|
||||
|
||||
/* Network service connection */
|
||||
protected var mNetworkService: NetworkService? = null
|
||||
|
@ -73,11 +79,12 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
|
||||
mBalanceRepository = BalanceRepository(this)
|
||||
|
||||
// Configure UserAccountViewModel to show the current account
|
||||
// Configure UserAccountViewModel to obtain the missing account ids
|
||||
mUserAccountViewModel = ViewModelProviders.of(this).get(UserAccountViewModel::class.java)
|
||||
|
||||
mUserAccountViewModel.getMissingUserAccountIds().observe(this, Observer<List<String>>{ userAccountIds ->
|
||||
if (userAccountIds.isNotEmpty()) {
|
||||
missingUserAccounts.clear()
|
||||
for (userAccountId in userAccountIds)
|
||||
missingUserAccounts.add(UserAccount(userAccountId))
|
||||
|
||||
|
@ -85,6 +92,18 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
}
|
||||
})
|
||||
|
||||
// Configure UserAccountViewModel to obtain the missing account ids
|
||||
mBalanceViewModel = ViewModelProviders.of(this).get(BalanceViewModel::class.java)
|
||||
|
||||
mBalanceViewModel.getMissingAssetIds().observe(this, Observer<List<String>>{ assetIds ->
|
||||
if (assetIds.isNotEmpty()) {
|
||||
missingAssets.clear()
|
||||
missingAssets.addAll(assetIds)
|
||||
|
||||
mHandler.postDelayed(mRequestMissingAssetsTask, Constants.NETWORK_SERVICE_RETRY_PERIOD)
|
||||
}
|
||||
})
|
||||
|
||||
mDisposable = RxBus.getBusInstance()
|
||||
.asFlowable()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
|
@ -101,6 +120,8 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
handleAccountProperties(message.result as List<AccountProperties>)
|
||||
} else if ((message.result as List<*>)[0] is AssetAmount) {
|
||||
handleBalanceUpdate(message.result as List<AssetAmount>)
|
||||
} else if ((message.result as List<*>)[0] is Asset) {
|
||||
handleAssets(message.result as List<Asset>)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -174,7 +195,11 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
|
||||
balances.add(balance)
|
||||
}
|
||||
mBalanceRepository!!.insertAll(balances)
|
||||
mBalanceRepository.insertAll(balances)
|
||||
}
|
||||
|
||||
private fun handleAssets(assets: List<Asset>) {
|
||||
Log.d(TAG, "handleAssets")
|
||||
}
|
||||
|
||||
private fun updateBalances() {
|
||||
|
@ -187,7 +212,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
}
|
||||
|
||||
/**
|
||||
* Task used to obtain the missing UserAccounts.
|
||||
* Task used to obtain the missing UserAccounts from Graphenej's NetworkService.
|
||||
*/
|
||||
private val mRequestMissingUserAccountsTask = object : Runnable {
|
||||
override fun run() {
|
||||
|
@ -199,6 +224,20 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Task used to obtain the missing Assets from Graphenej's NetworkService.
|
||||
*/
|
||||
private val mRequestMissingAssetsTask = object : Runnable {
|
||||
override fun run() {
|
||||
if (mNetworkService!!.isConnected) {
|
||||
// TODO use GetAssets to obtain the missing assets and save them into the db
|
||||
mNetworkService!!.sendMessage(GetObjects(missingAssets), GetAccounts.REQUIRED_API)
|
||||
} else if (missingAssets.isNotEmpty()){
|
||||
mHandler.postDelayed(this, Constants.NETWORK_SERVICE_RETRY_PERIOD)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Task used to perform a redundant payment check.
|
||||
*/
|
||||
|
@ -237,6 +276,7 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
|
|||
}
|
||||
mHandler.removeCallbacks(mCheckMissingPaymentsTask)
|
||||
mHandler.removeCallbacks(mRequestMissingUserAccountsTask)
|
||||
mHandler.removeCallbacks(mRequestMissingAssetsTask)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
|
|
@ -17,4 +17,8 @@ interface BalanceDao {
|
|||
|
||||
@Query("SELECT * FROM balances")
|
||||
fun getAll(): LiveData<List<Balance>>
|
||||
|
||||
// TODO not sure if this is the best place for this query as it involves two entities
|
||||
@Query("SELECT DISTINCT asset_id FROM balances WHERE asset_id NOT IN (SELECT id FROM assets)")
|
||||
fun getMissingAssetIds(): LiveData<List<String>>
|
||||
}
|
|
@ -24,6 +24,10 @@ class BalanceRepository internal constructor(context: Context) {
|
|||
return mBalanceDao.getAll()
|
||||
}
|
||||
|
||||
fun getMissingAssetIds(): LiveData<List<String>> {
|
||||
return mBalanceDao.getMissingAssetIds()
|
||||
}
|
||||
|
||||
private class insertAllAsyncTask internal constructor(private val mAsyncTaskDao: BalanceDao) :
|
||||
AsyncTask<List<Balance>, Void, Void>() {
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package cy.agorise.bitsybitshareswallet.viewmodels
|
||||
|
||||
import android.app.Application
|
||||
import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.LiveData
|
||||
import cy.agorise.bitsybitshareswallet.database.entities.Balance
|
||||
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
|
||||
|
||||
class BalanceViewModel(application: Application) : AndroidViewModel(application) {
|
||||
private var mRepository = BalanceRepository(application)
|
||||
|
||||
internal fun getMissingAssetIds(): LiveData<List<String>> {
|
||||
return mRepository.getMissingAssetIds()
|
||||
}
|
||||
|
||||
fun insertAll(balances: List<Balance>) {
|
||||
mRepository.insertAll(balances)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue