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 25ce4b3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<AttendeesPresenter> implements IAttendeesView {

private Context context;
Expand All @@ -61,13 +63,21 @@ public class AttendeesFragment extends BaseFragment<AttendeesPresenter> implemen
@Inject
Lazy<AttendeesPresenter> presenterProvider;

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

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

private ItemAdapter<Attendee> fastItemAdapter;
private FragmentAttendeesBinding binding;
private SwipeRefreshLayout refreshLayout;
private SearchView searchView;

private boolean initialized;

private RecyclerView.AdapterDataObserver observer;

private static final String FILTER_SYNC = "FILTER_SYNC";

public AttendeesFragment() {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -209,9 +243,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,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);
}
}
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_by"
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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<string name="create_event">Create Event</string>
<string name="event_name">Event Name</string>
<string name="payment_currency">Payment Currency</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 25ce4b3

Please sign in to comment.