From 25ce4b34e323d8e63a2aa8f11d7ff02cc684cd47 Mon Sep 17 00:00:00 2001 From: AkshayCHD Date: Wed, 28 Mar 2018 22:44:51 +0530 Subject: [PATCH] feat: sort attendee list on the basis of ticket type (#781) --- .../core/attendee/list/AttendeesFragment.java | 49 +++++++++++++++++-- .../attendee/list/StickyHeaderAdapter.java | 21 +++++++- app/src/main/res/menu/menu_attendees.xml | 10 ++++ app/src/main/res/values/strings.xml | 1 + 4 files changed, 75 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/AttendeesFragment.java b/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/AttendeesFragment.java index 782592f0f..42ca63328 100644 --- a/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/AttendeesFragment.java +++ b/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/AttendeesFragment.java @@ -49,6 +49,8 @@ * Use the {@link AttendeesFragment#newInstance} factory method to * create an instance of this fragment. */ + +@SuppressWarnings("PMD.TooManyMethods") public class AttendeesFragment extends BaseFragment implements IAttendeesView { private Context context; @@ -61,6 +63,12 @@ public class AttendeesFragment extends BaseFragment implemen @Inject Lazy presenterProvider; + private static final int SORTBYTICKET = 1; + private static final int SORTBYNAME = 0; + + private FastAdapter fastAdapter; + private StickyHeaderAdapter stickyHeaderAdapter; + private ItemAdapter fastItemAdapter; private FragmentAttendeesBinding binding; private SwipeRefreshLayout refreshLayout; @@ -68,6 +76,8 @@ public class AttendeesFragment extends BaseFragment implemen private boolean initialized; + private RecyclerView.AdapterDataObserver observer; + private static final String FILTER_SYNC = "FILTER_SYNC"; public AttendeesFragment() { @@ -104,6 +114,12 @@ public void onCreate(Bundle savedInstanceState) { eventId = getArguments().getLong(MainActivity.EVENT_KEY); } + @Override + public void onDestroy() { + super.onDestroy(); + fastAdapter.unregisterAdapterDataObserver(observer); + } + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.menu_attendees, menu); MenuItem search = menu.findItem(R.id.search); @@ -121,11 +137,29 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.filterBySync: fastItemAdapter.filter(FILTER_SYNC); return true; + case R.id.sortByTicket: + sortAttendees(SORTBYTICKET); + return true; + case R.id.sortByName: + sortAttendees(SORTBYNAME); + return true; default: return super.onOptionsItemSelected(item); } } + void sortAttendees(int sortBy) { + if (sortBy == SORTBYTICKET) { + fastItemAdapter.withComparator((Attendee a1, Attendee a2) -> a1.getTicket().getType().compareTo(a2.getTicket().getType())); + } else { + fastItemAdapter.withComparator((Attendee a1, Attendee a2) -> a1.getFirstname().compareTo(a2.getFirstname()), true); + } + fastItemAdapter.setNewList(getPresenter().getAttendees()); + stickyHeaderAdapter.setSortByName(sortBy == SORTBYTICKET); + binding.setVariable(BR.attendees, getPresenter().getAttendees()); + binding.executePendingBindings(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -209,9 +243,9 @@ private void setupRecyclerView() { } ); - StickyHeaderAdapter stickyHeaderAdapter = new StickyHeaderAdapter<>(); - - FastAdapter fastAdapter = FastAdapter.with(Arrays.asList(fastItemAdapter, stickyHeaderAdapter)); + stickyHeaderAdapter = new StickyHeaderAdapter<>(); + stickyHeaderAdapter.setSortByName(false); + fastAdapter = FastAdapter.with(Arrays.asList(fastItemAdapter, stickyHeaderAdapter)); fastAdapter.setHasStableIds(true); fastAdapter.withEventHook(new AttendeeItemCheckInEvent(this)); @@ -223,7 +257,14 @@ private void setupRecyclerView() { final StickyRecyclerHeadersDecoration decoration = new StickyRecyclerHeadersDecoration(stickyHeaderAdapter); recyclerView.addItemDecoration(decoration); - + observer = new RecyclerView.AdapterDataObserver() { + @Override + public void onChanged() { + super.onChanged(); + decoration.invalidateHeaders(); + } + }; + fastAdapter.registerAdapterDataObserver(observer); ViewUtils.setRecyclerViewScrollAwareFabBehaviour(recyclerView, binding.fabScanQr); } } diff --git a/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/StickyHeaderAdapter.java b/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/StickyHeaderAdapter.java index df7f4c273..704dadfd3 100644 --- a/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/StickyHeaderAdapter.java +++ b/app/src/main/java/org/fossasia/openevent/app/core/attendee/list/StickyHeaderAdapter.java @@ -7,6 +7,7 @@ import com.mikepenz.fastadapter.IItem; import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter; +import org.fossasia.openevent.app.data.models.Attendee; import org.fossasia.openevent.app.ui.HeaderViewHolder; import org.fossasia.openevent.app.data.models.delegates.IHeaderProvider; import org.fossasia.openevent.app.databinding.HeaderLayoutBinding; @@ -15,9 +16,17 @@ class StickyHeaderAdapter extends AbstractAdapter implements StickyRecyclerHeadersAdapter { + + private boolean sortByName; + @Override public long getHeaderId(int position) { - return getFastAdapter().getItem(position).getHeaderId(); + if (sortByName) { + Attendee attendee = (Attendee) getFastAdapter().getItem(position); + return attendee.getTicket().getType().hashCode(); + } else { + return getFastAdapter().getItem(position).getHeaderId(); + } } @Override @@ -27,7 +36,11 @@ public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) { @Override public void onBindHeaderViewHolder(HeaderViewHolder viewHolder, int position) { - viewHolder.bindHeader(getFastAdapter().getItem(position).getHeader()); + if (sortByName) { + Attendee attendee = (Attendee) getFastAdapter().getItem(position); + viewHolder.bindHeader(attendee.getTicket().getType()); + } else + viewHolder.bindHeader(getFastAdapter().getItem(position).getHeader()); } @Override @@ -69,4 +82,8 @@ public int getAdapterPosition(long l) { public int getGlobalPosition(int i) { return -1; } + + public void setSortByName(boolean sortByName) { + this.sortByName = sortByName; + } } diff --git a/app/src/main/res/menu/menu_attendees.xml b/app/src/main/res/menu/menu_attendees.xml index 1c9ce011d..5c762108f 100644 --- a/app/src/main/res/menu/menu_attendees.xml +++ b/app/src/main/res/menu/menu_attendees.xml @@ -19,5 +19,15 @@ android:title="@string/filter_by_sync" /> + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 60d3c380e..728b3e920 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -106,6 +106,7 @@ Create Event Event Name Payment Currency + Ticket Date and Time Timezone Location Details