- Now the Transaction List is visible to the user

This commit is contained in:
Javier Varona 2017-09-16 20:41:10 -04:00
parent d41518de0f
commit f388f77b49
10 changed files with 59 additions and 24 deletions

View file

@ -39,4 +39,6 @@ dependencies {
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9" annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9"
compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'; compile 'android.arch.persistence.room:runtime:1.0.0-alpha9';
apt "android.arch.persistence.room:compiler:1.0.0-alpha9"; apt "android.arch.persistence.room:compiler:1.0.0-alpha9";
compile 'com.idescout.sql:sqlscout-server:2.0';
} }

View file

@ -1,11 +1,20 @@
package cy.agorise.crystalwallet; package cy.agorise.crystalwallet;
import android.arch.lifecycle.LifecycleActivity; import android.arch.lifecycle.LifecycleActivity;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider; import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders; import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import java.util.List;
import cy.agorise.crystalwallet.dao.CrystalDatabase;
import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel;
import cy.agorise.crystalwallet.views.TransactionListView; import cy.agorise.crystalwallet.views.TransactionListView;
@ -19,9 +28,16 @@ public class IntroActivity extends LifecycleActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro); setContentView(R.layout.activity_intro);
transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class);
transactionListView = this.findViewById(R.id.transaction_list); transactionListView = this.findViewById(R.id.transaction_list);
transactionListView.init(transactionListViewModel); transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class);
LiveData<List<CryptoCoinTransaction>> transactionData = transactionListViewModel.getTransactionList();
transactionData.observe(this, new Observer<List<CryptoCoinTransaction>>() {
@Override
public void onChanged(List<CryptoCoinTransaction> cryptoCoinTransactions) {
transactionListView.setData(cryptoCoinTransactions);
}
});
} }
} }

View file

@ -2,6 +2,8 @@ package cy.agorise.crystalwallet.application;
import android.app.Application; import android.app.Application;
import com.idescout.sql.SqlScoutServer;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.dao.CrystalDatabase;
/** /**
@ -16,5 +18,6 @@ public class CrystalApplication extends Application {
//initialize the database //initialize the database
CrystalDatabase db = CrystalDatabase.getAppDatabase(this.getApplicationContext()); CrystalDatabase db = CrystalDatabase.getAppDatabase(this.getApplicationContext());
SqlScoutServer.create(this, getPackageName());
} }
} }

View file

@ -2,6 +2,8 @@ package cy.agorise.crystalwallet.dao;
import android.arch.lifecycle.LiveData; import android.arch.lifecycle.LiveData;
import android.arch.persistence.room.Dao; import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query; import android.arch.persistence.room.Query;
import java.util.List; import java.util.List;
@ -16,4 +18,7 @@ public interface TransactionDao {
@Query("SELECT * FROM crypto_coin_transaction") @Query("SELECT * FROM crypto_coin_transaction")
LiveData<List<CryptoCoinTransaction>> getAll(); LiveData<List<CryptoCoinTransaction>> getAll();
@Insert(onConflict = OnConflictStrategy.REPLACE)
public void insertTransaction(CryptoCoinTransaction... transactions);
} }

View file

@ -1,5 +1,7 @@
package cy.agorise.crystalwallet.viewmodels; package cy.agorise.crystalwallet.viewmodels;
import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData; import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.ViewModel; import android.arch.lifecycle.ViewModel;
import android.content.Context; import android.content.Context;
@ -14,13 +16,14 @@ import cy.agorise.crystalwallet.views.TransactionListView;
* Created by Henry Varona on 12/9/2017. * Created by Henry Varona on 12/9/2017.
*/ */
public class TransactionListViewModel extends ViewModel { public class TransactionListViewModel extends AndroidViewModel {
private LiveData<List<CryptoCoinTransaction>> transactionList; private LiveData<List<CryptoCoinTransaction>> transactionList;
private CrystalDatabase db; private CrystalDatabase db;
public TransactionListViewModel(Context context){ public TransactionListViewModel(Application application) {
this.db = CrystalDatabase.getAppDatabase(context); super(application);
this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext());
transactionList = this.db.transactionDao().getAll(); transactionList = this.db.transactionDao().getAll();
} }

View file

@ -25,9 +25,14 @@ public class TransactionListAdapter extends ArrayAdapter<CryptoCoinTransaction>
//List<CryptoCoinTransaction> items; //List<CryptoCoinTransaction> items;
public TransactionListAdapter(Context context, LiveData<List<CryptoCoinTransaction>> items) { public TransactionListAdapter(Context context, List<CryptoCoinTransaction> items) {
super(context, 0, items.getValue()); super(context, 0, items);
}
public void updateData(List<CryptoCoinTransaction> items){
this.clear();
this.addAll(items);
this.notifyDataSetChanged();
} }
@Override @Override
@ -44,7 +49,7 @@ public class TransactionListAdapter extends ArrayAdapter<CryptoCoinTransaction>
tvFrom.setText(transaction.getFrom()); tvFrom.setText(transaction.getFrom());
tvTo.setText(transaction.getTo()); tvTo.setText(transaction.getTo());
tvAmount.setText(transaction.getAmount()); tvAmount.setText(""+transaction.getAmount());
return convertView; return convertView;
} }

View file

@ -1,16 +1,15 @@
package cy.agorise.crystalwallet.views; package cy.agorise.crystalwallet.views;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import java.util.List;
import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.models.CryptoCoinTransaction;
import cy.agorise.crystalwallet.dao.TransactionDao;
import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel;
/** /**
@ -21,20 +20,23 @@ public class TransactionListView extends RelativeLayout {
View rootView; View rootView;
ListView listView; ListView listView;
ListAdapter listAdapter; TransactionListAdapter listAdapter;
TransactionListViewModel transactionListViewModel; TransactionListViewModel transactionListViewModel;
public TransactionListView(Context context, AttributeSet attrs) { public TransactionListView(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
rootView = inflate(context, R.layout.transaction_list, this); rootView = inflate(context, R.layout.transaction_list, this);
listView = rootView.findViewById(R.id.transactionListView); this.listView = rootView.findViewById(R.id.transactionListView);
} }
public void init(TransactionListViewModel transactionListViewModel){ public void setData(List<CryptoCoinTransaction> data){
this.transactionListViewModel = transactionListViewModel; if (this.listAdapter == null) {
listAdapter = new TransactionListAdapter(this.getContext(), transactionListViewModel.getTransactionList()); this.listAdapter = new TransactionListAdapter(this.getContext(), data);
listView.setAdapter(listAdapter); this.listView.setAdapter(this.listAdapter);
} else {
this.listAdapter.updateData(data);
}
} }

View file

@ -10,11 +10,6 @@
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cy.agorise.crystalwallet.IntroActivity"> tools:context="cy.agorise.crystalwallet.IntroActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<cy.agorise.crystalwallet.views.TransactionListView <cy.agorise.crystalwallet.views.TransactionListView
android:id="@+id/transaction_list" android:id="@+id/transaction_list"
android:layout_width="match_parent" android:layout_width="match_parent"

View file

@ -41,7 +41,7 @@
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:ems="10" android:ems="10"
android:gravity="center" android:gravity="center"
android:inputType="number" android:inputType="text"
android:text="amount" /> android:text="amount" />
</RelativeLayout> </RelativeLayout>
</LinearLayout> </LinearLayout>

View file

@ -18,6 +18,10 @@ allprojects {
maven { maven {
// For Room Persistence Library // For Room Persistence Library
url "https://maven.google.com" url "https://maven.google.com"
}
maven {
url 'http://www.idescout.com/maven/repo/'
} }
} }
} }