modified CryptoCoin Balance database to work as intended

Modified to be able to save and retrieve the bitsharesasset from database
This commit is contained in:
henry 2017-10-24 22:58:45 -04:00
parent e3629f5c5b
commit f80ac3b06d
5 changed files with 56 additions and 29 deletions

View file

@ -325,8 +325,8 @@ public abstract class GrapheneApiGenerator {
final CryptoCoinBalance balance = new CryptoCoinBalance(); final CryptoCoinBalance balance = new CryptoCoinBalance();
balance.setAccountId(accountId); balance.setAccountId(accountId);
balance.setBalance(((AccountBalanceUpdate) update).balance); balance.setBalance(((AccountBalanceUpdate) update).balance);
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type); BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(((AccountBalanceUpdate) update).asset_type);
if(assetInfo == null || assetInfo.getValue() == null){ if(assetInfo == null ){
final String assetType = ((AccountBalanceUpdate) update).asset_type; final String assetType = ((AccountBalanceUpdate) update).asset_type;
ArrayList<String> idAssets = new ArrayList<>(); ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(assetType); idAssets.add(assetType);
@ -335,9 +335,10 @@ public abstract class GrapheneApiGenerator {
public void success(Object answer, int idPetition) { public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){ if(answer instanceof BitsharesAsset){
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer);
cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer ); long cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer )[0];
info.setCryptoCurrencyId(cryptoCurrencyId);
bitsharesAssetDao.insertBitsharesAssetInfo(info); bitsharesAssetDao.insertBitsharesAssetInfo(info);
balance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(assetType).getValue().getCryptoCurrencyId()); balance.setCryptoCurrencyId(cryptoCurrencyId);
balanceDao.insertCryptoCoinBalance(balance); balanceDao.insertCryptoCoinBalance(balance);
} }
} }
@ -350,9 +351,8 @@ public abstract class GrapheneApiGenerator {
getAssetById(idAssets,getAssetRequest); getAssetById(idAssets,getAssetRequest);
}else { }else {
balance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId()); balance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(balance); balanceDao.insertCryptoCoinBalance(balance);
//TODO balance function
} }
BitsharesAccountManager.refreshAccountTransactions(accountId,context); BitsharesAccountManager.refreshAccountTransactions(accountId,context);
@ -393,32 +393,33 @@ public abstract class GrapheneApiGenerator {
final CryptoCoinBalance ccBalance = new CryptoCoinBalance(); final CryptoCoinBalance ccBalance = new CryptoCoinBalance();
ccBalance.setAccountId(accountId); ccBalance.setAccountId(accountId);
ccBalance.setBalance(balance.getAmount().longValue()); ccBalance.setBalance(balance.getAmount().longValue());
LiveData<BitsharesAssetInfo> assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()); BitsharesAssetInfo assetInfo = bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId());
if(assetInfo == null || assetInfo.getValue() == null){ if(assetInfo == null ){
ArrayList<String> idAssets = new ArrayList<>(); ArrayList<String> idAssets = new ArrayList<>();
idAssets.add(balance.getAsset().getObjectId()); idAssets.add(balance.getAsset().getObjectId());
ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() { ApiRequest getAssetRequest = new ApiRequest(1, new ApiRequestListener() {
@Override @Override
public void success(Object answer, int idPetition) { public void success(Object answer, int idPetition) {
if(answer instanceof BitsharesAsset){ List<BitsharesAsset> assets = (List<BitsharesAsset>) answer;
BitsharesAssetInfo info = new BitsharesAssetInfo((BitsharesAsset) answer); for(BitsharesAsset asset : assets) {
cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency)answer ); BitsharesAssetInfo info = new BitsharesAssetInfo(asset);
long[] cryptoCurrencyId = cryptoCurrencyDao.insertCryptoCurrency((CryptoCurrency) asset);
info.setCryptoCurrencyId(cryptoCurrencyId[0]);
bitsharesAssetDao.insertBitsharesAssetInfo(info); bitsharesAssetDao.insertBitsharesAssetInfo(info);
ccBalance.setCryptoCurrencyId(bitsharesAssetDao.getBitsharesAssetInfoById(balance.getAsset().getObjectId()).getValue().getCryptoCurrencyId()); ccBalance.setCryptoCurrencyId(cryptoCurrencyId[0]);
balanceDao.insertCryptoCoinBalance(ccBalance); balanceDao.insertCryptoCoinBalance(ccBalance);
} }
} }
@Override @Override
public void fail(int idPetition) { public void fail(int idPetition) {
} }
}); });
getAssetById(idAssets,getAssetRequest); getAssetById(idAssets,getAssetRequest);
}else { }else {
ccBalance.setCryptoCurrencyId(assetInfo.getValue().getCryptoCurrencyId()); ccBalance.setCryptoCurrencyId(assetInfo.getCryptoCurrencyId());
balanceDao.insertCryptoCoinBalance(ccBalance); balanceDao.insertCryptoCoinBalance(ccBalance);
} }
} }

View file

@ -23,7 +23,7 @@ public interface BitsharesAssetDao {
LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId); LiveData<BitsharesAssetInfo> getBitsharesAssetInfo(long cryptoCurrencyId);
@Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId") @Query("SELECT * FROM bitshares_asset WHERE bitshares_id = :bitsharesId")
LiveData<BitsharesAssetInfo> getBitsharesAssetInfoById(String bitsharesId); BitsharesAssetInfo getBitsharesAssetInfoById(String bitsharesId);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts); public long[] insertBitsharesAssetInfo(BitsharesAssetInfo... accounts);

