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 3d1321b commit e42db36
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* Use the {@link AttendeesFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@SuppressWarnings("PMD.TooManyMethods")
public class AttendeesFragment extends BaseFragment<IAttendeesPresenter> implements IAttendeesView {

private Context context;
Expand All @@ -63,6 +64,12 @@ public class AttendeesFragment extends BaseFragment<IAttendeesPresenter> impleme
@Inject
Lazy<IAttendeesPresenter> 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 @@ -123,11 +130,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 @@ -216,9 +241,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 @@ -230,7 +255,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 @@ -8,16 +8,25 @@
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import org.fossasia.openevent.app.common.app.viewholder.HeaderViewHolder;
import org.fossasia.openevent.app.common.data.models.Attendee;
import org.fossasia.openevent.app.common.data.models.contract.IHeaderProvider;
import org.fossasia.openevent.app.databinding.HeaderLayoutBinding;

import java.util.List;

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,9 @@ public int getAdapterPosition(long l) {
public int getGlobalPosition(int i) {
return -1;
}

public void setSortByName(boolean sortByName) {
this.sortByName = sortByName;
}
}

11 changes: 11 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,16 @@
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>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
<string name="filter">filter</string>
<string name="filter_by_sync">Pending Sync</string>
<string name="filter_by_none">None</string>
<string name="sort_attendee">Sort By</string>
<string name="sort_by_name">Name</string>
<string name="sort_by_ticket_type">Ticket</string>
<string name="publish_success">Your Event is published successfully.</string>
<string name="draft_success">Your Event is unpublished successfully.</string>
<string name="request_token">Request Token</string>
Expand Down

0 comments on commit e42db36

Please sign in to comment.