- Equivalent Totals in balances items is working

This commit is contained in:
Javier Varona 2018-03-11 22:54:16 -04:00
parent 8464ee18ec
commit b8a007132f
6 changed files with 74 additions and 12 deletions

View file

@ -17,16 +17,22 @@ import cy.agorise.crystalwallet.models.CryptoCoinBalance;
public class CryptoCoinBalanceListAdapter extends ListAdapter<CryptoCoinBalance, CryptoCoinBalanceViewHolder> { public class CryptoCoinBalanceListAdapter extends ListAdapter<CryptoCoinBalance, CryptoCoinBalanceViewHolder> {
CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder;
public CryptoCoinBalanceListAdapter() { public CryptoCoinBalanceListAdapter() {
super(CryptoCoinBalance.DIFF_CALLBACK); super(CryptoCoinBalance.DIFF_CALLBACK);
} }
public void setCryptoNetBalanceViewHolder(CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder){
this.cryptoNetBalanceViewHolder = cryptoNetBalanceViewHolder;
}
@Override @Override
public CryptoCoinBalanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public CryptoCoinBalanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.crypto_coin_balance_list_item,parent,false); View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.crypto_coin_balance_list_item,parent,false);
return new CryptoCoinBalanceViewHolder(v); return new CryptoCoinBalanceViewHolder(v, cryptoNetBalanceViewHolder);
} }
@Override @Override

View file

@ -83,7 +83,7 @@ public class CryptoCoinBalanceListView extends RelativeLayout {
* *
* @param data the list of crypto coin balances that will be show to the user * @param data the list of crypto coin balances that will be show to the user
*/ */
public void setData(List<CryptoCoinBalance> data){ public void setData(List<CryptoCoinBalance> data, CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder){
//initializes the list adapter //initializes the list adapter
if (this.listAdapter == null) { if (this.listAdapter == null) {
this.listAdapter = new CryptoCoinBalanceListAdapter(); this.listAdapter = new CryptoCoinBalanceListAdapter();
@ -92,6 +92,7 @@ public class CryptoCoinBalanceListView extends RelativeLayout {
//sets the data of the list adapter //sets the data of the list adapter
if (data != null) { if (data != null) {
this.listAdapter.setCryptoNetBalanceViewHolder(cryptoNetBalanceViewHolder);
this.listAdapter.setList(data); this.listAdapter.setList(data);
} }
} }

View file

@ -39,12 +39,15 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
private Context context; private Context context;
public CryptoCoinBalanceViewHolder(View itemView) { private CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder;
public CryptoCoinBalanceViewHolder(View itemView, CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder) {
super(itemView); super(itemView);
//TODO: use ButterKnife to load this //TODO: use ButterKnife to load this
cryptoCoinName = (TextView) itemView.findViewById(R.id.tvCryptoCoinName); cryptoCoinName = (TextView) itemView.findViewById(R.id.tvCryptoCoinName);
cryptoCoinBalance = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceAmount); cryptoCoinBalance = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceAmount);
cryptoCoinBalanceEquivalence = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceEquivalence); cryptoCoinBalanceEquivalence = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceEquivalence);
this.cryptoNetBalanceViewHolder = cryptoNetBalanceViewHolder;
this.context = itemView.getContext(); this.context = itemView.getContext();
} }
@ -102,12 +105,14 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder {
public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) {
if (cryptoCurrencyEquivalence != null) { if (cryptoCurrencyEquivalence != null) {
CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId()); CryptoCurrency toCurrency = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(cryptoCurrencyEquivalence.getFromCurrencyId());
double equivalentValue = (balance.getBalance() / Math.pow(10, currencyFrom.getPrecision())) /
(cryptoCurrencyEquivalence.getValue() / Math.pow(10, toCurrency.getPrecision()));
String equivalenceString = String.format( String equivalenceString = String.format(
"%.2f", "%.2f",
(balance.getBalance() / Math.pow(10, currencyFrom.getPrecision())) / equivalentValue
(cryptoCurrencyEquivalence.getValue() / Math.pow(10, toCurrency.getPrecision()))
); );
cryptoNetBalanceViewHolder.setEquivalentBalance(balance,equivalentValue);
cryptoCoinBalanceEquivalence.setText( cryptoCoinBalanceEquivalence.setText(
equivalenceString + " " + toCurrency.getName()); equivalenceString + " " + toCurrency.getName());
} }

View file

