- Now the Transaction History is working and is Paged

- New class to test the app: a Random Transaction Generator
This commit is contained in:
Javier Varona 2017-09-24 17:52:32 -04:00
parent 7306652dda
commit 515d09c0c2
8 changed files with 84 additions and 33 deletions

View file

@ -10,18 +10,25 @@ import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List;
import carbon.crypto.com.carbon.Assertions.RecyclerViewItemsCountAssertion; import carbon.crypto.com.carbon.Assertions.RecyclerViewItemsCountAssertion;
import cy.agorise.crystalwallet.IntroActivity; import cy.agorise.crystalwallet.IntroActivity;
import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.randomdatagenerators.RandomTransactionsGenerator;
import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/** /**
* Created by Henry Varona on 19/9/2017. * Created by Henry Varona on 19/9/2017.
*/ */
@ -29,34 +36,17 @@ import static android.support.test.espresso.matcher.ViewMatchers.withId;
public class TransactionListTest { public class TransactionListTest {
CrystalDatabase db; CrystalDatabase db;
List<CryptoCoinTransaction> transactions;
int numberOfTransactions = 100;
@Before @Before
public void addingTransactions(){ public void addingTransactions(){
Calendar cal = Calendar.getInstance(); db = CrystalDatabase.getAppDatabase(InstrumentationRegistry.getTargetContext());
transactions = RandomTransactionsGenerator.generateTransactions(numberOfTransactions,1262304001,1496275201,1,999999999);
this.db = CrystalDatabase.getAppDatabase(InstrumentationRegistry.getTargetContext()); for(int i=0;i<transactions.size();i++) {
cal.set(2017,01,01,01,01,01); db.transactionDao().insertTransaction(transactions.get(i));
CryptoCoinTransaction newTransaction = new CryptoCoinTransaction(); }
newTransaction.setAmount(1);
newTransaction.setFrom("friend1");
newTransaction.setTo("me1");
newTransaction.setDate(cal.getTime());
db.transactionDao().insertTransaction(newTransaction);
cal.set(2017,02,02,02,02,02);
newTransaction = new CryptoCoinTransaction();
newTransaction.setAmount(2);
newTransaction.setFrom("friend2");
newTransaction.setTo("me2");
newTransaction.setDate(cal.getTime());
db.transactionDao().insertTransaction(newTransaction);
cal.set(2017,03,03,03,03,03);
newTransaction = new CryptoCoinTransaction();
newTransaction.setAmount(3);
newTransaction.setFrom("friend3");
newTransaction.setTo("me3");
newTransaction.setDate(cal.getTime());
db.transactionDao().insertTransaction(newTransaction);
} }
@Rule @Rule
@ -64,7 +54,7 @@ public class TransactionListTest {
@Test @Test
public void numberOfTransactionsInList(){ public void numberOfTransactionsInList(){
onView(withId(R.id.transactionListView)).check(new RecyclerViewItemsCountAssertion(3)); onView(withId(R.id.transactionListView)).check(new RecyclerViewItemsCountAssertion(numberOfTransactions));
} }
@After @After

View file

@ -16,9 +16,12 @@ import java.util.List;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.randomdatagenerators.RandomTransactionsGenerator;
import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel;
import cy.agorise.crystalwallet.views.TransactionListView; import cy.agorise.crystalwallet.views.TransactionListView;
import static cy.agorise.crystalwallet.R.string.transactions;
public class IntroActivity extends LifecycleActivity { public class IntroActivity extends LifecycleActivity {
TransactionListViewModel transactionListViewModel; TransactionListViewModel transactionListViewModel;
@ -29,10 +32,18 @@ public class IntroActivity extends LifecycleActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro); setContentView(R.layout.activity_intro);
/*CrystalDatabase db = CrystalDatabase.getAppDatabase(getApplicationContext());
List<CryptoCoinTransaction> transactions = RandomTransactionsGenerator.generateTransactions(100,1262304001,1496275201,1,999999999);
for(int i=0;i<transactions.size();i++) {
db.transactionDao().insertTransaction(transactions.get(i));
}*/
transactionListView = this.findViewById(R.id.transaction_list); transactionListView = this.findViewById(R.id.transaction_list);
transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class);
LiveData<PagedList<CryptoCoinTransaction>> transactionData = transactionListViewModel.getTransactionList(); LiveData<PagedList<CryptoCoinTransaction>> transactionData = transactionListViewModel.getTransactionList();
transactionListView.setData(null);
transactionData.observe(this, new Observer<PagedList<CryptoCoinTransaction>>() { transactionData.observe(this, new Observer<PagedList<CryptoCoinTransaction>>() {
@Override @Override

View file

@ -0,0 +1,39 @@
package cy.agorise.crystalwallet.randomdatagenerators;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
/**
* Created by Henry Varona on 20/9/2017.
*/
public class RandomTransactionsGenerator {
static public List<CryptoCoinTransaction> generateTransactions(int numberOfTransactions, int minTimestamp, int maxTimestamp, int minAmount, int maxAmount){
ArrayList<CryptoCoinTransaction> result = new ArrayList<CryptoCoinTransaction>();
Random randomGenerator = new Random();
Calendar cal = Calendar.getInstance();
int randomTimeStamp;
int randomAmount;
CryptoCoinTransaction randomTransaction;
for (int i=0;i<numberOfTransactions;i++){
randomAmount = randomGenerator.nextInt((maxAmount - minAmount) + 1) + minAmount;
randomTimeStamp = randomGenerator.nextInt((maxTimestamp - minTimestamp) + 1) + minTimestamp;
cal.setTimeInMillis(randomTimeStamp*1000);
randomTransaction = new CryptoCoinTransaction();
randomTransaction.setAmount(randomAmount);
randomTransaction.setFrom("friend"+i);
randomTransaction.setTo("me"+i);
randomTransaction.setDate(cal.getTime());
result.add(randomTransaction);
}
return result;
}
}

View file

@ -31,7 +31,10 @@ public class TransactionListAdapter extends PagedListAdapter<CryptoCoinTransacti
@Override @Override
public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public TransactionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null; View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.transaction_list_item,parent,false);
return new TransactionViewHolder(v);
} }
@Override @Override

View file

@ -3,6 +3,7 @@ package cy.agorise.crystalwallet.views;
import android.arch.paging.PagedList; import android.arch.paging.PagedList;
import android.arch.paging.PagedListAdapter; import android.arch.paging.PagedListAdapter;
import android.content.Context; import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -51,13 +52,17 @@ public class TransactionListView extends RelativeLayout {
public void init(){ public void init(){
rootView = mInflater.inflate(R.layout.transaction_list, this, true); rootView = mInflater.inflate(R.layout.transaction_list, this, true);
this.listView = rootView.findViewById(R.id.transactionListView); this.listView = rootView.findViewById(R.id.transactionListView);
this.listView.setLayoutManager(new LinearLayoutManager(this.getContext()));
this.listView.setNestedScrollingEnabled(false);
} }
public void setData(PagedList<CryptoCoinTransaction> data){ public void setData(PagedList<CryptoCoinTransaction> data){
if (this.listAdapter == null) { if (this.listAdapter == null) {
this.listAdapter = new TransactionListAdapter(); this.listAdapter = new TransactionListAdapter();
this.listView.setAdapter(this.listAdapter); this.listView.setAdapter(this.listAdapter);
} else { }
if (data != null) {
this.listAdapter.setList(data); this.listAdapter.setList(data);
} }
} }

View file

@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_intro" android:id="@+id/activity_intro"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"

View file

@ -1,10 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"> android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView <android.support.v7.widget.RecyclerView
android:id="@+id/transactionListView" android:id="@+id/transactionListView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" /> android:layout_height="wrap_content"
/>
</LinearLayout> </LinearLayout>

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical"
android:paddingLeft="10dp" android:paddingLeft="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"