Skip to content

Commit

Permalink
feat: sort events alphabetically (fossasia#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD committed Mar 17, 2018
1 parent 3d1321b commit 185c444
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

Expand Down Expand Up @@ -51,6 +54,10 @@ public class EventListFragment extends BaseFragment<IEventsPresenter> implements
@Inject
Lazy<IEventsPresenter> presenterProvider;

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


private FragmentEventListBinding binding;
private RecyclerView recyclerView;
private SwipeRefreshLayout refreshLayout;
Expand Down Expand Up @@ -84,13 +91,37 @@ public static EventListFragment newInstance() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
setHasOptionsMenu(true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_event_list, container, false);
return binding.getRoot();

}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_events, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sortByEventName:
initialized = false;
setupRecyclerView(SORTBYNAME);
initialized = true;
return true;
case R.id.sortByEventDate:
getPresenter().loadUserEvents(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
Expand All @@ -106,7 +137,7 @@ public int getLoaderId() {
@Override
public void onStart() {
super.onStart();
setupRecyclerView();
setupRecyclerView(SORTBYNAME);
setupRefreshListener();
getPresenter().attach(this);
binding.setEvents(getPresenter().getEvents());
Expand Down Expand Up @@ -134,9 +165,9 @@ public void onStop() {
eventListAdapter.unregisterAdapterDataObserver(adapterDataObserver);
}

private void setupRecyclerView() {
private void setupRecyclerView(int sortBy) {
if (!initialized) {
eventListAdapter = new EventsListAdapter(getPresenter().getEvents(), bus);
eventListAdapter = new EventsListAdapter(getPresenter().getEvents(), bus, sortBy);

recyclerView = binding.eventRecyclerView;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.fossasia.openevent.app.databinding.EventLayoutBinding;
import org.fossasia.openevent.app.databinding.HeaderLayoutBinding;

import java.util.Collections;
import java.util.List;

class EventsListAdapter extends RecyclerView.Adapter<EventsListAdapter.EventRecyclerViewHolder>
Expand All @@ -21,7 +22,10 @@ class EventsListAdapter extends RecyclerView.Adapter<EventsListAdapter.EventRecy
private final List<Event> events;
private final IBus bus;

EventsListAdapter(List<Event> events, IBus bus) {
EventsListAdapter(List<Event> events, IBus bus, int sortBy) {
if (sortBy == 1) {
Collections.sort(events, (Event e1, Event e2) -> e1.getName().compareToIgnoreCase(e2.getName()));
}
this.events = events;
this.bus = bus;
}
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/res/menu/menu_events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/logout"
android:title="@string/logout"
android:icon="@drawable/ic_logout"
app:showAsAction="always"/>
<item android:id="@+id/sort"
android:title="@string/sort"
android:icon="@drawable/ic_filter"
app:showAsAction="ifRoom">
<menu>
<item android:id="@+id/sortByEventName"
android:title="@string/sort_by_name" />
<item android:id="@+id/sortByEventDate"
android:title="@string/sort_by_date" />
</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 @@ -157,6 +157,9 @@
<string name="speakers">Speakers</string>
<string name="sponsors">Sponsors</string>
<string name="sessions">Sessions</string>
<string name="sort">Sort By</string>
<string name="sort_by_name">Name</string>
<string name="sort_by_date">Date</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 185c444

Please sign in to comment.