From b8a007132fa7abaecb02aa30be2e33c96bee7119 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 11 Mar 2018 22:54:16 -0400 Subject: [PATCH] - Equivalent Totals in balances items is working --- .../views/CryptoCoinBalanceListAdapter.java | 8 ++++- .../views/CryptoCoinBalanceListView.java | 3 +- .../views/CryptoCoinBalanceViewHolder.java | 11 +++++-- .../views/CryptoNetBalanceViewHolder.java | 33 +++++++++++++++++-- app/src/main/res/layout/balance_list_item.xml | 12 +++++-- .../main/res/layout/fragment_transactions.xml | 19 +++++++++-- 6 files changed, 74 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListAdapter.java index 7554a5d..8d43915 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListAdapter.java @@ -17,16 +17,22 @@ import cy.agorise.crystalwallet.models.CryptoCoinBalance; public class CryptoCoinBalanceListAdapter extends ListAdapter { + CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder; + public CryptoCoinBalanceListAdapter() { super(CryptoCoinBalance.DIFF_CALLBACK); } + public void setCryptoNetBalanceViewHolder(CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder){ + this.cryptoNetBalanceViewHolder = cryptoNetBalanceViewHolder; + } + @Override public CryptoCoinBalanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 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 diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListView.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListView.java index 0c3e5ed..df4958f 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListView.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceListView.java @@ -83,7 +83,7 @@ public class CryptoCoinBalanceListView extends RelativeLayout { * * @param data the list of crypto coin balances that will be show to the user */ - public void setData(List data){ + public void setData(List data, CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder){ //initializes the list adapter if (this.listAdapter == null) { this.listAdapter = new CryptoCoinBalanceListAdapter(); @@ -92,6 +92,7 @@ public class CryptoCoinBalanceListView extends RelativeLayout { //sets the data of the list adapter if (data != null) { + this.listAdapter.setCryptoNetBalanceViewHolder(cryptoNetBalanceViewHolder); this.listAdapter.setList(data); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java index 45e0168..aee2bdc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -39,12 +39,15 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { private Context context; - public CryptoCoinBalanceViewHolder(View itemView) { + private CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder; + + public CryptoCoinBalanceViewHolder(View itemView, CryptoNetBalanceViewHolder cryptoNetBalanceViewHolder) { super(itemView); //TODO: use ButterKnife to load this cryptoCoinName = (TextView) itemView.findViewById(R.id.tvCryptoCoinName); cryptoCoinBalance = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceAmount); cryptoCoinBalanceEquivalence = (TextView) itemView.findViewById(R.id.tvCryptoCoinBalanceEquivalence); + this.cryptoNetBalanceViewHolder = cryptoNetBalanceViewHolder; this.context = itemView.getContext(); } @@ -102,12 +105,14 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { public void onChanged(@Nullable CryptoCurrencyEquivalence cryptoCurrencyEquivalence) { if (cryptoCurrencyEquivalence != null) { 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( "%.2f", - (balance.getBalance() / Math.pow(10, currencyFrom.getPrecision())) / - (cryptoCurrencyEquivalence.getValue() / Math.pow(10, toCurrency.getPrecision())) + equivalentValue ); + cryptoNetBalanceViewHolder.setEquivalentBalance(balance,equivalentValue); cryptoCoinBalanceEquivalence.setText( equivalenceString + " " + toCurrency.getName()); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java index 5fe8fc2..1753a02 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java @@ -16,6 +16,7 @@ import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; +import java.util.HashMap; import java.util.List; import butterknife.BindView; @@ -48,11 +49,18 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { */ 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 */ CryptoCoinBalanceListView cryptoCoinBalanceListView; + HashMap equivalentTotalHashMap; + /* * 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 cryptoNetIcon = (ImageView) itemView.findViewById(R.id.ivCryptoNetIcon); cryptoNetName = (TextView) itemView.findViewById(R.id.tvCryptoNetName); + cryptoNetEquivalentTotal = (TextView) itemView.findViewById(R.id.tvCryptoNetEquivalentTotal); cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalancesListView); btnSendFromThisAccount = (Button) itemView.findViewById(R.id.btnSendFromThisAccount); btnReceiveToThisAccount = (Button) itemView.findViewById(R.id.btnReceiveWithThisAccount); @@ -157,6 +166,23 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { newFragment.show(ft, "ReceiveDialog"); } + public void setEquivalentBalance(CryptoCoinBalance cryptoCoinBalance, double equivalentValue){ + if (this.equivalentTotalHashMap == null){ + this.equivalentTotalHashMap = new HashMap(); + } + + 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 */ @@ -164,6 +190,7 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { if (balance == null){ cryptoNetName.setText("loading..."); } else { + final CryptoNetBalanceViewHolder thisViewHolder = this; this.cryptoNetAccountId = balance.getAccountId(); cryptoNetName.setText(balance.getCryptoNet().getLabel()); @@ -172,13 +199,15 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { cryptoCoinBalanceListViewModel.init(balance.getAccountId()); LiveData> 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 cryptoCoinBalanceData.observe((LifecycleOwner)this.itemView.getContext(), new Observer>() { @Override public void onChanged(List cryptoCoinBalances) { - cryptoCoinBalanceListView.setData(cryptoCoinBalances); + cryptoCoinBalanceListView.setData(cryptoCoinBalances, thisViewHolder); + + } }); } diff --git a/app/src/main/res/layout/balance_list_item.xml b/app/src/main/res/layout/balance_list_item.xml index 1f32387..f0f5e08 100644 --- a/app/src/main/res/layout/balance_list_item.xml +++ b/app/src/main/res/layout/balance_list_item.xml @@ -14,7 +14,7 @@ android:layout_alignParentStart="true" android:layout_alignParentTop="true"> - + +