- Add method to update account balances.
- Create BalancesRepository to handle Balances related db operations.
This commit is contained in:
parent
51363bdf00
commit
fc98208c30
3 changed files with 97 additions and 2 deletions
|
@ -2,23 +2,40 @@ package cy.agorise.bitsybitshareswallet.activities
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.preference.PreferenceManager
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
import androidx.collection.LongSparseArray
|
||||||
import com.google.android.material.bottomnavigation.BottomNavigationView
|
import com.google.android.material.bottomnavigation.BottomNavigationView
|
||||||
import cy.agorise.bitsybitshareswallet.R
|
import cy.agorise.bitsybitshareswallet.R
|
||||||
|
import cy.agorise.bitsybitshareswallet.entities.Balance
|
||||||
import cy.agorise.bitsybitshareswallet.fragments.BalancesFragment
|
import cy.agorise.bitsybitshareswallet.fragments.BalancesFragment
|
||||||
import cy.agorise.bitsybitshareswallet.fragments.MerchantsFragment
|
import cy.agorise.bitsybitshareswallet.fragments.MerchantsFragment
|
||||||
import cy.agorise.bitsybitshareswallet.processors.TransfersLoader
|
import cy.agorise.bitsybitshareswallet.processors.TransfersLoader
|
||||||
|
import cy.agorise.bitsybitshareswallet.repositories.BalanceRepository
|
||||||
|
import cy.agorise.bitsybitshareswallet.utils.Constants
|
||||||
|
import cy.agorise.graphenej.AssetAmount
|
||||||
|
import cy.agorise.graphenej.RPC
|
||||||
|
import cy.agorise.graphenej.UserAccount
|
||||||
import cy.agorise.graphenej.api.ApiAccess
|
import cy.agorise.graphenej.api.ApiAccess
|
||||||
import cy.agorise.graphenej.api.ConnectionStatusUpdate
|
import cy.agorise.graphenej.api.ConnectionStatusUpdate
|
||||||
|
import cy.agorise.graphenej.api.calls.GetAccountBalances
|
||||||
import cy.agorise.graphenej.models.JsonRpcResponse
|
import cy.agorise.graphenej.models.JsonRpcResponse
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
|
import java.util.ArrayList
|
||||||
|
|
||||||
class MainActivity : ConnectedActivity() {
|
class MainActivity : ConnectedActivity() {
|
||||||
private val TAG = this.javaClass.simpleName
|
private val TAG = this.javaClass.simpleName
|
||||||
|
|
||||||
|
private val requestMap = LongSparseArray<String>()
|
||||||
|
|
||||||
|
/* Current user account */
|
||||||
|
private var mCurrentAccount: UserAccount? = null
|
||||||
|
|
||||||
|
private var mBalanceRepository: BalanceRepository? = null
|
||||||
|
|
||||||
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
|
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
|
||||||
when (item.itemId) {
|
when (item.itemId) {
|
||||||
R.id.navigation_receive -> {
|
R.id.navigation_receive -> {
|
||||||
|
@ -47,6 +64,13 @@ class MainActivity : ConnectedActivity() {
|
||||||
|
|
||||||
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
|
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
|
||||||
navigation.selectedItemId = R.id.navigation_balances
|
navigation.selectedItemId = R.id.navigation_balances
|
||||||
|
|
||||||
|
val userId = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
.getString(Constants.KEY_CURRENT_ACCOUNT_ID, "")
|
||||||
|
if (userId != "")
|
||||||
|
mCurrentAccount = UserAccount(userId)
|
||||||
|
|
||||||
|
mBalanceRepository = BalanceRepository(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadBalancesFragment() {
|
private fun loadBalancesFragment() {
|
||||||
|
@ -79,7 +103,9 @@ class MainActivity : ConnectedActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) {
|
override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) {
|
||||||
|
if (requestMap.get(response.id) == RPC.CALL_GET_ACCOUNT_BALANCES) {
|
||||||
|
handleBalanceUpdate(response as JsonRpcResponse<List<AssetAmount>>)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,8 +126,38 @@ class MainActivity : ConnectedActivity() {
|
||||||
Log.d(TAG, "ConnectionStatusUpdate: API_NETWORK_BROADCAST")
|
Log.d(TAG, "ConnectionStatusUpdate: API_NETWORK_BROADCAST")
|
||||||
// Instantiating this loader is enough to kick-start the transfers loading procedure
|
// Instantiating this loader is enough to kick-start the transfers loading procedure
|
||||||
TransfersLoader(this, lifecycle)
|
TransfersLoader(this, lifecycle)
|
||||||
|
|
||||||
|
updateBalances()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun updateBalances() {
|
||||||
|
if (mNetworkService!!.isConnected) {
|
||||||
|
val id = mNetworkService!!.sendMessage(
|
||||||
|
GetAccountBalances(mCurrentAccount, ArrayList()),
|
||||||
|
GetAccountBalances.REQUIRED_API
|
||||||
|
)
|
||||||
|
requestMap.put(id, RPC.CALL_GET_ACCOUNT_BALANCES)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun handleBalanceUpdate(response: JsonRpcResponse<List<AssetAmount>>) {
|
||||||
|
Log.d(TAG, "handleBalanceUpdate")
|
||||||
|
val now = System.currentTimeMillis() / 1000
|
||||||
|
val assetBalances = response.result
|
||||||
|
val balances = ArrayList<Balance>()
|
||||||
|
for (assetBalance in assetBalances) {
|
||||||
|
val balance = Balance(
|
||||||
|
mCurrentAccount!!.objectId,
|
||||||
|
assetBalance.asset.objectId,
|
||||||
|
assetBalance.amount.toLong(),
|
||||||
|
now
|
||||||
|
)
|
||||||
|
|
||||||
|
balances.add(balance)
|
||||||
|
}
|
||||||
|
mBalanceRepository!!.insertAll(balances)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cy.agorise.bitsybitshareswallet.daos
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
import androidx.room.Insert
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
import androidx.room.Query
|
import androidx.room.Query
|
||||||
import cy.agorise.bitsybitshareswallet.entities.Balance
|
import cy.agorise.bitsybitshareswallet.entities.Balance
|
||||||
|
|
||||||
|
@ -11,6 +12,9 @@ interface BalanceDao {
|
||||||
@Insert
|
@Insert
|
||||||
fun insert(balance: Balance)
|
fun insert(balance: Balance)
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insertAll(balances: List<Balance>)
|
||||||
|
|
||||||
@Query("SELECT * FROM balances")
|
@Query("SELECT * FROM balances")
|
||||||
fun getAllBalances(): LiveData<List<Balance>>
|
fun getAll(): LiveData<List<Balance>>
|
||||||
}
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
package cy.agorise.bitsybitshareswallet.repositories
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.AsyncTask
|
||||||
|
import androidx.lifecycle.LiveData
|
||||||
|
import cy.agorise.bitsybitshareswallet.daos.BalanceDao
|
||||||
|
import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase
|
||||||
|
import cy.agorise.bitsybitshareswallet.entities.Balance
|
||||||
|
|
||||||
|
class BalanceRepository internal constructor(context: Context) {
|
||||||
|
|
||||||
|
private val mBalanceDao: BalanceDao
|
||||||
|
|
||||||
|
init {
|
||||||
|
val db = BitsyDatabase.getDatabase(context)
|
||||||
|
mBalanceDao = db!!.balanceDao()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun insertAll(balances: List<Balance>) {
|
||||||
|
insertAllAsyncTask(mBalanceDao).execute(balances)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getAll(): LiveData<List<Balance>> {
|
||||||
|
return mBalanceDao.getAll()
|
||||||
|
}
|
||||||
|
|
||||||
|
private class insertAllAsyncTask internal constructor(private val mAsyncTaskDao: BalanceDao) :
|
||||||
|
AsyncTask<List<Balance>, Void, Void>() {
|
||||||
|
|
||||||
|
override fun doInBackground(vararg transfers: List<Balance>): Void? {
|
||||||
|
mAsyncTaskDao.insertAll(transfers[0])
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue