Commit 1b9b464b authored by Robert Hitt's avatar Robert Hitt
Browse files

Merge branch 'tabs' into 'master'

Tabs

See merge request !1
parents 1552de91 6d27cb5c
......@@ -26,6 +26,7 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'io.realm:android-adapters:1.4.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
......
package srct.whatsopen.ui;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.astuetz.PagerSlidingTabStrip;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
......@@ -20,8 +20,6 @@ import butterknife.ButterKnife;
import io.realm.Realm;
import io.realm.RealmList;
import io.realm.RealmResults;
import io.realm.Sort;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -31,6 +29,7 @@ import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.model.Facility;
import srct.whatsopen.ui.adapters.FacilityListFragmentPagerAdapter;
public class MainActivity extends AppCompatActivity {
......@@ -49,9 +48,16 @@ public class MainActivity extends AppCompatActivity {
WhatsOpenService service = WhatsOpenClient.getInstance();
callWhatsOpenAPI(service);
// Set up view
mRecyclerView = ButterKnife.findById(this, R.id.rvFacilities);
setUpRecyclerView();
// Get the ViewPager and set its PagerAdapter
ViewPager viewPager = ButterKnife.findById(this, R.id.view_pager);
viewPager.setAdapter(new FacilityListFragmentPagerAdapter(getSupportFragmentManager()));
// Now give the TabStrip the ViewPager
PagerSlidingTabStrip tabStrip = ButterKnife.findById(this, R.id.tabs);
tabStrip.setTabPaddingLeftRight(0);
tabStrip.setViewPager(viewPager);
viewPager.setCurrentItem(1);
}
@Override
......@@ -60,19 +66,18 @@ public class MainActivity extends AppCompatActivity {
mRealm.close();
}
// Handles set up for the Recycler View
private void setUpRecyclerView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new FacilityListAdapter(this,
mRealm.where(Facility.class).findAllSortedAsync("isOpen", Sort.DESCENDING)));
// Speeds things up for static lists
mRecyclerView.setHasFixedSize(true);
// does not work currently
/*
private void setDefaultTab(ViewPager viewPager) {
RealmResults<Facility> results = mRealm.where(Facility.class).equalTo("isFavorited", true)
.findAllAsync();
// Adds dividers between items
Drawable dividerDrawable = ContextCompat.getDrawable(this, R.drawable.divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(dividerDrawable));
if(results.size() == 0)
viewPager.setCurrentItem(1);
else
viewPager.setCurrentItem(0);
}
*/
// Gets a Call from the given Retrofit service, then asynchronously executes it
// On success, copies the resulting facility list to the Realm DB
......
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
* Also contains a bit of logic for figuring out What's Open
*/
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.adapters;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import srct.whatsopen.ui.fragments.FacilityListFragment;
public class FacilityListFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[] {"Favorites", "All", "Open", "Closed"};
public FacilityListFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return FacilityListFragment.newInstance(tabTitles[position]);
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
package srct.whatsopen.ui.fragments;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.ButterKnife;
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 {
public static final String ARG_MODE = "ARG_MODE";
private String mMode;
private Realm mRealm;
private RecyclerView mRecyclerView;
public static FacilityListFragment newInstance(String mode) {
Bundle args = new Bundle();
args.putString(ARG_MODE, mode);
FacilityListFragment fragment = new FacilityListFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMode = getArguments().getString(ARG_MODE);
mRealm = Realm.getDefaultInstance();
}
@Override
public void onDestroy() {
super.onDestroy();
mRealm.close();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_facility, container, false);
mRecyclerView = ButterKnife.findById(view, R.id.rvFacilities);
setUpRecyclerView(view);
return view;
}
// Handles set up for the Recycler View
private void setUpRecyclerView(View view) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
switch(mMode) {
case "All":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Favorites":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.equalTo("isFavorited", true)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Open":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.equalTo("isOpen", true)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
case "Closed":
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.equalTo("isOpen", false)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
break;
default:
mRecyclerView.setAdapter(new FacilityListAdapter(view.getContext(),
mRealm.where(Facility.class)
.findAllSortedAsync("isOpen", Sort.DESCENDING)));
}
// Speeds things up for static lists
mRecyclerView.setHasFixedSize(true);
// Adds dividers between items
Drawable dividerDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.divider);
mRecyclerView.addItemDecoration(new DividerItemDecoration(dividerDrawable));
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_selected="true"
android:drawable="@color/tabButtonPressed" />
<item android:state_enabled="true"
android:state_focused="true"
android:drawable="@color/tabButtonPressed" />
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="@color/tabButtonPressed" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context="srct.whatsopen.ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvFacilities"
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
app:pstsShouldExpand="true"
app:pstsIndicatorColor="@color/colorPrimary"
app:pstsIndicatorHeight="6dp"
app:pstsTabBackground="@drawable/tab_button_bg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="48dp"/>
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
tools:context="srct.whatsopen.ui.fragments.FacilityListFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvFacilities"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#006633</color>
<color name="colorPrimaryLight">#008542</color>
<color name="colorPrimaryDark">#015429</color>
<color name="colorAccent">#FF4081</color>
<color name="facilityText">#757575</color>
......@@ -8,4 +9,5 @@
<color name="favoriteButtonOn">#E8B827</color>
<color name="facilityClosed">#E0E0E0</color>
<color name="facilityOpen">#FAFAFA</color>
<color name="tabButtonPressed">#4b008542</color>
</resources>