- Add Room RxJava dependency to be able to use RxJava in Room db queries.

- Use RxJava's Single to make a Room query in TransfersLoader.
This commit is contained in:
Severiano Jaramillo 2018-11-27 21:42:39 -06:00
parent 08cf6b3c40
commit cec0857113
6 changed files with 57 additions and 14 deletions

View file

@ -45,6 +45,8 @@ dependencies {
implementation "androidx.room:room-runtime:$room_version" implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version" // RxJava support for Room
implementation 'org.bitcoinj:bitcoinj-core:0.14.3' implementation 'org.bitcoinj:bitcoinj-core:0.14.3'
implementation 'com.moldedbits.r2d2:r2d2:1.0.1' implementation 'com.moldedbits.r2d2:r2d2:1.0.1'

View file

@ -6,13 +6,16 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.widget.Toast import android.widget.Toast
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.R
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.graphenej.api.ApiAccess
import cy.agorise.graphenej.api.ConnectionStatusUpdate
import cy.agorise.graphenej.models.JsonRpcResponse
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : ConnectedActivity() {
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) { when (item.itemId) {
@ -72,4 +75,35 @@ class MainActivity : AppCompatActivity() {
super.onOptionsItemSelected(item) super.onOptionsItemSelected(item)
} }
} }
override fun handleJsonRpcResponse(response: JsonRpcResponse<*>) {
}
/**
* Private method called whenever there's an update to the connection status
* @param connectionStatusUpdate Connection status update.
*/
override fun handleConnectionStatusUpdate(connectionStatusUpdate: ConnectionStatusUpdate) {
when (connectionStatusUpdate.updateCode) {
ConnectionStatusUpdate.CONNECTED -> {
// Instantiating this loader is enough to kick-start the transfers loading procedure
TransfersLoader(this, lifecycle)
}
ConnectionStatusUpdate.DISCONNECTED -> {
// Do nothing for now
}
ConnectionStatusUpdate.AUTHENTICATED -> {}//updateBalances() }
ConnectionStatusUpdate.API_UPDATE -> {
// In certain cases the information about the accounts is not complete, this may not be the best
// solution but at least it works. Feel free to improve it or move it to a better place
//MissingAccountsLoader(this, lifecycle)
if (connectionStatusUpdate.api == ApiAccess.API_DATABASE) {
// Updating transfer and exchange costs in all possible input assets
//updateCosts()
}
}
}
}
} }

View file

@ -6,6 +6,7 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import cy.agorise.bitsybitshareswallet.entities.Transfer import cy.agorise.bitsybitshareswallet.entities.Transfer
import io.reactivex.Single
@Dao @Dao
interface TransferDao { interface TransferDao {
@ -17,7 +18,7 @@ interface TransferDao {
fun insertAll(transfers: List<Transfer>) fun insertAll(transfers: List<Transfer>)
@Query("SELECT COUNT(*) FROM transfers") @Query("SELECT COUNT(*) FROM transfers")
fun getCount(): Int fun getCount(): Single<Int>
@Query("SELECT * FROM transfers") @Query("SELECT * FROM transfers")
fun getAllTransfers(): LiveData<List<Transfer>> fun getAllTransfers(): LiveData<List<Transfer>>

View file

@ -8,7 +8,7 @@ import androidx.room.PrimaryKey
@Entity(tableName = "equivalent_values",foreignKeys = @Entity(tableName = "equivalent_values",foreignKeys =
[ForeignKey( [ForeignKey(
entity = Transfer::class, entity = Transfer::class,
parentColumns = ["operation_id"], parentColumns = ["id"],
childColumns = ["transfer_id"] childColumns = ["transfer_id"]
), ForeignKey( ), ForeignKey(
entity = Asset::class, entity = Asset::class,

View file

@ -27,6 +27,7 @@ import cy.agorise.graphenej.operations.TransferOperation
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import org.bitcoinj.core.DumpedPrivateKey import org.bitcoinj.core.DumpedPrivateKey
import org.bitcoinj.core.ECKey import org.bitcoinj.core.ECKey
import java.util.* import java.util.*
@ -218,7 +219,10 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
// If we are in debug mode, we first erase all entries in the 'transfer' table // If we are in debug mode, we first erase all entries in the 'transfer' table
transferRepository!!.deleteAll() transferRepository!!.deleteAll()
} }
val transferCount = transferRepository!!.getCount() val disposable = transferRepository!!.getCount()
.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe { transferCount ->
if (transferCount > 0) { if (transferCount > 0) {
// If we already have some transfers in the database, we might want to skip the request // If we already have some transfers in the database, we might want to skip the request
// straight to the last batch // straight to the last batch
@ -227,6 +231,7 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
// Retrieving account transactions // Retrieving account transactions
loadNextOperationsBatch() loadNextOperationsBatch()
} }
}
/** /**
* Handles a freshly obtained list of OperationHistory instances. This is how the full node * Handles a freshly obtained list of OperationHistory instances. This is how the full node
@ -241,7 +246,7 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li
val insertedCount = transferRepository!!.insertAll(processOperationList(operationHistoryList)) val insertedCount = transferRepository!!.insertAll(processOperationList(operationHistoryList))
// Log.d(TAG, String.format("Inserted count: %d, list size: %d", insertedCount, operationHistoryList.size)) // Log.d(TAG, String.format("Inserted count: %d, list size: %d", insertedCount, operationHistoryList.size))
if (operationHistoryList.isEmpty()) { if (/* insertedCount == 0 && */ operationHistoryList.isEmpty()) {
// We finally reached the end of the transactions, so now we must check for missing // We finally reached the end of the transactions, so now we must check for missing
// transfer times // transfer times
// mState = State.LOADING_MISSING_TIMES // mState = State.LOADING_MISSING_TIMES

View file

@ -5,6 +5,7 @@ import android.os.AsyncTask
import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase
import cy.agorise.bitsybitshareswallet.daos.TransferDao import cy.agorise.bitsybitshareswallet.daos.TransferDao
import cy.agorise.bitsybitshareswallet.entities.Transfer import cy.agorise.bitsybitshareswallet.entities.Transfer
import io.reactivex.Single
class TransferRepository internal constructor(context: Context) { class TransferRepository internal constructor(context: Context) {
@ -19,7 +20,7 @@ class TransferRepository internal constructor(context: Context) {
insertAllAsyncTask(mTransferDao).execute(transfers) insertAllAsyncTask(mTransferDao).execute(transfers)
} }
fun getCount(): Int { fun getCount(): Single<Int> {
return mTransferDao.getCount() return mTransferDao.getCount()
} }