From 185c444d891e854cbe3b351b0cb4d3832a1747c0 Mon Sep 17 00:00:00 2001 From: AkshayCHD Date: Sat, 17 Mar 2018 22:29:24 +0530 Subject: [PATCH] feat: sort events alphabetically (#781) --- .../module/event/list/EventListFragment.java | 37 +++++++++++++++++-- .../module/event/list/EventsListAdapter.java | 6 ++- app/src/main/res/menu/menu_events.xml | 15 ++++++-- app/src/main/res/values/strings.xml | 3 ++ 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventListFragment.java b/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventListFragment.java index c165ab2d5..193b8c06d 100644 --- a/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventListFragment.java +++ b/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventListFragment.java @@ -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; @@ -51,6 +54,10 @@ public class EventListFragment extends BaseFragment implements @Inject Lazy presenterProvider; + private static final int SORTBYDATE = 0; + private static final int SORTBYNAME = 1; + + private FragmentEventListBinding binding; private RecyclerView recyclerView; private SwipeRefreshLayout refreshLayout; @@ -84,6 +91,8 @@ public static EventListFragment newInstance() { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = getActivity(); + setHasOptionsMenu(true); + } @Override @@ -91,6 +100,28 @@ 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 @@ -106,7 +137,7 @@ public int getLoaderId() { @Override public void onStart() { super.onStart(); - setupRecyclerView(); + setupRecyclerView(SORTBYNAME); setupRefreshListener(); getPresenter().attach(this); binding.setEvents(getPresenter().getEvents()); @@ -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)); diff --git a/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventsListAdapter.java b/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventsListAdapter.java index 00e659a09..4ab33ab26 100644 --- a/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventsListAdapter.java +++ b/app/src/main/java/org/fossasia/openevent/app/module/event/list/EventsListAdapter.java @@ -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 @@ -21,7 +22,10 @@ class EventsListAdapter extends RecyclerView.Adapter events; private final IBus bus; - EventsListAdapter(List events, IBus bus) { + EventsListAdapter(List 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; } diff --git a/app/src/main/res/menu/menu_events.xml b/app/src/main/res/menu/menu_events.xml index 7f2881129..b6fb60c5b 100644 --- a/app/src/main/res/menu/menu_events.xml +++ b/app/src/main/res/menu/menu_events.xml @@ -2,8 +2,15 @@ - + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 22eea5dc5..49093aa62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -157,6 +157,9 @@ Speakers Sponsors Sessions + Sort By + Name + Date Sessions Submitted Sessions Accepted Sessions Confirmed