- Now the balance fragment is showing the user accounts. Still it needs to show the balances of their assets

This commit is contained in:
Javier Varona 2017-10-14 22:02:55 -04:00
parent b15c5a4710
commit 814d724096
15 changed files with 136 additions and 89 deletions

View file

@ -55,7 +55,7 @@ public class IntroActivity extends AppCompatActivity {
/*CrystalDatabase db = CrystalDatabase.getAppDatabase(getApplicationContext()); /*CrystalDatabase db = CrystalDatabase.getAppDatabase(getApplicationContext());
List<AccountSeed> seeds = RandomSeedGenerator.generateSeeds(2); List<AccountSeed> seeds = RandomSeedGenerator.generateSeeds(2);
for(int i=0;i<seeds.size();i++) { for(int i=0;i<seeds.size();i++) {
long newId = db.accountSeedDao().insertAccountSeed(seeds.get(i))[0]; long newId = db.accountSeedDao().insertAccountSeed(seeds.get(i));
seeds.get(i).setId(newId); seeds.get(i).setId(newId);
} }
List<CryptoNetAccount> accounts = RandomCryptoNetAccountGenerator.generateAccounts(5,seeds); List<CryptoNetAccount> accounts = RandomCryptoNetAccountGenerator.generateAccounts(5,seeds);

View file

@ -22,8 +22,11 @@ public interface CryptoCoinBalanceDao {
@Query("SELECT * FROM crypto_coin_balance") @Query("SELECT * FROM crypto_coin_balance")
List<CryptoCoinBalance> getAll(); List<CryptoCoinBalance> getAll();
@Query("SELECT * FROM crypto_net_account WHERE account_id = :acountId") @Query("SELECT id as account_id, account_number FROM crypto_net_account")
LiveData<List<CryptoNetBalance>> getAllFromAccount(long accountId); LiveData<List<CryptoNetBalance>> getAllBalances();
@Query("SELECT * FROM crypto_coin_balance WHERE account_id = :accountId")
LiveData<List<CryptoCoinBalance>> getBalancesFromAccount(long accountId);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertCryptoCoinBalance(CryptoCoinBalance... balances); public long[] insertCryptoCoinBalance(CryptoCoinBalance... balances);

View file

@ -23,13 +23,6 @@ public interface CryptoNetAccountDao {
@Query("SELECT * FROM crypto_net_account") @Query("SELECT * FROM crypto_net_account")
List<CryptoNetAccount> getAll(); List<CryptoNetAccount> getAll();
@Query("SELECT * FROM crypto_net_account")
LiveData<List<CryptoNetBalance>> getAllBalances();
@Query("SELECT 'Bitshares' as coin, 1 as balance FROM crypto_net_account WHERE id = :accountId")
LiveData<List<CryptoCoinBalance>> getBalancesFromAccount(long accountId);
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
public long[] insertCryptoNetAccount(CryptoNetAccount... accounts); public long[] insertCryptoNetAccount(CryptoNetAccount... accounts);

View file

@ -10,6 +10,7 @@ import android.content.Context;
import cy.agorise.crystalwallet.dao.converters.Converters; import cy.agorise.crystalwallet.dao.converters.Converters;
import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.CryptoCoinBalance;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoCurrency;
import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -19,7 +20,7 @@ import cy.agorise.crystalwallet.models.CryptoNetAccount;
* Created by Henry Varona on 4/9/2017. * Created by Henry Varona on 4/9/2017.
*/ */
@Database(entities = {AccountSeed.class, CryptoNetAccount.class, CryptoCoinTransaction.class, CryptoCurrency.class}, version = 2) @Database(entities = {AccountSeed.class, CryptoNetAccount.class, CryptoCoinTransaction.class, CryptoCurrency.class, CryptoCoinBalance.class}, version = 2)
@TypeConverters({Converters.class}) @TypeConverters({Converters.class})
public abstract class CrystalDatabase extends RoomDatabase { public abstract class CrystalDatabase extends RoomDatabase {
@ -28,6 +29,7 @@ public abstract class CrystalDatabase extends RoomDatabase {
public abstract AccountSeedDao accountSeedDao(); public abstract AccountSeedDao accountSeedDao();
public abstract CryptoNetAccountDao cryptoNetAccountDao(); public abstract CryptoNetAccountDao cryptoNetAccountDao();
public abstract TransactionDao transactionDao(); public abstract TransactionDao transactionDao();
public abstract CryptoCoinBalanceDao cryptoCoinBalanceDao();
public static CrystalDatabase getAppDatabase(Context context) { public static CrystalDatabase getAppDatabase(Context context) {
if (instance == null) { if (instance == null) {

View file

@ -103,4 +103,18 @@ public class Converters {
return SeedType.valueOf(value); return SeedType.valueOf(value);
} }
} }
@TypeConverter
public int cryptoNetToAccountNumber(CryptoNet value) {
if (value == null) {
return -1;
} else {
return value.getBip44Index();
}
}
@TypeConverter
public CryptoNet accountNumberToCryptoNet(int value) {
return CryptoNet.fromBip44Index(value);
}
} }

View file

@ -1,6 +1,8 @@
package cy.agorise.crystalwallet.enums; package cy.agorise.crystalwallet.enums;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/** /**
* CryptoNet Enumeration, a Crypto Net is define as the net where a CryptoCoin works, iniside the * CryptoNet Enumeration, a Crypto Net is define as the net where a CryptoCoin works, iniside the
@ -9,16 +11,25 @@ import java.io.Serializable;
* Created by Henry Varona on 12/9/2017. * Created by Henry Varona on 12/9/2017.
*/ */
public enum CryptoNet implements Serializable { public enum CryptoNet implements Serializable {
BITCOIN("BITCOIN",6), BITCOIN_TEST("BITCOIN(TEST)",6), LITECOIN("LITECOIN",6), DASH("DASH",6), DOGECOIN("DOGECOIN",6), BITSHARES("BITSHARES",1), STEEM("STEEN",1); UNKNOWN("UNKNOWN",6,-1), BITCOIN("BITCOIN",6,1), BITCOIN_TEST("BITCOIN(TEST)",6,2), LITECOIN("LITECOIN",6,3), DASH("DASH",6,5), DOGECOIN("DOGECOIN",6,4), BITSHARES("BITSHARES",1,6), STEEM("STEEM",1,7);
protected String label; protected String label;
protected int confirmationsNeeded; protected int confirmationsNeeded;
protected int bip44Index;
CryptoNet(String label,int confirmationsNeeded){ private static Map<Integer, CryptoNet> bip44Map = new HashMap<Integer, CryptoNet>();
static {
for (CryptoNet cryptoNetEnum : CryptoNet.values()) {
bip44Map.put(cryptoNetEnum.bip44Index, cryptoNetEnum);
}
}
CryptoNet(String label,int confirmationsNeeded, int bip44Index){
this.label = label; this.label = label;
this.confirmationsNeeded = confirmationsNeeded; this.confirmationsNeeded = confirmationsNeeded;
this.bip44Index = bip44Index;
} }
public String getLabel(){ public String getLabel(){
@ -28,4 +39,16 @@ public enum CryptoNet implements Serializable {
public int getConfirmationsNeeded(){ public int getConfirmationsNeeded(){
return this.confirmationsNeeded; return this.confirmationsNeeded;
} }
public int getBip44Index(){
return this.bip44Index;
}
public static CryptoNet fromBip44Index(int index){
if (bip44Map.containsKey(index)) {
return bip44Map.get(index);
} else {
return CryptoNet.UNKNOWN;
}
}
} }

View file

@ -1,5 +1,8 @@
package cy.agorise.crystalwallet.fragments; package cy.agorise.crystalwallet.fragments;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -8,9 +11,22 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.models.CryptoNetBalance;
import cy.agorise.crystalwallet.viewmodels.CryptoCoinBalanceListViewModel;
import cy.agorise.crystalwallet.viewmodels.CryptoNetBalanceListViewModel;
import cy.agorise.crystalwallet.views.CryptoNetBalanceListView;
public class BalanceFragment extends Fragment { public class BalanceFragment extends Fragment {
CryptoNetBalanceListViewModel cryptoNetBalanceListViewModel;
@BindView(R.id.vCryptoNetBalanceListView)
CryptoNetBalanceListView vCryptoNetBalanceListView;
public BalanceFragment() { public BalanceFragment() {
// Required empty public constructor // Required empty public constructor
} }
@ -31,6 +47,20 @@ public class BalanceFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_balance, container, false); View view = inflater.inflate(R.layout.fragment_balance, container, false);
ButterKnife.bind(this, view);
cryptoNetBalanceListViewModel = ViewModelProviders.of(this).get(CryptoNetBalanceListViewModel.class);
LiveData<List<CryptoNetBalance>> cryptoNetBalanceData = cryptoNetBalanceListViewModel.getCryptoNetBalanceList();
vCryptoNetBalanceListView.setData(null);
cryptoNetBalanceData.observe(this, new Observer<List<CryptoNetBalance>>() {
@Override
public void onChanged(List<CryptoNetBalance> cryptoNetBalances) {
vCryptoNetBalanceListView.setData(cryptoNetBalances);
}
});
return view;
} }
} }

View file

@ -24,6 +24,13 @@ import cy.agorise.crystalwallet.enums.CryptoCoin;
childColumns = "account_id")) childColumns = "account_id"))
public class CryptoCoinBalance { public class CryptoCoinBalance {
/**
* The id on the database
*/
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "id")
private long mId;
@ColumnInfo(name="account_id") @ColumnInfo(name="account_id")
private long mAccountId; private long mAccountId;
@ -33,6 +40,14 @@ public class CryptoCoinBalance {
@ColumnInfo(name = "balance") @ColumnInfo(name = "balance")
private int mBalance; private int mBalance;
public long getId() {
return mId;
}
public void setId(long id) {
this.mId = id;
}
public long getAccountId() { public long getAccountId() {
return mAccountId; return mAccountId;
} }

View file

@ -4,98 +4,59 @@ 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.ForeignKey; import android.arch.persistence.room.ForeignKey;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.Index; import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey; import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.DiffCallback; import android.support.v7.recyclerview.extensions.DiffCallback;
import cy.agorise.crystalwallet.enums.CryptoCoin; import cy.agorise.crystalwallet.enums.CryptoCoin;
import cy.agorise.crystalwallet.enums.CryptoNet;
import static android.arch.persistence.room.ColumnInfo.INTEGER;
/** /**
* Represents a geneeric CryptoNet Account Balance * Represents a generic CryptoNet Account Balance
* *
* Created by Henry Varona on 6/9/2017. * Created by Henry Varona on 6/9/2017.
*/ */
@Entity(tableName = "crypto_net_account", @Entity
indices = {@Index("id"),@Index("seed_id")},
foreignKeys = @ForeignKey(entity = AccountSeed.class,
parentColumns = "id",
childColumns = "seed_id"))
public class CryptoNetBalance { public class CryptoNetBalance {
/** /**
* The id on the database * The id of the account of this balance
*/ */
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "account_id")
@ColumnInfo(name = "id") private long mAccountId;
private long mId;
/** /**
* The id of the seed used by this account * The cryptonet of the account
*/ */
@ColumnInfo(name = "seed_id") @ColumnInfo(name = "account_number", typeAffinity = INTEGER)
private long mSeedId; private CryptoNet mCryptoNet;
/** public long getAccountId() {
* The account number on the bip44 or slip44 return mAccountId;
*/
@ColumnInfo(name = "account_number")
private int mAccountNumber;
/**
* The account index on this wallet
*/
@ColumnInfo(name = "account_index")
private int mAccountIndex;
private CryptoCoin mCryptoCoin;
public long getId() {
return mId;
} }
public void setId(long id) { public CryptoNet getCryptoNet() {
this.mId = id; return mCryptoNet;
} }
public long getSeedId() { public void setAccountId(long accountId) {
return mSeedId; this.mAccountId = accountId;
} }
public void setSeedId(long seedId) { public void setCryptoNet(CryptoNet cryptoNet) {
this.mSeedId = seedId; this.mCryptoNet = cryptoNet;
}
public int getAccountNumber() {
return mAccountNumber;
}
public void setAccountNumber(int accountNumber) {
this.mAccountNumber = accountNumber;
}
public int getAccountIndex() {
return mAccountIndex;
}
public void setAccountIndex(int accountIndex) {
this.mAccountIndex = accountIndex;
}
public CryptoCoin getCryptoCoin() {
return mCryptoCoin;
}
public void setCryptoCoin(CryptoCoin cryptoCoin) {
this.mCryptoCoin = cryptoCoin;
} }
public static final DiffCallback<CryptoNetBalance> DIFF_CALLBACK = new DiffCallback<CryptoNetBalance>() { public static final DiffCallback<CryptoNetBalance> DIFF_CALLBACK = new DiffCallback<CryptoNetBalance>() {
@Override @Override
public boolean areItemsTheSame( public boolean areItemsTheSame(
@NonNull CryptoNetBalance oldBalance, @NonNull CryptoNetBalance newBalance) { @NonNull CryptoNetBalance oldBalance, @NonNull CryptoNetBalance newBalance) {
return oldBalance.getId() == newBalance.getId(); return oldBalance.getAccountId() == newBalance.getAccountId();
} }
@Override @Override
public boolean areContentsTheSame( public boolean areContentsTheSame(
@ -110,11 +71,7 @@ public class CryptoNetBalance {
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
CryptoNetBalance that = (CryptoNetBalance) o; CryptoNetBalance that = (CryptoNetBalance) o;
return mAccountId == that.mAccountId;
if (mId != that.mId) return false;
if (mSeedId != that.mSeedId) return false;
if (mAccountNumber != that.mAccountNumber) return false;
return mAccountIndex == that.mAccountIndex;
} }
} }

View file

@ -1,9 +1,11 @@
package cy.agorise.crystalwallet.randomdatagenerators; package cy.agorise.crystalwallet.randomdatagenerators;
import java.lang.reflect.Array;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import cy.agorise.crystalwallet.enums.CryptoNet;
import cy.agorise.crystalwallet.models.AccountSeed; import cy.agorise.crystalwallet.models.AccountSeed;
import cy.agorise.crystalwallet.models.CryptoNetAccount; import cy.agorise.crystalwallet.models.CryptoNetAccount;
@ -17,12 +19,19 @@ public class RandomCryptoNetAccountGenerator {
ArrayList<CryptoNetAccount> result = new ArrayList<CryptoNetAccount>(); ArrayList<CryptoNetAccount> result = new ArrayList<CryptoNetAccount>();
Random randomGenerator = new Random(); Random randomGenerator = new Random();
CryptoNetAccount randomAccount; CryptoNetAccount randomAccount;
ArrayList<CryptoNet> cryptoNetList = new ArrayList<CryptoNet>();
for (CryptoNet cryptoNet : CryptoNet.values()){
if (!cryptoNet.name().equals("UNKNOWN")) {
cryptoNetList.add(cryptoNet);
}
}
for (int i=0;i<numberOfAccounts;i++){ for (int i=0;i<numberOfAccounts;i++){
int randomSeedIndex = randomGenerator.nextInt(seeds.size()); int randomSeedIndex = randomGenerator.nextInt(seeds.size());
AccountSeed randomSelectedSeed = seeds.get(randomSeedIndex); AccountSeed randomSelectedSeed = seeds.get(randomSeedIndex);
int randomAccountNumber = cryptoNetList.get(randomGenerator.nextInt(cryptoNetList.size())).getBip44Index();
int randomAccountIndex = randomGenerator.nextInt(1000); int randomAccountIndex = randomGenerator.nextInt(1000);
int randomAccountNumber = randomGenerator.nextInt(1000);
randomAccount = new CryptoNetAccount(); randomAccount = new CryptoNetAccount();
randomAccount.setSeedId(randomSelectedSeed.getId()); randomAccount.setSeedId(randomSelectedSeed.getId());

View file

@ -23,7 +23,7 @@ public class CryptoCoinBalanceListViewModel extends AndroidViewModel {
public CryptoCoinBalanceListViewModel(Application application, CryptoNetAccount account) { public CryptoCoinBalanceListViewModel(Application application, CryptoNetAccount account) {
super(application); super(application);
this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext());
this.cryptoCoinBalanceList = this.db.cryptoNetAccountDao().getBalancesFromAccount(account.getId()); this.cryptoCoinBalanceList = this.db.cryptoCoinBalanceDao().getBalancesFromAccount(account.getId());
} }
public LiveData<List<CryptoCoinBalance>> getCryptoCoinBalanceList(){ public LiveData<List<CryptoCoinBalance>> getCryptoCoinBalanceList(){

View file

@ -22,7 +22,7 @@ public class CryptoNetBalanceListViewModel extends AndroidViewModel {
public CryptoNetBalanceListViewModel(Application application) { public CryptoNetBalanceListViewModel(Application application) {
super(application); super(application);
this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext());
this.cryptoNetBalanceList = this.db.cryptoNetAccountDao().getAllBalances(); this.cryptoNetBalanceList = this.db.cryptoCoinBalanceDao().getAllBalances();
} }
public LiveData<List<CryptoNetBalance>> getCryptoNetBalanceList(){ public LiveData<List<CryptoNetBalance>> getCryptoNetBalanceList(){

View file

@ -51,11 +51,11 @@ public class CryptoCoinBalanceListView extends RelativeLayout {
public void init(){ public void init(){
rootView = mInflater.inflate(R.layout.crypto_coin_balance_list, this, true); rootView = mInflater.inflate(R.layout.crypto_coin_balance_list, this, true);
this.listView = rootView.findViewById(R.id.cryptoCoinBalanceListView); /*this.listView = rootView.findViewById(R.id.cryptoCoinBalanceListView);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext()); final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext());
this.listView.setLayoutManager(linearLayoutManager); this.listView.setLayoutManager(linearLayoutManager);
this.listView.setNestedScrollingEnabled(false); this.listView.setNestedScrollingEnabled(false);*/
} }
public void setData(List<CryptoCoinBalance> data){ public void setData(List<CryptoCoinBalance> data){

View file

@ -16,14 +16,14 @@ import cy.agorise.crystalwallet.models.CryptoNetBalance;
public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
private ImageView cryptoNetIcon; private ImageView cryptoNetIcon;
private TextView cryptoNetName; private TextView cryptoNetName;
private CryptoNetBalanceListView cryptoNetBalanceListView; private CryptoCoinBalanceListView cryptoCoinBalanceListView;
public CryptoNetBalanceViewHolder(View itemView) { public CryptoNetBalanceViewHolder(View itemView) {
super(itemView); super(itemView);
cryptoNetIcon = (ImageView) itemView.findViewById(R.id.ivCryptoNetIcon); cryptoNetIcon = (ImageView) itemView.findViewById(R.id.ivCryptoNetIcon);
cryptoNetName = (TextView) itemView.findViewById(R.id.tvCryptoNetName); cryptoNetName = (TextView) itemView.findViewById(R.id.tvCryptoNetName);
cryptoNetBalanceListView = (CryptoNetBalanceListView) itemView.findViewById(R.id.cryptoCoinBalanceListView); cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalanceListView);
} }
@ -35,7 +35,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
if (balance == null){ if (balance == null){
cryptoNetName.setText("loading..."); cryptoNetName.setText("loading...");
} else { } else {
cryptoNetName.setText(balance.getCryptoCoin().getLabel()); cryptoNetName.setText(balance.getCryptoNet().getLabel());
} }
} }
} }

View file

@ -6,6 +6,7 @@
<cy.agorise.crystalwallet.views.CryptoNetBalanceListView <cy.agorise.crystalwallet.views.CryptoNetBalanceListView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:id="@+id/vCryptoNetBalanceListView" />
</FrameLayout> </FrameLayout>