Skip to content

Commit

Permalink
feat: sort attendee list on the basis of ticket type (#781) (#782)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD authored and iamareebjamal committed Jul 16, 2018
1 parent 85979f9 commit 9a08aa6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 @@ -60,13 +62,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";

/**
Expand Down Expand Up @@ -97,6 +107,14 @@ public void onCreate(Bundle savedInstanceState) {
eventId = getArguments().getLong(MainActivity.EVENT_KEY);
}

@Override
public void onDestroy() {
super.onDestroy();
if (observer != null) {
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 @@ -114,11 +132,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);
}
}

private 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 @@ -202,9 +238,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 @@ -216,7 +252,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;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M6,13H18V11H6M3,6V8H21V6M10,18H14V16H10V18Z" />
<path android:fillColor="#000" android:pathData="M3,2H21V2H21V4H20.92L14,10.92V22.91L10,18.91V10.91L3.09,4H3V2Z" />
</vector>
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/ic_sort.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M3,13H15V11H3M3,6V8H21V6M3,18H9V16H3V18Z" />
</vector>
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_by"
android:icon="@drawable/ic_sort"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/sortByName"
android:title="@string/sort_by_name" />
<item android:id="@+id/sortByTicket"
android:title="@string/ticket" />
</menu>
</item>

</menu>
2 changes: 1 addition & 1 deletion app/src/main/res/menu/menu_events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<item android:id="@+id/sort"
android:title="@string/sort_by"
android:icon="@drawable/ic_filter"
android:icon="@drawable/ic_sort"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/sortByEventName"
Expand Down
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="ticket">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 9a08aa6

Please sign in to comment.