Skip to content

Commit

Permalink
feat: sort attendee list on the basis of ticket type (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD committed Mar 28, 2018
1 parent 88d2ad4 commit 791509b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ public class AttendeesFragment extends BaseFragment<AttendeesPresenter> implemen
@Inject
Lazy<AttendeesPresenter> presenterProvider;

private static final int SORTBYTICKET = 1;
private static final int SORTBYNAME = 0;

FastAdapter<Attendee> fastAdapter;
StickyHeaderAdapter<Attendee> stickyHeaderAdapter;

private ItemAdapter<Attendee> fastItemAdapter;
private FragmentAttendeesBinding binding;
private SwipeRefreshLayout refreshLayout;
Expand Down Expand Up @@ -121,11 +127,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) {
Expand Down Expand Up @@ -209,9 +233,9 @@ private void setupRecyclerView() {
}
);

StickyHeaderAdapter<Attendee> stickyHeaderAdapter = new StickyHeaderAdapter<>();

FastAdapter<Attendee> 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));

Expand All @@ -223,6 +247,12 @@ private void setupRecyclerView() {

final StickyRecyclerHeadersDecoration decoration = new StickyRecyclerHeadersDecoration(stickyHeaderAdapter);
recyclerView.addItemDecoration(decoration);
fastAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
decoration.invalidateHeaders();
}
});

ViewUtils.setRecyclerViewScrollAwareFabBehaviour(recyclerView, binding.fabScanQr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,9 +16,17 @@

class StickyHeaderAdapter<T extends IItem & IHeaderProvider> extends AbstractAdapter<T>
implements StickyRecyclerHeadersAdapter<HeaderViewHolder> {

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
Expand All @@ -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
Expand Down Expand Up @@ -69,4 +82,8 @@ public int getAdapterPosition(long l) {
public int getGlobalPosition(int i) {
return -1;
}

public void setSortByName(boolean sortByName) {
this.sortByName = sortByName;
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/menu/menu_attendees.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@
android:title="@string/filter_by_sync" />
</menu>
</item>
<item android:id="@+id/sortAttendee"
android:title="@string/sort_attendee"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/sortByName"
android:title="@string/sort_by_name" />
<item android:id="@+id/sortByTicket"
android:title="@string/sort_by_ticket_type" />
</menu>
</item>

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
<string name="create_event">Create Event</string>
<string name="event_name">Event Name</string>
<string name="payment_currency">Payment Currency</string>
<string name="sort_attendee">Sort By</string>
<string name="sort_by_ticket_type">Ticket</string>
<string name="date_and_time">Date and Time</string>
<string name="timezone">Timezone</string>
<string name="location_details">Location Details</string>
Expand Down

0 comments on commit 791509b

Please sign in to comment.