Add will be closed at

This commit is contained in:
PavelRatushny 2017-12-07 18:00:54 +02:00
parent 587aaff2c6
commit 246877cf10
6 changed files with 135 additions and 10 deletions

View file

@ -222,6 +222,34 @@ public class OpeningHoursParser {
return openFrom; return openFrom;
} }
public String getClosedAtStr(Calendar cal) {
String closedAt = getClosedAtDayStr(cal);
if (Algorithms.isEmpty(closedAt)) {
closedAt = getClosedAtNextDayStr(cal);
}
return closedAt;
}
private String getClosedAtDayStr(Calendar cal) {
String closedAt = "";
for (OpeningHoursRule r : rules) {
if (r.containsDay(cal) && r.containsMonth(cal)) {
closedAt = r.getClosedAtStr(cal, false);
}
}
return closedAt;
}
private String getClosedAtNextDayStr(Calendar cal) {
String closedAt = "";
for (OpeningHoursRule r : rules) {
if (r.containsNextDay(cal) && r.containsMonth(cal)) {
closedAt = r.getClosedAtStr(cal, true);
}
}
return closedAt;
}
public String getCurrentRuleTime(Calendar cal) { public String getCurrentRuleTime(Calendar cal) {
// make exception for overlapping times i.e. // make exception for overlapping times i.e.
// (1) Mo 14:00-16:00; Tu off // (1) Mo 14:00-16:00; Tu off
@ -354,6 +382,14 @@ public class OpeningHoursParser {
*/ */
public boolean containsDay(Calendar cal); public boolean containsDay(Calendar cal);
/**
* Check if the next day after "cal" is part of this rule
*
* @param cal the time to check
* @return true if the next day is part of the rule
*/
boolean containsNextDay(Calendar cal);
/** /**
* Check if the month of "cal" is part of this rule * Check if the month of "cal" is part of this rule
* *
@ -381,6 +417,8 @@ public class OpeningHoursParser {
boolean isOpen24_7(); boolean isOpen24_7();
String getOpenedFromStr(Calendar cal, boolean checkPrevious); String getOpenedFromStr(Calendar cal, boolean checkPrevious);
String getClosedAtStr(Calendar cal, boolean checkNext);
} }
/** /**
@ -598,6 +636,16 @@ public class OpeningHoursParser {
return false; return false;
} }
@Override
public boolean containsNextDay(Calendar cal) {
int i = cal.get(Calendar.DAY_OF_WEEK);
int p = (i + 6) % 7;
if (days[p]) {
return true;
}
return false;
}
/** /**
* Check if the previous weekday of time "cal" is part of this rule * Check if the previous weekday of time "cal" is part of this rule
* *
@ -677,6 +725,14 @@ public class OpeningHoursParser {
return p; return p;
} }
private int getNextDay(int currentDay) {
int n = currentDay + 1;
if (n > 6) {
n -= 7;
}
return n;
}
private int getCurrentTimeInMinutes(Calendar cal) { private int getCurrentTimeInMinutes(Calendar cal) {
return cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE); return cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE);
} }
@ -804,23 +860,49 @@ public class OpeningHoursParser {
if (startTime < endTime || endTime == -1) { if (startTime < endTime || endTime == -1) {
if (days[d] && !checkPrevious) { if (days[d] && !checkPrevious) {
if (time - startTime <= 600 && (endTime == -1 || time <= endTime)) { if (time - startTime <= 600 && (endTime == -1 || time <= endTime)) {
int stHour = startTime / 60; formatTime(startTime, sb);
int stTime = startTime - stHour * 60;
formatTime(stHour, stTime, sb);
break; break;
} }
} }
} else { } else {
if (time >= startTime && days[d] && !checkPrevious) { if (time >= startTime && days[d] && !checkPrevious) {
int stHour = startTime / 60; formatTime(startTime, sb);
int stTime = startTime - stHour * 60;
formatTime(stHour, stTime, sb);
break; break;
} else if (time < endTime && days[p] && checkPrevious) { } else if (time < endTime && days[p] && checkPrevious) {
if (24 * 60 - endTime + time <= 600) { if (24 * 60 - endTime + time <= 600) {
int stHour = startTime / 60; formatTime(startTime, sb);
int stTime = startTime - stHour * 60; break;
formatTime(stHour, stTime, sb); }
}
}
}
return sb.toString();
}
@Override
public String getClosedAtStr(Calendar cal, boolean checkNext) {
int limit = 300;
StringBuilder sb = new StringBuilder();
int d = getCurrentDay(cal);
int n = getNextDay(d);
int time = getCurrentTimeInMinutes(cal);
for (int i = 0; i < startTimes.size(); i++) {
int startTime = startTimes.get(i);
int endTime = endTimes.get(i);
if (startTime < endTime && endTime != -1) {
if (days[d] && !checkNext) {
if (time <= endTime && endTime - time <= limit ) {
formatTime(endTime, sb);
break;
}
}
} else {
if (time <= endTime && days[d] && !checkNext) {
formatTime(endTime, sb);
break;
} else if (time < endTime && days[n] && checkNext) {
if (endTime - time <= limit) {
formatTime(endTime, sb);
break; break;
} }
} }
@ -1004,6 +1086,11 @@ public class OpeningHoursParser {
return false; return false;
} }
@Override
public boolean containsNextDay(Calendar cal) {
return false;
}
@Override @Override
public boolean containsMonth(Calendar cal) { public boolean containsMonth(Calendar cal) {
return false; return false;
@ -1029,6 +1116,11 @@ public class OpeningHoursParser {
return ""; return "";
} }
@Override
public String getClosedAtStr(Calendar cal, boolean checkNext) {
return "";
}
@Override @Override
public String toString() { public String toString() {
return toRuleString(); return toRuleString();
@ -1374,6 +1466,11 @@ public class OpeningHoursParser {
b.append(t); b.append(t);
} }
private static void formatTime(int minutes, StringBuilder sb) {
int hour = minutes / 60;
int time = minutes - hour * 60;
formatTime(hour, time, sb);
}
/** /**
* test if the calculated opening hours are what you expect * test if the calculated opening hours are what you expect

View file

@ -11,6 +11,7 @@
--> -->
<string name="shared_string_opened">Opened</string> <string name="shared_string_opened">Opened</string>
<string name="opened_from">Opened from</string> <string name="opened_from">Opened from</string>
<string name="will_be_closed_at">Will be closed at</string>
<string name="additional_actions">Additional actions</string> <string name="additional_actions">Additional actions</string>
<string name="release_3_0"> <string name="release_3_0">
\u2022 Detection of stop signs now considers driving direction\n\n \u2022 Detection of stop signs now considers driving direction\n\n

View file

@ -1112,6 +1112,8 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
openingHoursStr = getString(R.string.shared_string_is_open_24_7); openingHoursStr = getString(R.string.shared_string_is_open_24_7);
} else if (!Algorithms.isEmpty(menu.getOpenFromStr())) { } else if (!Algorithms.isEmpty(menu.getOpenFromStr())) {
openingHoursStr = getString(R.string.opened_from) + " " + menu.getOpenFromStr(); openingHoursStr = getString(R.string.opened_from) + " " + menu.getOpenFromStr();
} else if (!Algorithms.isEmpty(menu.getClosedAtStr())) {
openingHoursStr = getString(R.string.will_be_closed_at) + " " + menu.getClosedAtStr();
} }
openingHoursTextView.setText(openingHoursStr); openingHoursTextView.setText(openingHoursStr);
openingHoursView.setVisibility(View.VISIBLE); openingHoursView.setVisibility(View.VISIBLE);

View file

@ -432,6 +432,10 @@ public abstract class MenuController extends BaseMenuController {
return ""; return "";
} }
public String getClosedAtStr() {
return "";
}
public String getCommonTypeStr() { public String getCommonTypeStr() {
return ""; return "";
} }

View file

@ -20,6 +20,7 @@ public abstract class MenuTitleController {
protected String streetStr = ""; protected String streetStr = "";
protected boolean open24_7; protected boolean open24_7;
protected String openFromStr = ""; protected String openFromStr = "";
protected String closedAtStr = "";
private AddressLookupRequest addressLookupRequest; private AddressLookupRequest addressLookupRequest;
@ -108,8 +109,12 @@ public abstract class MenuTitleController {
return openFromStr; return openFromStr;
} }
public String getClosedAtStr() {
return closedAtStr;
}
public boolean isOpened() { public boolean isOpened() {
if (isOpen24_7() || !Algorithms.isEmpty(getOpenFromStr())) { if (isOpen24_7() || !Algorithms.isEmpty(getOpenFromStr()) || !Algorithms.isEmpty(getClosedAtStr())) {
return true; return true;
} else { } else {
return false; return false;
@ -211,6 +216,7 @@ public abstract class MenuTitleController {
if (menuController != null) { if (menuController != null) {
open24_7 = menuController.isOpen24_7(); open24_7 = menuController.isOpen24_7();
openFromStr = menuController.getOpenFromStr(); openFromStr = menuController.getOpenFromStr();
closedAtStr = menuController.getClosedAtStr();
} }
} }

View file

@ -128,6 +128,11 @@ public class AmenityMenuController extends MenuController {
return getOpenFromStr(amenity); return getOpenFromStr(amenity);
} }
@Override
public String getClosedAtStr() {
return getClosedAtStr(amenity);
}
public static String getTypeStr(Amenity amenity) { public static String getTypeStr(Amenity amenity) {
PoiCategory pc = amenity.getType(); PoiCategory pc = amenity.getType();
PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType()); PoiType pt = pc.getPoiTypeByKeyName(amenity.getSubType());
@ -166,6 +171,16 @@ public class AmenityMenuController extends MenuController {
} }
} }
public static String getClosedAtStr(Amenity amenity) {
OpeningHoursParser.OpeningHours openingHours = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (openingHours == null) {
return "";
} else {
Calendar cal = Calendar.getInstance();
return openingHours.getClosedAtStr(cal);
}
}
@Override @Override
public String getCommonTypeStr() { public String getCommonTypeStr() {
PoiCategory pc = amenity.getType(); PoiCategory pc = amenity.getType();