Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RecycleView adapters refactoring #360

Open
mikolevy opened this issue Sep 9, 2018 · 0 comments
Open

RecycleView adapters refactoring #360

mikolevy opened this issue Sep 9, 2018 · 0 comments

Comments

@mikolevy
Copy link
Collaborator

mikolevy commented Sep 9, 2018

In all RecyclerViewAdapter classes we have some common, not needed code. We should refactor it to remove this code.

  1. Initialization
    It's always done in three steps:
  • Create an adapter with the constructor (planListAdapter = new PlanRecyclerViewAdapter(planItemClickListener);)
  • set adapter in recycle view (recyclerView.setAdapter(planListAdapter);)
  • set adapter items via setter method (planListAdapter.setPlanItems(planTemplateRepository.getAll());)

We can pass data in constructor to avoid initialization with empty ArrayList and extra setter call:

planListAdapter = new PlanRecyclerViewAdapter(planTemplateRepository.getAll(), planItemClickListener);
recyclerView.setAdapter(planListAdapter);
  1. onBindViewHolder
    Currently, we do checking against null or empty values list: (planItemList != null && !planItemList.isEmpty())

Actually we don't need those checks, cause:

  • value list should never be null (it's inited in the constructor with a list of values from the repo, or updated later via setter with the same kind of data). Setting null value should be considered as programming error and should cause app crash instead of undefined behavior.
  • If the list is empty there is nothing to display so Android won't call this method
    We can just remove those checks
  1. getItemCount
    Currently, we do checking against null or empty values list: return planItemList != null && planItemList.size() != 0 ? planItemList.size() : 0;
  • List won't be null (as described in 2) )
  • We don't need special treatment for the situation when list.size() == 0 cause return list.size() already resolves it (return list.size() != 0 ? list.size() : 0; is identical to return list.size();
    We can refactor it to simply return list.size();
dorotatomczak added a commit to dorotatomczak/friendly-plans that referenced this issue Nov 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant