Commit 1552de91 authored by Robert Hitt's avatar Robert Hitt
Browse files

Facilities are now sorted by open status

- The favorite buttons are also a bit bigger
parent 177252f3
......@@ -18,8 +18,17 @@ public class Facility extends RealmObject {
@SerializedName("main_schedule")
private MainSchedule mMainSchedule;
private boolean isOpen;
private boolean isFavorited;
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean open) {
isOpen = open;
}
public String getName() {
return mName;
}
......
......@@ -59,10 +59,7 @@ public class FacilityListAdapter extends
public void onBindViewHolder(ViewHolder holder, int position) {
Facility facility = getData().get(position);
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
boolean isOpen = getOpenStatus(openTimesList);
if(isOpen) {
if(facility.isOpen()) {
// set the RV cell to be highlighted
holder.itemView.setBackgroundColor(ContextCompat
.getColor(context, R.color.facilityOpen));
......@@ -83,43 +80,6 @@ public class FacilityListAdapter extends
textView.setText(facility.getName());
}
// Uses the device time to determine which facilities should be open
private boolean getOpenStatus(RealmList<OpenTimes> openTimesList) {
Calendar now = Calendar.getInstance();
// have to mess with the current day value, as Calender.DAY_OF_WEEK
// starts with Saturday as 1 and the Whats Open Api starts with Monday
// at 0, for some reason.
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
RealmResults<OpenTimes> results = openTimesList.where()
.beginGroup()
.equalTo("startDay", currentDay)
.or()
.equalTo("endDay", currentDay)
.endGroup()
.findAll();
if(results.size() == 0)
return false;
OpenTimes result = results.first();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date startTime = sdf.parse(result.getStartTime());
Date endTime = sdf.parse(result.getEndTime());
// have to parse it from date to string to date. how fun
Date currentTime = sdf.parse(sdf.format(now.getTime()));
if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0)
return true;
else
return false;
} catch (ParseException pe) {
return false;
}
}
// Set up for the Recycler View cells
public class ViewHolder extends RecyclerView.ViewHolder {
......@@ -133,6 +93,11 @@ public class FacilityListAdapter extends
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) {
......
......@@ -9,17 +9,25 @@ import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
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;
import srct.whatsopen.R;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.service.WhatsOpenClient;
import srct.whatsopen.service.WhatsOpenService;
import srct.whatsopen.model.Facility;
......@@ -56,7 +64,7 @@ public class MainActivity extends AppCompatActivity {
private void setUpRecyclerView() {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(new FacilityListAdapter(this,
mRealm.where(Facility.class).findAllAsync()));
mRealm.where(Facility.class).findAllSortedAsync("isOpen", Sort.DESCENDING)));
// Speeds things up for static lists
mRecyclerView.setHasFixedSize(true);
......@@ -79,6 +87,7 @@ public class MainActivity extends AppCompatActivity {
// Query SharedReferences for each Facility's favorite status. defaults to false
for(Facility facility : facilities) {
facility.setOpen(getOpenStatus(facility));
facility.setFavorited(pref.getBoolean(facility.getName(), false));
}
......@@ -93,5 +102,41 @@ public class MainActivity extends AppCompatActivity {
}
});
}
// Uses the device time to determine which facilities should be open
private boolean getOpenStatus(Facility facility) {
Calendar now = Calendar.getInstance();
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
// have to mess with the current day value, as Calender.DAY_OF_WEEK
// starts with Saturday as 1 and the Whats Open Api starts with Monday
// at 0, for some reason.
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
OpenTimes currentOpenTimes = null;
for(OpenTimes o : openTimesList) {
if(o.getStartDay() == currentDay || o.getEndDay() == currentDay)
currentOpenTimes = o;
}
if(currentOpenTimes == null)
return false;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date startTime = sdf.parse(currentOpenTimes.getStartTime());
Date endTime = sdf.parse(currentOpenTimes.getEndTime());
// have to parse it from date to string to date. how fun
Date currentTime = sdf.parse(sdf.format(now.getTime()));
if(currentTime.compareTo(startTime) > 0 && currentTime.compareTo(endTime) < 0)
return true;
else
return false;
} catch (ParseException pe) {
return false;
}
}
}
......@@ -8,19 +8,29 @@
android:background="@color/facilityClosed"
android:elevation="2dp">
<TextView android:id="@+id/facility_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/facilityText"
android:paddingLeft="10dp"
android:layout_weight="1"/>
<ImageButton android:id="@+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/favorite_button_off_24dp"
android:background="#00ffffff"
android:scaleType="fitCenter"
android:elevation="2dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
<RelativeLayout android:id="@+id/text_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView android:id="@+id/facility_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/facilityText"
android:paddingLeft="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton android:id="@+id/favorite_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/favorite_button_off_24dp"
android:background="#00ffffff"
android:elevation="2dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
</RelativeLayout>
</LinearLayout>
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