diff --git a/app/build.gradle b/app/build.gradle index 6619da6..a45fff0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,6 +45,8 @@ dependencies { implementation "androidx.room:room-runtime:$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 'com.moldedbits.r2d2:r2d2:1.0.1' diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt index 60feda4..51e9424 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/activities/MainActivity.kt @@ -6,13 +6,16 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import com.google.android.material.bottomnavigation.BottomNavigationView -import androidx.appcompat.app.AppCompatActivity import cy.agorise.bitsybitshareswallet.R import cy.agorise.bitsybitshareswallet.fragments.BalancesFragment 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.* -class MainActivity : AppCompatActivity() { +class MainActivity : ConnectedActivity() { private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item -> when (item.itemId) { @@ -72,4 +75,35 @@ class MainActivity : AppCompatActivity() { 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() + } + } + } + } } diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/daos/TransferDao.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/daos/TransferDao.kt index 5edaf4d..3ca0ec2 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/daos/TransferDao.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/daos/TransferDao.kt @@ -6,6 +6,7 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import cy.agorise.bitsybitshareswallet.entities.Transfer +import io.reactivex.Single @Dao interface TransferDao { @@ -17,7 +18,7 @@ interface TransferDao { fun insertAll(transfers: List) @Query("SELECT COUNT(*) FROM transfers") - fun getCount(): Int + fun getCount(): Single @Query("SELECT * FROM transfers") fun getAllTransfers(): LiveData> diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/entities/EquivalentValue.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/entities/EquivalentValue.kt index a6fbe71..2ff4bd3 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/entities/EquivalentValue.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/entities/EquivalentValue.kt @@ -8,7 +8,7 @@ import androidx.room.PrimaryKey @Entity(tableName = "equivalent_values",foreignKeys = [ForeignKey( entity = Transfer::class, - parentColumns = ["operation_id"], + parentColumns = ["id"], childColumns = ["transfer_id"] ), ForeignKey( entity = Asset::class, diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/processors/TransfersLoader.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/processors/TransfersLoader.kt index 5a2801a..94cf9a5 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/processors/TransfersLoader.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/processors/TransfersLoader.kt @@ -27,6 +27,7 @@ import cy.agorise.graphenej.operations.TransferOperation import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.functions.Consumer +import io.reactivex.schedulers.Schedulers import org.bitcoinj.core.DumpedPrivateKey import org.bitcoinj.core.ECKey import java.util.* @@ -218,14 +219,18 @@ 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 transferRepository!!.deleteAll() } - val transferCount = transferRepository!!.getCount() - if (transferCount > 0) { - // If we already have some transfers in the database, we might want to skip the request - // straight to the last batch - historicalTransferCount = Math.floor((transferCount / HISTORICAL_TRANSFER_BATCH_SIZE).toDouble()).toInt() - } - // Retrieving account transactions - loadNextOperationsBatch() + val disposable = transferRepository!!.getCount() + .subscribeOn(Schedulers.computation()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe { transferCount -> + if (transferCount > 0) { + // If we already have some transfers in the database, we might want to skip the request + // straight to the last batch + historicalTransferCount = Math.floor((transferCount / HISTORICAL_TRANSFER_BATCH_SIZE).toDouble()).toInt() + } + // Retrieving account transactions + loadNextOperationsBatch() + } } /** @@ -241,7 +246,7 @@ class TransfersLoader(private var mContext: Context?, private val mLifeCycle: Li val insertedCount = transferRepository!!.insertAll(processOperationList(operationHistoryList)) // 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 // transfer times // mState = State.LOADING_MISSING_TIMES diff --git a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferRepository.kt b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferRepository.kt index e34c435..7f7f2f1 100644 --- a/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferRepository.kt +++ b/app/src/main/java/cy/agorise/bitsybitshareswallet/repositories/TransferRepository.kt @@ -5,6 +5,7 @@ import android.os.AsyncTask import cy.agorise.bitsybitshareswallet.daos.BitsyDatabase import cy.agorise.bitsybitshareswallet.daos.TransferDao import cy.agorise.bitsybitshareswallet.entities.Transfer +import io.reactivex.Single class TransferRepository internal constructor(context: Context) { @@ -19,7 +20,7 @@ class TransferRepository internal constructor(context: Context) { insertAllAsyncTask(mTransferDao).execute(transfers) } - fun getCount(): Int { + fun getCount(): Single { return mTransferDao.getCount() }