View file

@ -85,15 +85,22 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
if(grapheneAccount.getAccountId() == null){ if(grapheneAccount.getAccountId() == null){
System.out.println("Importing without accountid"); System.out.println("Importing without accountid");
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName());
}else if(grapheneAccount.getName() == null){ if(fetch == null) {
System.out.println("Importing without accountname");
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId());
}
if(grapheneAccount == null) {
//TODO grapaheneAccount null, error fetching //TODO grapaheneAccount null, error fetching
return null; return null;
} }
grapheneAccount.setAccountId(fetch.getAccountId());
}else if(grapheneAccount.getName() == null){
System.out.println("Importing without accountname");
GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId());
if(fetch == null) {
//TODO grapaheneAccount null, error fetching
return null;
}
grapheneAccount.setName(fetch.getName());
}
System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId()); System.out.println("Importing not null " + ((GrapheneAccount) account).getName() + " " + ((GrapheneAccount) account).getAccountId());
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
@ -110,14 +117,28 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
@Override @Override
public void loadAccountFromDB(CryptoNetAccount account, Context context) { public void loadAccountFromDB(CryptoNetAccount account, Context context) {
System.out.println("On loadAccount from db");
if(account instanceof GrapheneAccount){ if(account instanceof GrapheneAccount){
GrapheneAccount grapheneAccount = (GrapheneAccount) account; GrapheneAccount grapheneAccount = (GrapheneAccount) account;
CrystalDatabase db = CrystalDatabase.getAppDatabase(context); CrystalDatabase db = CrystalDatabase.getAppDatabase(context);
grapheneAccount.loadInfo(db.grapheneAccountInfoDao().getByAccountId(account.getId())); GrapheneAccountInfo info = db.grapheneAccountInfoDao().getByAccountId(account.getId());
grapheneAccount.loadInfo(info);
if(grapheneAccount.getAccountId() == null){ if(grapheneAccount.getAccountId() == null){
grapheneAccount = this.getAccountInfoByName(grapheneAccount.getName()); System.out.println("On loadAccount null id");
GrapheneAccount fetch = this.getAccountInfoByName(grapheneAccount.getName());
if(fetch != null){
info.setAccountId(fetch.getAccountId());
grapheneAccount.setAccountId(fetch.getAccountId());
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info);
}
}else if(grapheneAccount.getName() == null){ }else if(grapheneAccount.getName() == null){
grapheneAccount = this.getAccountInfoById(grapheneAccount.getAccountId()); System.out.println("On loadAccount null name");
GrapheneAccount fetch = this.getAccountInfoById(grapheneAccount.getAccountId());
if(fetch != null) {
info.setName(fetch.getName());
grapheneAccount.setName(fetch.getName());
db.grapheneAccountInfoDao().insertGrapheneAccountInfo(info);
}
} }
if(grapheneAccount == null) { if(grapheneAccount == null) {
@ -125,6 +146,8 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
return; return;
} }
System.out.println("On loadAccount no nulls " + grapheneAccount.getName() + " " + grapheneAccount.getAccountId());
GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); GrapheneApiGenerator.subscribeBitsharesAccount(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
this.refreshAccountTransactions(account.getId(),context); this.refreshAccountTransactions(account.getId(),context);
GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context); GrapheneApiGenerator.getAccountBalance(grapheneAccount.getId(),grapheneAccount.getAccountId(),context);
@ -321,6 +344,7 @@ public class BitsharesAccountManager implements CryptoAccountManager, CryptoNetI
transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue()); transaction.setAmount(transfer.getOperation().getAssetAmount().getAmount().longValue());
CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol()); CryptoCurrency currency = db.cryptoCurrencyDao().getByName(transfer.getOperation().getAssetAmount().getAsset().getSymbol());
if(currency == null){ if(currency == null){
System.out.println("CryptoCurrency not in database");
//CryptoCurrency not in database //CryptoCurrency not in database
Asset asset = transfer.getOperation().getAssetAmount().getAsset(); Asset asset = transfer.getOperation().getAssetAmount().getAsset();
BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA; BitsharesAsset.Type assetType = BitsharesAsset.Type.UIA;

View file

@ -18,7 +18,7 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
*/ */
@Entity(tableName="crypto_coin_balance", @Entity(tableName="crypto_coin_balance",
indices = {@Index("id"),@Index("account_id")}, indices = {@Index("id"),@Index("account_id"),@Index(value = {"account_id","crypto_currency_id"}, unique=true)},
foreignKeys = @ForeignKey(entity = CryptoNetAccount.class, foreignKeys = @ForeignKey(entity = CryptoNetAccount.class,
parentColumns = "id", parentColumns = "id",
childColumns = "account_id")) childColumns = "account_id"))

View file

@ -2,6 +2,7 @@ package cy.agorise.crystalwallet.models;
import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity; import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey; import android.arch.persistence.room.PrimaryKey;
import android.arch.persistence.room.TypeConverters; import android.arch.persistence.room.TypeConverters;
@ -13,7 +14,8 @@ import cy.agorise.crystalwallet.enums.CryptoNet;
* *
* Created by henry Henry Varona on 11/9/2017. * Created by henry Henry Varona on 11/9/2017.
*/ */
@Entity(tableName="crypto_currency") @Entity(tableName="crypto_currency",
indices = {@Index(value = {"crypto_net","name"}, unique=true)})
public class CryptoCurrency { public class CryptoCurrency {
/** /**