From f388f77b492bbec91cf7c6d241d0bd7490bc6e58 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sat, 16 Sep 2017 20:41:10 -0400 Subject: [PATCH] - Now the Transaction List is visible to the user --- app/build.gradle | 2 ++ .../agorise/crystalwallet/IntroActivity.java | 20 +++++++++++++++-- .../application/CrystalApplication.java | 3 +++ .../crystalwallet/dao/TransactionDao.java | 5 +++++ .../viewmodels/TransactionListViewModel.java | 9 +++++--- .../views/TransactionListAdapter.java | 11 +++++++--- .../views/TransactionListView.java | 22 ++++++++++--------- app/src/main/res/layout/activity_intro.xml | 5 ----- .../main/res/layout/transaction_list_item.xml | 2 +- build.gradle | 4 ++++ 10 files changed, 59 insertions(+), 24 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0923225..8f65315 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,4 +39,6 @@ dependencies { annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha9" compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'; apt "android.arch.persistence.room:compiler:1.0.0-alpha9"; + + compile 'com.idescout.sql:sqlscout-server:2.0'; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java index 5024117..191ead1 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java @@ -1,11 +1,20 @@ package cy.agorise.crystalwallet; import android.arch.lifecycle.LifecycleActivity; +import android.arch.lifecycle.LiveData; +import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProvider; import android.arch.lifecycle.ViewModelProviders; import android.os.Bundle; +import android.support.annotation.Nullable; 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.views.TransactionListView; @@ -19,9 +28,16 @@ public class IntroActivity extends LifecycleActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intro); - transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); transactionListView = this.findViewById(R.id.transaction_list); - transactionListView.init(transactionListViewModel); + transactionListViewModel = ViewModelProviders.of(this).get(TransactionListViewModel.class); + LiveData> transactionData = transactionListViewModel.getTransactionList(); + + transactionData.observe(this, new Observer>() { + @Override + public void onChanged(List cryptoCoinTransactions) { + transactionListView.setData(cryptoCoinTransactions); + } + }); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index 8319739..8e00a4d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -2,6 +2,8 @@ package cy.agorise.crystalwallet.application; import android.app.Application; +import com.idescout.sql.SqlScoutServer; + import cy.agorise.crystalwallet.dao.CrystalDatabase; /** @@ -16,5 +18,6 @@ public class CrystalApplication extends Application { //initialize the database CrystalDatabase db = CrystalDatabase.getAppDatabase(this.getApplicationContext()); + SqlScoutServer.create(this, getPackageName()); } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java index 33f4898..62f4601 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/TransactionDao.java @@ -2,6 +2,8 @@ package cy.agorise.crystalwallet.dao; import android.arch.lifecycle.LiveData; 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 java.util.List; @@ -16,4 +18,7 @@ public interface TransactionDao { @Query("SELECT * FROM crypto_coin_transaction") LiveData> getAll(); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + public void insertTransaction(CryptoCoinTransaction... transactions); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java index 3d2b7b7..463f342 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/TransactionListViewModel.java @@ -1,5 +1,7 @@ package cy.agorise.crystalwallet.viewmodels; +import android.app.Application; +import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.ViewModel; import android.content.Context; @@ -14,13 +16,14 @@ import cy.agorise.crystalwallet.views.TransactionListView; * Created by Henry Varona on 12/9/2017. */ -public class TransactionListViewModel extends ViewModel { +public class TransactionListViewModel extends AndroidViewModel { private LiveData> transactionList; private CrystalDatabase db; - public TransactionListViewModel(Context context){ - this.db = CrystalDatabase.getAppDatabase(context); + public TransactionListViewModel(Application application) { + super(application); + this.db = CrystalDatabase.getAppDatabase(application.getApplicationContext()); transactionList = this.db.transactionDao().getAll(); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java index a687097..ca57c53 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListAdapter.java @@ -25,9 +25,14 @@ public class TransactionListAdapter extends ArrayAdapter //List items; - public TransactionListAdapter(Context context, LiveData> items) { - super(context, 0, items.getValue()); + public TransactionListAdapter(Context context, List items) { + super(context, 0, items); + } + public void updateData(List items){ + this.clear(); + this.addAll(items); + this.notifyDataSetChanged(); } @Override @@ -44,7 +49,7 @@ public class TransactionListAdapter extends ArrayAdapter tvFrom.setText(transaction.getFrom()); tvTo.setText(transaction.getTo()); - tvAmount.setText(transaction.getAmount()); + tvAmount.setText(""+transaction.getAmount()); return convertView; } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java index 3d70086..c5b58c3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionListView.java @@ -1,16 +1,15 @@ package cy.agorise.crystalwallet.views; -import android.arch.lifecycle.ViewModelProviders; import android.content.Context; import android.util.AttributeSet; import android.view.View; -import android.widget.ListAdapter; import android.widget.ListView; import android.widget.RelativeLayout; +import java.util.List; + import cy.agorise.crystalwallet.R; -import cy.agorise.crystalwallet.dao.CrystalDatabase; -import cy.agorise.crystalwallet.dao.TransactionDao; +import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.viewmodels.TransactionListViewModel; /** @@ -21,20 +20,23 @@ public class TransactionListView extends RelativeLayout { View rootView; ListView listView; - ListAdapter listAdapter; + TransactionListAdapter listAdapter; TransactionListViewModel transactionListViewModel; public TransactionListView(Context context, AttributeSet attrs) { super(context, attrs); 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){ - this.transactionListViewModel = transactionListViewModel; - listAdapter = new TransactionListAdapter(this.getContext(), transactionListViewModel.getTransactionList()); - listView.setAdapter(listAdapter); + public void setData(List data){ + if (this.listAdapter == null) { + this.listAdapter = new TransactionListAdapter(this.getContext(), data); + this.listView.setAdapter(this.listAdapter); + } else { + this.listAdapter.updateData(data); + } } diff --git a/app/src/main/res/layout/activity_intro.xml b/app/src/main/res/layout/activity_intro.xml index e7dbdb0..f452a80 100644 --- a/app/src/main/res/layout/activity_intro.xml +++ b/app/src/main/res/layout/activity_intro.xml @@ -10,11 +10,6 @@ android:paddingTop="@dimen/activity_vertical_margin" tools:context="cy.agorise.crystalwallet.IntroActivity"> - - \ No newline at end of file diff --git a/build.gradle b/build.gradle index 6dbbf1f..e25ab03 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,10 @@ allprojects { maven { // For Room Persistence Library url "https://maven.google.com" + + } + maven { + url 'http://www.idescout.com/maven/repo/' } } }