Commit 9ce6e25d authored by Robert Hitt's avatar Robert Hitt
Browse files

Tabs are now green when pressed

- Also, the title of the Favorites tab is no longer cut off
- Changed with the layout of the tabs slightly as well
parent 913479ca
package srct.whatsopen.ui;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.OrderedRealmCollection;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmRecyclerViewAdapter;
import io.realm.RealmResults;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.OpenTimes;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
*/
public class FacilityListAdapter extends
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> {
public FacilityListAdapter(Context context,
OrderedRealmCollection<Facility> data) {
super(context, data, true);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View facilityView = inflater.inflate(R.layout.item_facility, parent, false);
ViewHolder viewHolder = new ViewHolder(facilityView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Facility facility = getData().get(position);
if(facility.isOpen()) {
// set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityOpen));
} else {
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityClosed));
}
if(facility.isFavorited()) {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
}
else {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
}
holder.data = facility;
TextView textView = holder.nameTextView;
textView.setText(facility.getName());
}
// Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.facility_name) TextView nameTextView;
@BindView(R.id.favorite_button) ImageButton favoriteButton;
public Facility data;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
// should expand to the facility's detail view
@OnClick(R.id.text_layout)
public void expandFacilityView() {
}
// toggles favorite status
@OnClick(R.id.favorite_button)
public void setFavorite(ImageButton favoriteButton) {
if(data.isFavorited()) {
favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
toggleFavoriteAsync(false);
}
else {
favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
toggleFavoriteAsync(true);
}
}
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
void toggleFavoriteAsync(final boolean status) {
Realm realm = Realm.getDefaultInstance();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = data.getName();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
// have to requery for the object as it was created on a separate thread
Facility facility = bgRealm.where(Facility.class)
.equalTo("mName", facilityName).findFirst();
facility.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
}
}, null, null);
realm.close();
}
}
}
package srct.whatsopen.ui.adapters;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.realm.OrderedRealmCollection;
import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
/**
* Basic RecyclerView boilerplate, with some added Realm stuff
*/
public class FacilityListAdapter extends
RealmRecyclerViewAdapter<Facility, FacilityListAdapter.ViewHolder> {
public FacilityListAdapter(Context context,
OrderedRealmCollection<Facility> data) {
super(context, data, true);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View facilityView = inflater.inflate(R.layout.item_facility, parent, false);
ViewHolder viewHolder = new ViewHolder(facilityView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Facility facility = getData().get(position);
if(facility.isOpen()) {
// set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityOpen));
} else {
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityClosed));
}
if(facility.isFavorited()) {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
}
else {
holder.favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
}
holder.data = facility;
TextView textView = holder.nameTextView;
textView.setText(facility.getName());
}
// Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.facility_name) TextView nameTextView;
@BindView(R.id.favorite_button) ImageButton favoriteButton;
public Facility data;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
// should expand to the facility's detail view
@OnClick(R.id.text_layout)
public void expandFacilityView() {
}
// toggles favorite status
@OnClick(R.id.favorite_button)
public void setFavorite(ImageButton favoriteButton) {
if(data.isFavorited()) {
favoriteButton.setImageResource(R.drawable.favorite_button_off_24dp);
toggleFavoriteAsync(false);
}
else {
favoriteButton.setImageResource(R.drawable.favorite_button_on_24dp);
toggleFavoriteAsync(true);
}
}
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
// Would block the favorite button redrawing if done on the UI thread
void toggleFavoriteAsync(final boolean status) {
Realm realm = Realm.getDefaultInstance();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = data.getName();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
// have to requery for the object as it was created on a separate thread
Facility facility = bgRealm.where(Facility.class)
.equalTo("mName", facilityName).findFirst();
facility.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
}
}, null, null);
realm.close();
}
}
}
package srct.whatsopen.ui;
package srct.whatsopen.ui.adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import srct.whatsopen.ui.FacilityListFragment;
public class FacilityListFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[] {"All", "Favorites", "Open", "Closed"};
private String tabTitles[] = new String[] {"Fav", "All", "Open", "Closed"};
public FacilityListFragmentPagerAdapter(FragmentManager fm) {
super(fm);
......
package srct.whatsopen.ui;
package srct.whatsopen.ui.fragments;
import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
......@@ -16,6 +14,8 @@ import io.realm.Realm;
import io.realm.Sort;
import srct.whatsopen.R;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.DividerItemDecoration;
import srct.whatsopen.ui.adapters.FacilityListAdapter;
public class FacilityListFragment extends android.support.v4.app.Fragment {
......@@ -67,7 +67,7 @@ public class FacilityListFragment extends android.support.v4.app.Fragment {
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Favorites":
case "Fav":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.equalTo("isFavorited", true)
......
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment