Commit 5f7f2d35 authored by Robert Hitt's avatar Robert Hitt
Browse files

Refactored MainPresenter and FacilityPresenter

- they should work with facilities that are open 24/7
parent 188fb42d
Pipeline #749 passed with stages
in 4 minutes and 46 seconds
......@@ -2,6 +2,7 @@ package srct.whatsopen.presenters;
import android.content.SharedPreferences;
import android.graphics.Path;
import android.preference.PreferenceManager;
import java.text.ParseException;
......@@ -66,7 +67,11 @@ public class FacilityPresenter {
String durationMessage;
if(facility.isOpen()) {
String closingTime = openTimesList.get(currentDay).getEndTime();
if(facilityDoesNotClose(openTimesList.first())) {
return "This facility is always open";
}
String closingTime = getCurrentEndTime(openTimesList, currentDay);
closingTime = parseTo12HourTime(closingTime);
durationMessage = "Closes at " + closingTime;
......@@ -74,14 +79,14 @@ public class FacilityPresenter {
}
// Check if the facility opens later today
if(currentDay < openTimesList.size()) {
if(openTimesContains(openTimesList, currentDay)) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
try {
Date currentTime = now.getTime();
Date startTime = sdf.parse(openTimesList.get(currentDay).getStartTime());
Date currentTime = sdf.parse(sdf.format(now.getTime()));
Date startTime = sdf.parse(getCurrentStartTime(openTimesList, currentDay));
if(currentTime.compareTo(startTime) < 0) {
String openingTime = openTimesList.get(currentDay).getStartTime();
String openingTime = getCurrentStartTime(openTimesList, currentDay);
openingTime = parseTo12HourTime(openingTime);
return "Opens today at " + openingTime;
......@@ -97,7 +102,7 @@ public class FacilityPresenter {
int nextDay = findNextDay(openTimesList, currentDay);
String nextDayStr = parseIntToDay(nextDay);
String openingTime = openTimesList.get(nextDay).getStartTime();
String openingTime = getCurrentStartTime(openTimesList, nextDay);
openingTime = parseTo12HourTime(openingTime);
durationMessage = "Opens next on " + nextDayStr + " at " + openingTime;
......@@ -105,6 +110,14 @@ public class FacilityPresenter {
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();
......@@ -118,6 +131,32 @@ public class FacilityPresenter {
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;
}
private boolean facilityDoesNotClose(OpenTimes openTimes) {
return (openTimes.getStartDay() == 0 && openTimes.getEndDay() == 6 &&
openTimes.getStartTime().equals("00:00:00") &&
openTimes.getEndTime().equals("23:59:59"));
}
// Parses 24 hour formatted time String to 12 hour formatted time String
private String parseTo12HourTime(String time) {
try {
......@@ -159,6 +198,9 @@ public class FacilityPresenter {
if(openTimesList.size() == 0)
return "No schedule available";
if(facilityDoesNotClose(openTimesList.first()))
return "This facility is always open";
StringBuilder scheduleString = new StringBuilder();
boolean first = true;
for(OpenTimes o : openTimesList) {
......
......@@ -103,7 +103,7 @@ public class MainPresenter {
OpenTimes currentOpenTimes = null;
for(OpenTimes o : openTimesList) {
if(o.getStartDay() == currentDay || o.getEndDay() == currentDay)
if(o.getStartDay() <= currentDay && o.getEndDay() >= currentDay)
currentOpenTimes = o;
}
......
package srct.whatsopen;
import android.content.Context;
import android.graphics.Path;
import android.support.v7.app.AppCompatActivity;
import org.junit.Before;
......@@ -57,7 +58,7 @@ public class FacilityPresenterUnitTest {
now.set(2017, 0, 11, 10, 0); // Wednesday, 1/11/2017, 10:00:00
String statusDuration = mPresenter.getStatusDuration(mFacility, now);
assertEquals("Opens on Monday at 11:00 AM", statusDuration);
assertEquals("Opens next on Monday at 11:00 AM", statusDuration);
}
@Test
......@@ -123,4 +124,63 @@ public class FacilityPresenterUnitTest {
assertEquals("<b>Monday</b>: 11:00 AM - 6:00 PM<br/>" +
"<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("This facility is always open", statusDuration);
}
@Test
public void testFacilityScheduleNeverCloses() {
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"));
String schedule = mPresenter.getSchedule(mFacility, now);
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<>();
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"));
String schedule = mPresenter.getSchedule(mFacility, now);
assertEquals("<b>Saturday</b>: 8:00 AM - 9:00 AM<br/>" +
"<b>Sunday</b>: 8:00 AM - 9:00 AM", schedule);
}
}
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