Commit 3482956e authored by Robert Hitt's avatar Robert Hitt

Status duration is now determined on start-up

-This should make it so that if all the facilities in the list view show
status duration, it won't make scrolling sluggish (as it takes
a fair amount of computation to determine status duration)
parent 397edf2f
Pipeline #824 passed with stages
in 4 minutes and 51 seconds
......@@ -10,13 +10,14 @@ public class Facility extends RealmObject {
public Facility(String name, String location, MainSchedule mainSchedule,
RealmList<SpecialSchedule> specialSchedules, boolean isOpen,
boolean isFavorited) {
boolean isFavorited, String statusDuration) {
mName = name;
mLocation = location;
mMainSchedule = mainSchedule;
mSpecialSchedules = specialSchedules;
this.isOpen = isOpen;
this.isFavorited = isFavorited;
this.statusDuration = statusDuration;
}
public Facility() {
......@@ -37,6 +38,15 @@ public class Facility extends RealmObject {
private boolean isOpen;
private boolean isFavorited;
private String statusDuration;
public String getStatusDuration() {
return statusDuration;
}
public void setStatusDuration(String statusDuration) {
this.statusDuration = statusDuration;
}
public RealmList<SpecialSchedule> getSpecialSchedules() {
return mSpecialSchedules;
......
......@@ -68,102 +68,45 @@ public class FacilityPresenter {
realm.close();
}
// Finds the next time the facility closes or opens and returns it
public String getStatusDuration(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now);
if(openTimesList.size() == 0)
return "No open time on schedule";
// Parses the schedule into an HTML string
// Kind of a hacky approach
public String getSchedule(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now);
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
String durationMessage;
if(facility.isOpen()) {
if(facilityDoesNotClose(openTimesList)) {
return "Open 24/7";
}
String closingTime = getCurrentEndTime(openTimesList, currentDay);
closingTime = parseTo12HourTime(closingTime);
durationMessage = "Closes at " + closingTime;
return durationMessage;
}
// Check if the facility opens later today
if(openTimesContains(openTimesList, currentDay)) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date currentTime = sdf.parse(sdf.format(now.getTime()));
Date startTime = sdf.parse(getCurrentStartTime(openTimesList, currentDay));
if(currentTime.compareTo(startTime) < 0) {
String openingTime = getCurrentStartTime(openTimesList, currentDay);
openingTime = parseTo12HourTime(openingTime);
return "Opens today at " + openingTime;
}
} catch (ParseException pe) {
pe.printStackTrace();
return "";
}
}
// Else return the opening time of the next day
int nextDay = findNextDay(openTimesList, currentDay);
String nextDayStr = parseIntToDay(nextDay);
String openingTime = getCurrentStartTime(openTimesList, nextDay);
openingTime = parseTo12HourTime(openingTime);
durationMessage = "Opens next on " + nextDayStr + " at " + openingTime;
if(openTimesList.size() == 0)
return "No schedule available";
return durationMessage;
}
if(facilityDoesNotClose(openTimesList))
return "This facility is always open";
private boolean openTimesContains(RealmList<OpenTimes> openTimesList, int currentDay) {
StringBuilder scheduleString = new StringBuilder();
boolean first = true;
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
return true;
}
return false;
}
if(first)
first = false;
else
scheduleString.append("<br/>");
// Returns the next open day in the list of OpenTimes
private int findNextDay(RealmList<OpenTimes> openTimesList, int current) {
int nextDay = openTimesList.first().getStartDay();
for(OpenTimes o : openTimesList) {
if(o.getStartDay() > current) {
nextDay = o.getStartDay();
break;
}
}
// the current day's schedule should be highlighted
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
scheduleString.append("<strong>");
return nextDay;
}
scheduleString.append("<b>" + parseIntToDay(o.getStartDay()) + "</b>: ");
scheduleString.append(parseTo12HourTime(o.getStartTime()));
scheduleString.append(" - ");
scheduleString.append(parseTo12HourTime(o.getEndTime()));
// Returns the end time for the current day
private String getCurrentEndTime(RealmList<OpenTimes> openTimesList, int currentDay) {
String endTime = "";
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
endTime = o.getEndTime();
scheduleString.append("</strong>");
}
return endTime;
}
// Returns the end time for the current day
private String getCurrentStartTime(RealmList<OpenTimes> openTimesList, int currentDay) {
String startTime = "";
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
startTime = o.getStartTime();
}
return startTime;
return scheduleString.toString();
}
private boolean facilityDoesNotClose(List<OpenTimes> openTimesList) {
public static boolean facilityDoesNotClose(List<OpenTimes> openTimesList) {
boolean doesNotClose = false;
int counter = 0;
......@@ -186,7 +129,7 @@ public class FacilityPresenter {
}
// Parses 24 hour formatted time String to 12 hour formatted time String
private String parseTo12HourTime(String time) {
public static String parseTo12HourTime(String time) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
final Date date = sdf.parse(time);
......@@ -198,7 +141,7 @@ public class FacilityPresenter {
}
// Parses an integer to a String of the day of the week
private String parseIntToDay(int day) {
public static String parseIntToDay(int day) {
switch(day) {
case 0:
return "Monday";
......@@ -219,42 +162,6 @@ public class FacilityPresenter {
}
}
// Parses the schedule into an HTML string
// Kind of a hacky approach
public String getSchedule(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now);
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
if(openTimesList.size() == 0)
return "No schedule available";
if(facilityDoesNotClose(openTimesList))
return "This facility is always open";
StringBuilder scheduleString = new StringBuilder();
boolean first = true;
for(OpenTimes o : openTimesList) {
if(first)
first = false;
else
scheduleString.append("<br/>");
// the current day's schedule should be highlighted
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
scheduleString.append("<strong>");
scheduleString.append("<b>" + parseIntToDay(o.getStartDay()) + "</b>: ");
scheduleString.append(parseTo12HourTime(o.getStartTime()));
scheduleString.append(" - ");
scheduleString.append(parseTo12HourTime(o.getEndTime()));
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
scheduleString.append("</strong>");
}
return scheduleString.toString();
}
// Returns the active schedule given the current date
private RealmList<OpenTimes> getActiveSchedule(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
......
......@@ -83,6 +83,7 @@ public class MainPresenter {
// Query SharedReferences for each Facility's favorite status. defaults to false
facility.setFavorited(pref.getBoolean(facility.getName()+"FavoriteStatus", false));
facility.setOpen(getOpenStatus(facility, Calendar.getInstance()));
facility.setStatusDuration(getStatusDuration(facility, Calendar.getInstance()));
}
return facilities;
......@@ -134,13 +135,14 @@ public class MainPresenter {
realm.close();
}
// Sets the open status of each facility in the Realm instance
// Sets the open status and status duration of each facility in the Realm instance
private void updateOpenStatus() {
Realm realm = Realm.getDefaultInstance();
realm.executeTransactionAsync(bgRealm -> {
List<Facility> facilities = bgRealm.where(Facility.class).findAll();
for(Facility f : facilities) {
f.setOpen(getOpenStatus(f, Calendar.getInstance()));
f.setStatusDuration(getStatusDuration(f, Calendar.getInstance()));
}
}, null, null);
realm.close();
......@@ -188,6 +190,104 @@ public class MainPresenter {
}
}
// Finds the next time the facility closes or opens and returns it
public String getStatusDuration(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = getActiveSchedule(facility, now);
if(openTimesList == null)
return "";
if(openTimesList.size() == 0)
return "No open time on schedule";
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
String durationMessage;
if(facility.isOpen()) {
if(FacilityPresenter.facilityDoesNotClose(openTimesList)) {
return "Open 24/7";
}
String closingTime = getCurrentEndTime(openTimesList, currentDay);
closingTime = FacilityPresenter.parseTo12HourTime(closingTime);
durationMessage = "Closes at " + closingTime;
return durationMessage;
}
// Check if the facility opens later today
if(openTimesContains(openTimesList, currentDay)) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date currentTime = sdf.parse(sdf.format(now.getTime()));
Date startTime = sdf.parse(getCurrentStartTime(openTimesList, currentDay));
if(currentTime.compareTo(startTime) < 0) {
String openingTime = getCurrentStartTime(openTimesList, currentDay);
openingTime = FacilityPresenter.parseTo12HourTime(openingTime);
return "Opens today at " + openingTime;
}
} catch (ParseException pe) {
pe.printStackTrace();
return "";
}
}
// Else return the opening time of the next day
int nextDay = findNextDay(openTimesList, currentDay);
String nextDayStr = FacilityPresenter.parseIntToDay(nextDay);
String openingTime = getCurrentStartTime(openTimesList, nextDay);
openingTime = FacilityPresenter.parseTo12HourTime(openingTime);
durationMessage = "Opens next on " + nextDayStr + " at " + openingTime;
return durationMessage;
}
private boolean openTimesContains(RealmList<OpenTimes> openTimesList, int currentDay) {
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
return true;
}
return false;
}
// Returns the next open day in the list of OpenTimes
private int findNextDay(RealmList<OpenTimes> openTimesList, int current) {
int nextDay = openTimesList.first().getStartDay();
for(OpenTimes o : openTimesList) {
if(o.getStartDay() > current) {
nextDay = o.getStartDay();
break;
}
}
return nextDay;
}
// Returns the end time for the current day
private String getCurrentEndTime(RealmList<OpenTimes> openTimesList, int currentDay) {
String endTime = "";
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
endTime = o.getEndTime();
}
return endTime;
}
// Returns the end time for the current day
private String getCurrentStartTime(RealmList<OpenTimes> openTimesList, int currentDay) {
String startTime = "";
for(OpenTimes o : openTimesList) {
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
startTime = o.getStartTime();
}
return startTime;
}
// Returns the active schedule given the current date
private RealmList<OpenTimes> getActiveSchedule(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
......
......@@ -147,8 +147,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView,
String statusText = mFacility.isOpen() ? "Open" : "Closed";
openStatusTextView.setText(statusText);
openDurationTextView.setText(mPresenter.getStatusDuration(mFacility,
Calendar.getInstance()));
openDurationTextView.setText(mFacility.getStatusDuration());
locationTextView.setText(mFacility.getLocation());
......
......@@ -121,7 +121,7 @@ public class FacilityListAdapter extends
mPresenter = new FacilityPresenter();
mPresenter.attachView(this);
duration = mPresenter.getStatusDuration(data, Calendar.getInstance());
duration = data.getStatusDuration();
durationTextView.setText(duration);
}
......
......@@ -38,51 +38,12 @@ public class FacilityPresenterUnitTest {
"2017-01-09", "2017-01-15");
mFacility = new Facility("Chef's Table at Brooklyn Fare", "Whitetop Hall",
mainSchedule, new RealmList<>(), false, true);
mainSchedule, new RealmList<>(), false, true, "");
now = Calendar.getInstance();
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
}
@Test
public void testFacilityMessageOpensToday() {
// Set date
now.set(2017, 0, 9, 10, 0); // Monday, 1/9/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens today at 11:00 AM", statusDuration);
}
@Test
public void testFacilityMessageOpensNotToday() {
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens next on Monday at 11:00 AM", statusDuration);
}
@Test
public void testFacilityMessageCloses() {
// Set date
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
mFacility.setOpen(true);
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Closes at 6:00 PM", statusDuration);
}
@Test
public void testFacilityMessageNoSchedule() {
// Set date
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
mFacility.setMainSchedule(new MainSchedule(new RealmList<OpenTimes>(),
"2017-01-09", "2017-01-15"));
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("No open time on schedule", statusDuration);
}
@Test
public void testFacilitySchedule() {
......@@ -128,22 +89,6 @@ public class FacilityPresenterUnitTest {
"<b>Tuesday</b>: 1:00 PM - 6:00 PM", schedule);
}
@Test
public void testFacilityMessageNeverCloses() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
openTimesList.add(new OpenTimes(0, 6, "00:00:00", "23:59:59"));
mFacility.setOpen(true);
mFacility.setMainSchedule(new MainSchedule(openTimesList,
"2017-01-09", "2017-01-15"));
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Open 24/7", statusDuration);
}
@Test
public void testFacilityScheduleNeverCloses() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
......@@ -157,22 +102,6 @@ public class FacilityPresenterUnitTest {
assertEquals("This facility is always open", schedule);
}
@Test
public void testFacilityMessage_2() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
openTimesList.add(new OpenTimes(5, 5, "08:00:00", "09:00:00"));
openTimesList.add(new OpenTimes(6, 6, "08:00:00", "09:00:00"));
mFacility.setMainSchedule(new MainSchedule(openTimesList,
"2017-01-09", "2017-01-15"));
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens next on Saturday at 8:00 AM", statusDuration);
}
@Test
public void testFacilitySchedule_2() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
......
......@@ -42,7 +42,7 @@ public class MainPresenterUnitTest {
specialSchedules.add(s2);
mFacility = new Facility("The French Laundry", "Johnson Center",
mainSchedule, specialSchedules, false, true);
mainSchedule, specialSchedules, false, true, "");
now = Calendar.getInstance();
}
......@@ -70,4 +70,76 @@ public class MainPresenterUnitTest {
assertTrue(mPresenter.getOpenStatus(mFacility, now));
}
@Test
public void testFacilityMessageOpensToday() {
// Set date
now.set(2017, 0, 9, 10, 0); // Monday, 1/9/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens today at 11:00 AM", statusDuration);
}
@Test
public void testFacilityMessageOpensNotToday() {
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens next on Monday at 11:00 AM", statusDuration);
}
@Test
public void testFacilityMessageCloses() {
// Set date
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
mFacility.setOpen(true);
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Closes at 6:00 PM", statusDuration);
}
@Test
public void testFacilityMessageNoSchedule() {
// Set date
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
mFacility.setMainSchedule(new MainSchedule(new RealmList<OpenTimes>(),
"2017-01-09", "2017-01-15"));
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("No open time on schedule", statusDuration);
}
@Test
public void testFacilityMessageNeverCloses() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
openTimesList.add(new OpenTimes(0, 6, "00:00:00", "23:59:59"));
mFacility.setOpen(true);
mFacility.setMainSchedule(new MainSchedule(openTimesList,
"2017-01-09", "2017-01-15"));
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Open 24/7", statusDuration);
}
@Test
public void testFacilityMessage_2() {
RealmList<OpenTimes> openTimesList = new RealmList<>();
openTimesList.add(new OpenTimes(5, 5, "08:00:00", "09:00:00"));
openTimesList.add(new OpenTimes(6, 6, "08:00:00", "09:00:00"));
mFacility.setMainSchedule(new MainSchedule(openTimesList,
"2017-01-09", "2017-01-15"));
// Set date
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens next on Saturday at 8:00 AM", statusDuration);
}
}
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