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 16, 2018
1 parent 3d1321b commit 8ab4d1f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.support.design.widget.BottomSheetDialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
Expand All @@ -18,6 +19,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;

import com.android.databinding.library.baseAdapters.BR;
import com.mikepenz.fastadapter.FastAdapter;
Expand All @@ -39,7 +41,9 @@
import org.fossasia.openevent.app.module.attendee.qrscan.ScanQRActivity;
import org.fossasia.openevent.app.module.main.MainActivity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.inject.Inject;
Expand All @@ -51,6 +55,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 +68,12 @@ public class AttendeesFragment extends BaseFragment<IAttendeesPresenter> impleme
@Inject
Lazy<IAttendeesPresenter> presenterProvider;

private List<Attendee> listAttendee;

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


private ItemAdapter<Attendee> fastItemAdapter;
private FragmentAttendeesBinding binding;
private SwipeRefreshLayout refreshLayout;
Expand Down Expand Up @@ -102,6 +113,8 @@ public void onCreate(Bundle savedInstanceState) {
context = getActivity();
setHasOptionsMenu(true);

listAttendee = new ArrayList<>();

if (getArguments() != null)
eventId = getArguments().getLong(MainActivity.EVENT_KEY);
}
Expand All @@ -123,11 +136,44 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.filterBySync:
fastItemAdapter.filter(FILTER_SYNC);
return true;

case R.id.sort:
sortAttendees();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

void sortAttendees() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Sort By");
LayoutInflater inflater = getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.attendee_sort_layout, null);

alert.setView(alertLayout);
alert.setPositiveButton("Sort",
(dialog, which) -> {
RadioButton firstnameRadio = ((AlertDialog) dialog).findViewById(R.id.sortByFirstname);
RadioButton ticketTypeRadio = ((AlertDialog) dialog).findViewById(R.id.sortByTicketType);

if (firstnameRadio.isChecked()) {
viewResults(listAttendee, SORTBYNAME);
dialog.dismiss();
}
if (ticketTypeRadio.isChecked()) {
viewResults(listAttendee, SORTBYTICKET);
dialog.dismiss();
}
}
);

AlertDialog dialog = alert.create();
dialog.show();

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand Down Expand Up @@ -268,6 +314,17 @@ public void showScanButton(boolean show) {

@Override
public void showResults(List<Attendee> attendees) {
listAttendee.clear();
listAttendee.addAll(attendees);
viewResults(attendees, SORTBYNAME);
}

public void viewResults(List<Attendee> attendees, int sortBy) {
if (sortBy == SORTBYTICKET) {
Collections.sort(attendees, (Attendee a1, Attendee a2) -> a1.getTicket().getType().compareTo(a2.getTicket().getType()));
} else {
Collections.sort(attendees, (Attendee a1, Attendee a2) -> a1.getFirstname().compareTo(a2.getFirstname()));
}
fastItemAdapter.setNewList(attendees);
binding.setVariable(BR.attendees, attendees);
binding.executePendingBindings();
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/layout/attendee_sort_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<RadioButton
android:id="@+id/sortByFirstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sort_by_firstname"
android:layout_marginTop="@dimen/spacing_medium"
android:layout_marginStart="@dimen/spacing_large"
android:layout_marginLeft="@dimen/spacing_large" />

<RadioButton
android:id="@+id/sortByTicketType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/spacing_large"
android:text="@string/sort_by_tickertype"
android:layout_marginStart="@dimen/spacing_large" />
</RadioGroup>

3 changes: 3 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,8 @@
android:title="@string/filter_by_sync" />
</menu>
</item>
<item android:id="@+id/sort"
android:title="@string/sort" />


</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 @@ -157,6 +157,9 @@
<string name="speakers">Speakers</string>
<string name="sponsors">Sponsors</string>
<string name="sessions">Sessions</string>
<string name="sort_by_firstname">First Name</string>
<string name="sort_by_tickertype">Ticket Type</string>
<string name="sort">Sort</string>
<string name="sessions_submitted">Sessions Submitted</string>
<string name="sessions_accepted">Sessions Accepted</string>
<string name="sessions_confirmed">Sessions Confirmed</string>
Expand Down

0 comments on commit 8ab4d1f

Please sign in to comment.