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 21, 2018
1 parent 3d1321b commit a62943a
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 7 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,9 @@ 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,6 +90,7 @@ public static EventListFragment newInstance() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
setHasOptionsMenu(true);
}

@Override
Expand All @@ -93,6 +100,37 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
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:
sortEvents(SORTBYNAME);
return true;
case R.id.sortByEventDate:
sortEvents(SORTBYDATE);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

private void sortEvents(int sortBy) {
if (sortBy == SORTBYNAME) {
getPresenter().sortByName(SORTBYNAME);
} else {
getPresenter().sortByName(SORTBYDATE);
}
eventListAdapter.setSortByName(sortBy == SORTBYNAME);
binding.setVariable(BR.events, getPresenter().getEvents());
binding.executePendingBindings();
eventListAdapter.notifyDataSetChanged();
}

@Override
public Lazy<IEventsPresenter> getPresenterProvider() {
return presenterProvider;
Expand Down Expand Up @@ -153,7 +191,6 @@ public void onChanged() {
}
};
}

eventListAdapter.registerAdapterDataObserver(adapterDataObserver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import org.fossasia.openevent.app.databinding.HeaderLayoutBinding;

import java.util.List;
import java.util.Locale;

class EventsListAdapter extends RecyclerView.Adapter<EventsListAdapter.EventRecyclerViewHolder>
implements StickyRecyclerHeadersAdapter<HeaderViewHolder> {

private final List<Event> events;
private final IBus bus;

private boolean sortByName;

EventsListAdapter(List<Event> events, IBus bus) {
this.events = events;
this.bus = bus;
Expand All @@ -42,7 +45,11 @@ public void onBindViewHolder(final EventRecyclerViewHolder holder, int position)

@Override
public long getHeaderId(int position) {
return events.get(position).getHeaderId();
if (sortByName) {
return events.get(position).getName().substring(0, 1).toUpperCase(Locale.getDefault()).hashCode();
} else {
return events.get(position).getHeaderId();
}
}

@Override
Expand All @@ -53,14 +60,22 @@ public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {

@Override
public void onBindHeaderViewHolder(HeaderViewHolder headerViewHolder, int i) {
headerViewHolder.bindHeader(events.get(i).getHeader());
if (sortByName) {
headerViewHolder.bindHeader(events.get(i).getName().substring(0, 1).toUpperCase(Locale.getDefault()));
} else {
headerViewHolder.bindHeader(events.get(i).getHeader());
}
}

@Override
public int getItemCount() {
return events.size();
}

public void setSortByName(boolean sortBy) {
sortByName = sortBy;
}

//view holder class
class EventRecyclerViewHolder extends RecyclerView.ViewHolder {
private final EventLayoutBinding binding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import org.fossasia.openevent.app.common.app.rx.Logger;
import org.fossasia.openevent.app.common.data.models.Event;
import org.fossasia.openevent.app.common.data.repository.contract.IEventRepository;
import org.fossasia.openevent.app.common.utils.core.service.DateService;
import org.fossasia.openevent.app.module.event.list.contract.IEventsPresenter;
import org.fossasia.openevent.app.module.event.list.contract.IEventsView;

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

import javax.inject.Inject;
Expand All @@ -26,6 +28,8 @@ public class EventsPresenter extends BasePresenter<IEventsView> implements IEven

private final IEventRepository eventsDataRepository;

private static final int SORTBYNAME = 1;

@Inject
public EventsPresenter(IEventRepository eventsDataRepository) {
this.eventsDataRepository = eventsDataRepository;
Expand All @@ -41,6 +45,15 @@ public List<Event> getEvents() {
return events;
}

@Override
public void sortByName(int sortBy) {
if (sortBy == SORTBYNAME)
Collections.sort(events, (e1, e2) -> e1.getName().compareToIgnoreCase(e2.getName()));
else {
Collections.sort(events, (e1, e2) -> DateService.compareEventDates(e1, e2));
}
}

@Override
public void loadUserEvents(boolean forceReload) {
if (getView() == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface IEventsPresenter extends IPresenter<IEventsView> {

List<Event> getEvents();

void sortByName(int sortBy);

void loadUserEvents(boolean forceReload);

}
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_by"
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_by">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 a62943a

Please sign in to comment.