Thursday, 6 July 2017

design patterns - Android MVP architecture - communication between repository and view

I'm learning Android MVP architecture given here. Based on the example I created my own simple app that just displays list with recyclerview with pull to refresh functionality.



When list is pulled I want first to clear the recyclerview and then reload the data again from the repository (with fake latency delay). But, when I clear the data inside RecyclerViewAdpater, all data inside repository are also being cleared and there is nothing to show. I just can't understand the reason.



Here's my activity that creates both Presenter and View (that is Fragment):



    protected void onCreate(Bundle savedInstanceState) {

mainFragment = MainFragment.newInstance();


FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.contentFrame, mainFragment);
transaction.commit();

new MainPresenter(new MainDataRepository(), mainFragment);
}


Here's my Fragment (MainFragment) - ATTENTION: updateList(null) - This is where it clears all data, including inside Repository:




    public MainFragment() {
}

public static MainFragment newInstance() {
return new MainFragment();
}

@Nullable
@Override

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//....

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
recyclerAdapter.updateList(null); // **This is where it clears all data, including inside Repository**

Handler handler = new Handler();
handler.postDelayed(new Runnable() {

@Override
public void run() {
mPresenter.loadList(); // to reload the list
}
}, 1500);
}
});

//...
}


@Override
public void onResume() {
super.onResume();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mPresenter.loadList();

}
}, 3000);
}

@Override
public void setPresenter(MainContractor.Presenter presenter) {
mPresenter = presenter;
}

@Override

public void showList(List mainDataList) {
if(recyclerAdapter == null) {
recyclerAdapter = new RecyclerAdapter(mainDataList);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(recyclerAdapter);
}else{
recyclerAdapter.updateList(mainDataList);
}
}



Here's my presenter (MainPresenter):



    public MainPresenter(MainDataRepository mainDataRepository, MainContractor.View view){
mMainDataRepository = mainDataRepository;
mMainContractorView = view;

view.setPresenter(this);
}


@Override
public void loadList() {
ArrayList strings = mMainDataRepository.getList();

mMainContractorView.showList(strings);
}


Here's my repository (MainDataRepository):




private ArrayList repositoryList;

public MainDataRepository() {
createList();
}

private void createList() {
if(repositoryList == null){
repositoryList = new ArrayList<>();
}


for (int i = 1; i <= 10; i++) {
repositoryList.add("Item " + i);
}
}

public ArrayList getList(){
return repositoryList;
}



And the last one, This is how I am updating recyclerview inside RecyclerAdapter:



public class RecyclerAdapter extends RecyclerView.Adapter {

private List stringsList;

public RecyclerAdapter(List stringsList) {
this.stringsList = stringsList;
}


public void updateList(List newStrings){
if(newStrings != null){
stringsList = newStrings;
}else{
stringsList.clear();
}
notifyDataSetChanged();
}


//....
}


Why updateList mehod inside RecyclerAdapter also clears the data inside repository that is ArrayList repositoryList?

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...