- Equivalent Totals in balances items is working

master
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> {
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

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
*/
public void setData(List<CryptoCoinBalance> data){
public void setData(List<CryptoCoinBalance> 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);
}
}

View File

@ -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());
}

View File

@ -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<CryptoCoinBalance, Double> 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<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
*/
@ -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<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
cryptoCoinBalanceData.observe((LifecycleOwner)this.itemView.getContext(), new Observer<List<CryptoCoinBalance>>() {
@Override
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_alignParentTop="true">
<LinearLayout
<RelativeLayout
android:id="@+id/cryptoNetBalanceTitleBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -31,8 +31,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_toRightOf="@id/ivCryptoNetIcon"
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
android:id="@+id/btnSendFromThisAccount"
android:layout_width="wrap_content"
@ -46,7 +54,7 @@
android:layout_height="wrap_content"
android:text="receive"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
<cy.agorise.crystalwallet.views.CryptoCoinBalanceListView
android:id="@+id/cryptoCoinBalancesListView"

View File

@ -4,9 +4,22 @@
android:layout_height="match_parent"
tools:context="cy.agorise.crystalwallet.fragments.TransactionsFragment">
<cy.agorise.crystalwallet.views.TransactionListView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/vTransactionListView" />
android:layout_height="wrap_content">
<!--<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>