From 8e7a596d24c4143e12a8f37a6b2baac1e509923b Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 18 Oct 2017 22:35:16 -0400 Subject: [PATCH] - Now the balance fragment shows the balances of every cryptonetaccount with its currencies --- .../dao/converters/Converters.java | 2 +- .../fragments/BalanceFragment.java | 6 ++- .../models/CryptoCoinBalance.java | 6 +-- .../models/CryptoNetAccount.java | 2 + .../RandomCryptoCoinBalanceGenerator.java | 2 +- .../CryptoCoinBalanceListViewModel.java | 7 +++- .../views/CryptoCoinBalanceViewHolder.java | 2 +- .../views/CryptoNetBalanceListAdapter.java | 9 ++++- .../views/CryptoNetBalanceListView.java | 6 ++- .../views/CryptoNetBalanceViewHolder.java | 38 +++++++++++-------- 10 files changed, 51 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java b/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java index f027c4d..023d0eb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/converters/Converters.java @@ -73,7 +73,7 @@ public class Converters { if (net == null) { return ""; } else { - return net.getLabel(); + return net.name(); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java index 998cbd8..0ef4437 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/BalanceFragment.java @@ -52,12 +52,14 @@ public class BalanceFragment extends Fragment { cryptoNetBalanceListViewModel = ViewModelProviders.of(this).get(CryptoNetBalanceListViewModel.class); LiveData> cryptoNetBalanceData = cryptoNetBalanceListViewModel.getCryptoNetBalanceList(); - vCryptoNetBalanceListView.setData(null); + vCryptoNetBalanceListView.setData(null, this); + final Fragment fragment = this; + cryptoNetBalanceData.observe(this, new Observer>() { @Override public void onChanged(List cryptoNetBalances) { - vCryptoNetBalanceListView.setData(cryptoNetBalances); + vCryptoNetBalanceListView.setData(cryptoNetBalances, fragment); } }); diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java index 0f0a5c6..c2eae46 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoCoinBalance.java @@ -56,11 +56,11 @@ public class CryptoCoinBalance { this.mAccountId = accountId; } - public long getCryptoCurrency() { + public long getCryptoCurrencyId() { return mCryptoCurrencyId; } - public void setCryptoCurrency(long cryptoCurrencyId) { + public void setCryptoCurrencyId(long cryptoCurrencyId) { this.mCryptoCurrencyId = cryptoCurrencyId; } @@ -76,7 +76,7 @@ public class CryptoCoinBalance { @Override public boolean areItemsTheSame( @NonNull CryptoCoinBalance oldBalance, @NonNull CryptoCoinBalance newBalance) { - return oldBalance.getCryptoCurrency() == newBalance.getCryptoCurrency(); + return oldBalance.getCryptoCurrencyId() == newBalance.getCryptoCurrencyId(); } @Override public boolean areContentsTheSame( diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java index f72cb2f..421a5aa 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java @@ -4,6 +4,7 @@ package cy.agorise.crystalwallet.models; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.ForeignKey; +import android.arch.persistence.room.Ignore; import android.arch.persistence.room.Index; import android.arch.persistence.room.PrimaryKey; @@ -50,6 +51,7 @@ public class CryptoNetAccount { public CryptoNetAccount() { } + @Ignore public CryptoNetAccount(long mId, long mSeedId, int mAccountIndex, CryptoNet mCryptoNet) { this.mId = mId; this.mSeedId = mSeedId; diff --git a/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoCoinBalanceGenerator.java b/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoCoinBalanceGenerator.java index af96784..cdeeeb4 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoCoinBalanceGenerator.java +++ b/app/src/main/java/cy/agorise/crystalwallet/randomdatagenerators/RandomCryptoCoinBalanceGenerator.java @@ -37,7 +37,7 @@ public class RandomCryptoCoinBalanceGenerator { randomBalance = new CryptoCoinBalance(); randomBalance.setAccountId(randomSelectedAccount.getId()); randomBalance.setBalance(randomAmount); - randomBalance.setCoin(randomCryptoCoin); + //randomBalance.setCoin(randomCryptoCoin); result.add(randomBalance); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCoinBalanceListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCoinBalanceListViewModel.java index c124dae..1e76ea7 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCoinBalanceListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/CryptoCoinBalanceListViewModel.java @@ -20,10 +20,13 @@ public class CryptoCoinBalanceListViewModel extends AndroidViewModel { private LiveData> cryptoCoinBalanceList; private CrystalDatabase db; - public CryptoCoinBalanceListViewModel(Application application, CryptoNetAccount account) { + public CryptoCoinBalanceListViewModel(Application application) { super(application); this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); - this.cryptoCoinBalanceList = this.db.cryptoCoinBalanceDao().getBalancesFromAccount(account.getId()); + } + + public void init(long cryptoNetAccountId){ + this.cryptoCoinBalanceList = this.db.cryptoCoinBalanceDao().getBalancesFromAccount(cryptoNetAccountId); } public LiveData> getCryptoCoinBalanceList(){ 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 3619f4d..c46d448 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -33,7 +33,7 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { cryptoCoinName.setText("loading..."); cryptoCoinBalance.setText(""); } else { - cryptoCoinName.setText(balance.getCoin().getLabel()); + //cryptoCoinName.setText(balance.getCoin().getLabel()); cryptoCoinBalance.setText(""+balance.getBalance()); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceListAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceListAdapter.java index b0e013f..458402b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceListAdapter.java @@ -1,6 +1,7 @@ package cy.agorise.crystalwallet.views; +import android.support.v4.app.Fragment; import android.support.v7.recyclerview.extensions.ListAdapter; import android.view.LayoutInflater; import android.view.View; @@ -8,6 +9,7 @@ import android.view.ViewGroup; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.CryptoNetBalance; +import cy.agorise.crystalwallet.viewmodels.CryptoCoinBalanceListViewModel; /** * Created by Henry Varona on 11/9/2017. @@ -15,8 +17,11 @@ import cy.agorise.crystalwallet.models.CryptoNetBalance; public class CryptoNetBalanceListAdapter extends ListAdapter { - public CryptoNetBalanceListAdapter() { + Fragment fragment; + + public CryptoNetBalanceListAdapter(Fragment fragment) { super(CryptoNetBalance.DIFF_CALLBACK); + this.fragment = fragment; } @Override @@ -24,7 +29,7 @@ public class CryptoNetBalanceListAdapter extends ListAdapter data){ + public void setData(List data, Fragment fragment){ if (this.listAdapter == null) { - this.listAdapter = new CryptoNetBalanceListAdapter(); + this.listAdapter = new CryptoNetBalanceListAdapter(fragment); this.listView.setAdapter(this.listAdapter); } 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 0db7099..10d49bc 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoNetBalanceViewHolder.java @@ -1,14 +1,24 @@ package cy.agorise.crystalwallet.views; +import android.app.Activity; +import android.support.v4.app.Fragment; +import android.arch.lifecycle.LifecycleOwner; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; +import android.arch.paging.PagedList; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; +import java.util.List; + import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.models.CryptoCoinBalance; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoNetBalance; +import cy.agorise.crystalwallet.viewmodels.CryptoCoinBalanceListViewModel; /** * Created by Henry Varona on 17/9/2017. @@ -19,13 +29,14 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { private TextView cryptoNetName; private CryptoCoinBalanceListView cryptoCoinBalanceListView; + private Fragment fragment; - public CryptoNetBalanceViewHolder(View itemView) { + public CryptoNetBalanceViewHolder(View itemView, Fragment fragment) { super(itemView); cryptoNetIcon = (ImageView) itemView.findViewById(R.id.ivCryptoNetIcon); cryptoNetName = (TextView) itemView.findViewById(R.id.tvCryptoNetName); cryptoCoinBalanceListView = (CryptoCoinBalanceListView) itemView.findViewById(R.id.cryptoCoinBalancesListView); - + this.fragment = fragment; } public void clear(){ @@ -38,20 +49,17 @@ public class CryptoNetBalanceViewHolder extends RecyclerView.ViewHolder { } else { cryptoNetName.setText(balance.getCryptoNet().getLabel()); - /*transactionListView = this.findViewById(R.id.transaction_list); + CryptoCoinBalanceListViewModel cryptoCoinBalanceListViewModel = ViewModelProviders.of(this.fragment).get(CryptoCoinBalanceListViewModel.class); + cryptoCoinBalanceListViewModel.init(balance.getAccountId()); + LiveData> cryptoCoinBalanceData = cryptoCoinBalanceListViewModel.getCryptoCoinBalanceList(); + cryptoCoinBalanceListView.setData(null); - transactionListViewModel = ViewModelProviders.of(getContext).get(TransactionListViewModel.class); - LiveData> transactionData = transactionListViewModel.getTransactionList(); - transactionListView.setData(null); - - transactionData.observe(this, new Observer>() { - @Override - public void onChanged(PagedList cryptoCoinTransactions) { - transactionListView.setData(cryptoCoinTransactions); - } - }); - - cryptoCoinBalanceListView.setData();*/ + cryptoCoinBalanceData.observe((LifecycleOwner)this.itemView.getContext(), new Observer>() { + @Override + public void onChanged(List cryptoCoinBalances) { + cryptoCoinBalanceListView.setData(cryptoCoinBalances); + } + }); } } }