Simplify Contacts section

This commit is contained in:
Severiano Jaramillo 2018-07-10 16:19:10 -05:00
parent ff2a58bcf8
commit 7e51385dc0
4 changed files with 22 additions and 132 deletions

View file

@ -8,6 +8,8 @@ import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; 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.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -17,12 +19,14 @@ import butterknife.ButterKnife;
import cy.agorise.crystalwallet.R; import cy.agorise.crystalwallet.R;
import cy.agorise.crystalwallet.models.Contact; import cy.agorise.crystalwallet.models.Contact;
import cy.agorise.crystalwallet.viewmodels.ContactListViewModel; import cy.agorise.crystalwallet.viewmodels.ContactListViewModel;
import cy.agorise.crystalwallet.views.ContactListView; import cy.agorise.crystalwallet.views.ContactListAdapter;
public class ContactsFragment extends Fragment { public class ContactsFragment extends Fragment {
@BindView(R.id.vContactListView) @BindView(R.id.recyclerViewContacts)
ContactListView contactListView; RecyclerView recyclerViewContacts;
ContactListAdapter adapter;
public ContactsFragment() { public ContactsFragment() {
// Required empty public constructor // Required empty public constructor
@ -44,20 +48,26 @@ public class ContactsFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_contacts, container, false); View view = inflater.inflate(R.layout.fragment_contacts, container, false);
ButterKnife.bind(this, v); ButterKnife.bind(this, view);
ContactListViewModel contactListViewModel = ViewModelProviders.of(this).get(ContactListViewModel.class); // Configure RecyclerView and its adapter
contactListViewModel.init(); 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<PagedList<Contact>> contactsLiveData = contactListViewModel.getContactList(); LiveData<PagedList<Contact>> contactsLiveData = contactListViewModel.getContactList();
contactsLiveData.observe(this, new Observer<PagedList<Contact>>() { contactsLiveData.observe(this, new Observer<PagedList<Contact>>() {
@Override @Override
public void onChanged(@Nullable PagedList<Contact> contacts) { public void onChanged(@Nullable PagedList<Contact> contacts) {
contactListView.setData(contacts); adapter.submitList(contacts);
} }
}); });
return v; return view;
} }
} }

View file

@ -32,16 +32,6 @@ public class ContactListViewModel extends AndroidViewModel {
).build(); ).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){ public void init(CryptoNet cryptoNet){
contactList = new LivePagedListBuilder(this.db.contactDao().contactsByNameAndCryptoNet(cryptoNet.name()), contactList = new LivePagedListBuilder(this.db.contactDao().contactsByNameAndCryptoNet(cryptoNet.name()),
new PagedList.Config.Builder() new PagedList.Config.Builder()

View file

@ -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<Contact> 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);
}
}
}

View file

@ -4,9 +4,10 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="cy.agorise.crystalwallet.fragments.ContactsFragment"> tools:context="cy.agorise.crystalwallet.fragments.ContactsFragment">
<cy.agorise.crystalwallet.views.ContactListView <android.support.v7.widget.RecyclerView
android:id="@+id/recyclerViewContacts"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:id="@+id/vContactListView" /> tools:listitem="@layout/contact_list_item" />
</FrameLayout> </FrameLayout>