@ -16,6 +16,7 @@ import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import java.util.HashMap;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
@ -48,11 +49,18 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
*/ */
TextView cryptoNetName; TextView cryptoNetName;
/*
* the view holding the equivalent total of the crypto net user account
*/
TextView cryptoNetEquivalentTotal;
/* /*
* The list view of the crypto coins balances of this crypto net balance * The list view of the crypto coins balances of this crypto net balance
*/ */
CryptoCoinBalanceListView cryptoCoinBalanceListView; CryptoCoinBalanceListView cryptoCoinBalanceListView;
HashMap<CryptoCoinBalance, Double> equivalentTotalHashMap;
/* /*
* The button for sending transactions from this crypto net balance account * The button for sending transactions from this crypto net balance account
*/ */
@ -86,6 +94,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
//TODO: use ButterKnife to load the views //TODO: use ButterKnife to load the views
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);
cryptoNetEquivalentTotal = (TextView) itemView.findViewById(R.id.tvCryptoNetEquivalentTotal);
cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalancesListView); cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalancesListView);
btnSendFromThisAccount = (Button) itemView.findViewById(R.id.btnSendFromThisAccount); btnSendFromThisAccount = (Button) itemView.findViewById(R.id.btnSendFromThisAccount);
btnReceiveToThisAccount = (Button) itemView.findViewById(R.id.btnReceiveWithThisAccount); btnReceiveToThisAccount = (Button) itemView.findViewById(R.id.btnReceiveWithThisAccount);
@ -157,6 +166,23 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
newFragment.show(ft, "ReceiveDialog"); newFragment.show(ft, "ReceiveDialog");
} }
public void setEquivalentBalance(CryptoCoinBalance cryptoCoinBalance, double equivalentValue){
if (this.equivalentTotalHashMap == null){
this.equivalentTotalHashMap = new HashMap<CryptoCoinBalance, Double>();
}
if (cryptoCoinBalance != null) {
this.equivalentTotalHashMap.put(cryptoCoinBalance, equivalentValue);
float totalEquivalent = 0;
for (Double nextEquivalent : this.equivalentTotalHashMap.values()){
totalEquivalent += nextEquivalent;
}
this.cryptoNetEquivalentTotal.setText(""+totalEquivalent);
}
}
/* /*
* Binds this view with the data of an element of the list * Binds this view with the data of an element of the list
*/ */
@ -164,6 +190,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
if (balance == null){ if (balance == null){
cryptoNetName.setText("loading..."); cryptoNetName.setText("loading...");
} else { } else {
final CryptoNetBalanceViewHolder thisViewHolder = this;
this.cryptoNetAccountId = balance.getAccountId(); this.cryptoNetAccountId = balance.getAccountId();
cryptoNetName.setText(balance.getCryptoNet().getLabel()); cryptoNetName.setText(balance.getCryptoNet().getLabel());
@ -172,13 +199,15 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder {
cryptoCoinBalanceListViewModel.init(balance.getAccountId()); cryptoCoinBalanceListViewModel.init(balance.getAccountId());
LiveData<List<CryptoCoinBalance>> cryptoCoinBalanceData = cryptoCoinBalanceListViewModel.getCryptoCoinBalanceList(); LiveData<List<CryptoCoinBalance>> cryptoCoinBalanceData = cryptoCoinBalanceListViewModel.getCryptoCoinBalanceList();
cryptoCoinBalanceListView.setData(null); cryptoCoinBalanceListView.setData(null, this);
//Observes the livedata, so any of its changes on the database will be reloaded here //Observes the livedata, so any of its changes on the database will be reloaded here
cryptoCoinBalanceData.observe((LifecycleOwner)this.itemView.getContext(), new Observer<List<CryptoCoinBalance>>() { cryptoCoinBalanceData.observe((LifecycleOwner)this.itemView.getContext(), new Observer<List<CryptoCoinBalance>>() {
@Override @Override
public void onChanged(List<CryptoCoinBalance> cryptoCoinBalances) { public void onChanged(List<CryptoCoinBalance> cryptoCoinBalances) {
cryptoCoinBalanceListView.setData(cryptoCoinBalances); cryptoCoinBalanceListView.setData(cryptoCoinBalances, thisViewHolder);
} }
}); });
} }

View file

@ -14,7 +14,7 @@
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:layout_alignParentTop="true"> android:layout_alignParentTop="true">
<LinearLayout <RelativeLayout
android:id="@+id/cryptoNetBalanceTitleBarLayout" android:id="@+id/cryptoNetBalanceTitleBarLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -31,8 +31,16 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ems="10" android:ems="10"
android:layout_toRightOf="@id/ivCryptoNetIcon"
android:text="unknown coin" /> android:text="unknown coin" />
<TextView
android:id="@+id/tvCryptoNetEquivalentTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="0.003€" />
<Button <Button
android:id="@+id/btnSendFromThisAccount" android:id="@+id/btnSendFromThisAccount"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -46,7 +54,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="receive" android:text="receive"
android:visibility="gone" /> android:visibility="gone" />
</LinearLayout> </RelativeLayout>
<cy.agorise.crystalwallet.views.CryptoCoinBalanceListView <cy.agorise.crystalwallet.views.CryptoCoinBalanceListView
android:id="@+id/cryptoCoinBalancesListView" android:id="@+id/cryptoCoinBalancesListView"

View file

@ -4,9 +4,22 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="cy.agorise.crystalwallet.fragments.TransactionsFragment"> tools:context="cy.agorise.crystalwallet.fragments.TransactionsFragment">
<cy.agorise.crystalwallet.views.TransactionListView <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:id="@+id/vTransactionListView" />
<!--<TextView
android:id="@+id/tvTransactionSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android: />
<Spinner
android:layout_width="match_parent"
android:layout_height="match_parent"></Spinner>
-->
<cy.agorise.crystalwallet.views.TransactionListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vTransactionListView" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView> </android.support.v4.widget.NestedScrollView>