From 7e51385dc0e9597b313b389e1c520d1e83cf28a3 Mon Sep 17 00:00:00 2001 From: Severiano Jaramillo Date: Tue, 10 Jul 2018 16:19:10 -0500 Subject: [PATCH] Simplify Contacts section --- .../fragments/ContactsFragment.java | 28 +++-- .../viewmodels/ContactListViewModel.java | 10 -- .../crystalwallet/views/ContactListView.java | 111 ------------------ app/src/main/res/layout/fragment_contacts.xml | 5 +- 4 files changed, 22 insertions(+), 132 deletions(-) delete mode 100644 app/src/main/java/cy/agorise/crystalwallet/views/ContactListView.java diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/ContactsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/ContactsFragment.java index 0e06790..54f69cd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/ContactsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/ContactsFragment.java @@ -8,6 +8,8 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -17,12 +19,14 @@ import butterknife.ButterKnife; import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.models.Contact; import cy.agorise.crystalwallet.viewmodels.ContactListViewModel; -import cy.agorise.crystalwallet.views.ContactListView; +import cy.agorise.crystalwallet.views.ContactListAdapter; public class ContactsFragment extends Fragment { - @BindView(R.id.vContactListView) - ContactListView contactListView; + @BindView(R.id.recyclerViewContacts) + RecyclerView recyclerViewContacts; + + ContactListAdapter adapter; public ContactsFragment() { // Required empty public constructor @@ -44,20 +48,26 @@ public class ContactsFragment extends Fragment { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - View v = inflater.inflate(R.layout.fragment_contacts, container, false); - ButterKnife.bind(this, v); + View view = inflater.inflate(R.layout.fragment_contacts, container, false); + ButterKnife.bind(this, view); - ContactListViewModel contactListViewModel = ViewModelProviders.of(this).get(ContactListViewModel.class); - contactListViewModel.init(); + // Configure RecyclerView and its adapter + recyclerViewContacts.setLayoutManager(new LinearLayoutManager(getContext())); + adapter = new ContactListAdapter(); + recyclerViewContacts.setAdapter(adapter); + + // Gets contacts LiveData instance from ContactsViewModel + ContactListViewModel contactListViewModel = + ViewModelProviders.of(this).get(ContactListViewModel.class); LiveData> contactsLiveData = contactListViewModel.getContactList(); contactsLiveData.observe(this, new Observer>() { @Override public void onChanged(@Nullable PagedList contacts) { - contactListView.setData(contacts); + adapter.submitList(contacts); } }); - return v; + return view; } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/ContactListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/ContactListViewModel.java index 51c3836..5a1a512 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/ContactListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/ContactListViewModel.java @@ -32,16 +32,6 @@ public class ContactListViewModel extends AndroidViewModel { ).build(); } - public void init(){ - contactList = new LivePagedListBuilder(this.db.contactDao().contactsByName(), - new PagedList.Config.Builder() - .setEnablePlaceholders(true) - .setPageSize(10) - .setPrefetchDistance(10) - .build() - ).build(); - } - public void init(CryptoNet cryptoNet){ contactList = new LivePagedListBuilder(this.db.contactDao().contactsByNameAndCryptoNet(cryptoNet.name()), new PagedList.Config.Builder() diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/ContactListView.java b/app/src/main/java/cy/agorise/crystalwallet/views/ContactListView.java deleted file mode 100644 index 37d5cfa..0000000 --- a/app/src/main/java/cy/agorise/crystalwallet/views/ContactListView.java +++ /dev/null @@ -1,111 +0,0 @@ -package cy.agorise.crystalwallet.views; - -import android.arch.paging.PagedList; -import android.content.Context; -import android.support.v4.app.Fragment; -import android.support.v7.widget.LinearLayoutManager; -import android.support.v7.widget.RecyclerView; -import android.util.AttributeSet; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.RelativeLayout; - -import cy.agorise.crystalwallet.R; -import cy.agorise.crystalwallet.models.Contact; -import cy.agorise.crystalwallet.viewmodels.ContactListViewModel; - -/** - * Created by Henry Varona on 1/15/2018. - * - * A list view showing the user contacts - */ - -public class ContactListView extends RelativeLayout { - - LayoutInflater mInflater; - - /* - * The root view of this view - */ - View rootView; - /* - * The list view that holds every user contact item - */ - RecyclerView listView; - /* - * The adapter for the previous list view - */ - ContactListAdapter listAdapter; - - ContactListViewModel contactListViewModel; - - /* - * how much contacts will remain to show before the list loads more - */ - private int visibleThreshold = 5; - /* - * if true, the contact list will be loading new data - */ - private boolean loading = true; - - /* - * One of three constructors needed to be inflated from a layout - */ - public ContactListView(Context context){ - super(context); - this.mInflater = LayoutInflater.from(context); - init(); - } - - /* - * One of three constructors needed to be inflated from a layout - */ - public ContactListView(Context context, AttributeSet attrs) { - super(context, attrs); - this.mInflater = LayoutInflater.from(context); - init(); - } - - /* - * One of three constructors needed to be inflated from a layout - */ - public ContactListView(Context context, AttributeSet attrs, int defStyle){ - super(context, attrs, defStyle); - this.mInflater = LayoutInflater.from(context); - init(); - } - - /* - * Initializes this view - */ - public void init(){ - rootView = mInflater.inflate(R.layout.contact_list, this, true); - this.listView = rootView.findViewById(R.id.contactListView); - - final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext()); - this.listView.setLayoutManager(linearLayoutManager); - //Prevents the list to start again when scrolling to the end - //this.listView.setNestedScrollingEnabled(false); - - } - - /* - * Sets the elements data of this view - * - * @param data the contacts that will be showed to the user - */ - public void setData(PagedList data){ - //Initializes the adapter of the contact list - if (this.listAdapter == null) { - this.listAdapter = new ContactListAdapter(); - this.listView.setAdapter(this.listAdapter); - } - - //Sets the data of the transaction list - if (data != null) { - this.listAdapter.submitList(data); - } - } - - -} diff --git a/app/src/main/res/layout/fragment_contacts.xml b/app/src/main/res/layout/fragment_contacts.xml index 95a3da7..043bf37 100644 --- a/app/src/main/res/layout/fragment_contacts.xml +++ b/app/src/main/res/layout/fragment_contacts.xml @@ -4,9 +4,10 @@ android:layout_height="match_parent" tools:context="cy.agorise.crystalwallet.fragments.ContactsFragment"> - + tools:listitem="@layout/contact_list_item" />