Fix time and refactor

This commit is contained in:
PavelRatushny 2017-12-08 14:25:34 +02:00
parent b29871e263
commit 8e5a232d21
5 changed files with 132 additions and 120 deletions

View file

@ -186,76 +186,84 @@ public class OpeningHoursParser {
return isOpenDay || isOpenPrevious; return isOpenDay || isOpenPrevious;
} }
public boolean isOpen24_7() { public boolean isOpened24_7() {
boolean open24_7 = false; boolean opened24_7 = false;
for (OpeningHoursRule r : rules) { for (OpeningHoursRule r : rules) {
open24_7 = r.isOpened24_7(); opened24_7 = r.isOpened24_7();
} }
return open24_7; return opened24_7;
} }
public String getOpenedFromTime(Calendar cal) { public String getNearToOpeningTime(Calendar cal) {
String openFrom = getOpenFromTimeDay(cal); return getOpeningTime(cal, false);
if (Algorithms.isEmpty(openFrom)) {
openFrom = getOpenFromTimePrevious(cal);
}
return openFrom;
} }
private String getOpenFromTimeDay(Calendar cal) { public String getOpeningTime(Calendar cal) {
String openFrom = ""; return getOpeningTime(cal, true);
}
private String getOpeningTime(Calendar cal, boolean nearToOpening) {
String openingAt = getOpeningTimeDay(cal, nearToOpening);
if (Algorithms.isEmpty(openingAt)) {
openingAt = getOpeningTimePrevious(cal, nearToOpening);
}
return openingAt;
}
private String getOpeningTimeDay(Calendar cal, boolean nearToOpening) {
String openingAt = "";
for (OpeningHoursRule r : rules) { for (OpeningHoursRule r : rules) {
if (r.containsDay(cal) && r.containsMonth(cal)) { if (r.containsDay(cal) && r.containsMonth(cal)) {
openFrom = r.getOpenedFromTime(cal, false); openingAt = r.getTime(cal, false, nearToOpening, true);
} }
} }
return openFrom; return openingAt;
} }
private String getOpenFromTimePrevious(Calendar cal) { private String getOpeningTimePrevious(Calendar cal, boolean nearToOpening) {
String openFrom = ""; String openingAt = "";
for (OpeningHoursRule r : rules) { for (OpeningHoursRule r : rules) {
if (r.containsPreviousDay(cal) && r.containsMonth(cal)) { if (r.containsPreviousDay(cal) && r.containsMonth(cal)) {
openFrom = r.getOpenedFromTime(cal, true); openingAt = r.getTime(cal, true, nearToOpening, true);
} }
} }
return openFrom; return openingAt;
} }
public String getClosedAtTime(Calendar cal) { public String getNearToClosingTime(Calendar cal) {
return getClosedTime(cal, true); return getClosingTime(cal, true);
} }
public String getOpenedTillTime(Calendar cal) { public String getClosingTime(Calendar cal) {
return getClosedTime(cal, false); return getClosingTime(cal, false);
} }
private String getClosedTime(Calendar cal, boolean nearToClosing) { private String getClosingTime(Calendar cal, boolean nearToClosing) {
String closedAt = getClosedAtTimeDay(cal, nearToClosing); String closingAt = getClosingTimeDay(cal, nearToClosing);
if (Algorithms.isEmpty(closedAt)) { if (Algorithms.isEmpty(closingAt)) {
closedAt = getClosedAtTimeNextDay(cal, nearToClosing); closingAt = getClosingTimeNext(cal, nearToClosing);
} }
return closedAt; return closingAt;
} }
private String getClosedAtTimeDay(Calendar cal, boolean nearToClosing) { private String getClosingTimeDay(Calendar cal, boolean nearToClosing) {
String closedAt = ""; String closingAt = "";
for (OpeningHoursRule r : rules) { for (OpeningHoursRule r : rules) {
if (r.containsDay(cal) && r.containsMonth(cal)) { if (r.containsDay(cal) && r.containsMonth(cal)) {
closedAt = r.getClosedAtTime(cal, false, nearToClosing); closingAt = r.getTime(cal, false, nearToClosing, false);
} }
} }
return closedAt; return closingAt;
} }
private String getClosedAtTimeNextDay(Calendar cal, boolean nearToClosing) { private String getClosingTimeNext(Calendar cal, boolean nearToClosing) {
String closedAt = ""; String closingAt = "";
for (OpeningHoursRule r : rules) { for (OpeningHoursRule r : rules) {
if (r.containsNextDay(cal) && r.containsMonth(cal)) { if (r.containsNextDay(cal) && r.containsMonth(cal)) {
closedAt = r.getClosedAtTime(cal, true, nearToClosing); closingAt = r.getTime(cal, true, nearToClosing, false);
} }
} }
return closedAt; return closingAt;
} }
public String getCurrentRuleTime(Calendar cal) { public String getCurrentRuleTime(Calendar cal) {
@ -424,9 +432,7 @@ public class OpeningHoursParser {
boolean isOpened24_7(); boolean isOpened24_7();
String getOpenedFromTime(Calendar cal, boolean checkPrevious); String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening);
String getClosedAtTime(Calendar cal, boolean checkNext, boolean nearToClosing);
} }
/** /**
@ -857,62 +863,54 @@ public class OpeningHoursParser {
} }
@Override @Override
public String getOpenedFromTime(Calendar cal, boolean checkPrevious) { public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) {
int limit = 300; int nearToEventLimit = 120;
StringBuilder sb = new StringBuilder();
int d = getCurrentDay(cal);
int p = getPreviousDay(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] && !checkPrevious) {
if (time - startTime <= limit && (endTime == -1 || time <= endTime)) {
formatTime(startTime, sb);
break;
}
}
} else {
if (time >= startTime && days[d] && !checkPrevious) {
formatTime(startTime, sb);
break;
} else if (time < endTime && days[p] && checkPrevious) {
if (24 * 60 - endTime + time <= limit) {
formatTime(startTime, sb);
break;
}
}
}
}
return sb.toString();
}
@Override
public String getClosedAtTime(Calendar cal, boolean checkNext, boolean nearToClosing) {
int nearToClosingLimit = 120;
int freeLimit = 300; int freeLimit = 300;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
int d = getCurrentDay(cal); int d = getCurrentDay(cal);
int n = getNextDay(d); int ad = opening ? getNextDay(d) : getPreviousDay(d);
int time = getCurrentTimeInMinutes(cal); int time = getCurrentTimeInMinutes(cal);
for (int i = 0; i < startTimes.size(); i++) { for (int i = 0; i < startTimes.size(); i++) {
int startTime = startTimes.get(i); int startTime = startTimes.get(i);
int endTime = endTimes.get(i); int endTime = endTimes.get(i);
int diff = endTime - time; if (opening) {
if (startTime < endTime && endTime != -1) { if (startTime < endTime || endTime == -1) {
if (days[d] && !checkNext) { if (days[d] && !checkAnotherDay) {
if ((time <= endTime) && ((!nearToClosing && diff >= freeLimit) || (nearToClosing && diff <= nearToClosingLimit))) { int diff = startTime - time;
formatTime(endTime, sb); if ((time <= startTime) && ((!nearToEvent && diff <= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) {
formatTime(startTime, sb);
break;
}
}
} else {
int diff = -1;
if (time <= startTime && days[d] && !checkAnotherDay) {
diff = startTime - time;
} else if (time > endTime && days[ad] && checkAnotherDay) {
diff = 24 * 60 - endTime + time;
}
if ((diff != -1) && ((!nearToEvent && diff <= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) {
formatTime(startTime, sb);
break; break;
} }
} }
} else { } else {
if (time <= endTime && days[d] && !checkNext) { if (startTime < endTime && endTime != -1) {
formatTime(endTime, sb); if (days[d] && !checkAnotherDay) {
break; int diff = endTime - time;
} else if (time < endTime && days[n] && checkNext) { if ((time <= endTime) && ((!nearToEvent && diff >= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) {
if ((!nearToClosing && diff >= freeLimit) || (nearToClosing && diff <= nearToClosingLimit)) { formatTime(endTime, sb);
break;
}
}
} else {
int diff = -1;
if (time <= endTime && days[d] && !checkAnotherDay) {
diff = 24 * 60 - time + endTime;
} else if (time < endTime && days[ad] && checkAnotherDay) {
diff = startTime - time;
}
if ((diff != -1) && ((!nearToEvent && diff >= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) {
formatTime(endTime, sb); formatTime(endTime, sb);
break; break;
} }
@ -1123,12 +1121,7 @@ public class OpeningHoursParser {
} }
@Override @Override
public String getOpenedFromTime(Calendar cal, boolean checkPrevious) { public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) {
return "";
}
@Override
public String getClosedAtTime(Calendar cal, boolean checkNext, boolean nearToClosing) {
return ""; return "";
} }

View file

@ -9,10 +9,10 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
--> -->
<string name="shared_string_opened">Opened</string>
<string name="opened_from">Opened from</string> <string name="opened_from">Opened from</string>
<string name="opened_till">Opened till</string> <string name="opened_till">Opened till</string>
<string name="will_be_closed_at">Will be closed at</string> <string name="will_be_closed_at">Will be closed at</string>
<string name="will_be_opened_at">Will be opened 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

@ -1106,17 +1106,19 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo
View openingHoursView = view.findViewById(R.id.opening_hours_view); View openingHoursView = view.findViewById(R.id.opening_hours_view);
TextView openingHoursTextView = (TextView) view.findViewById(R.id.opening_hours_text_view); TextView openingHoursTextView = (TextView) view.findViewById(R.id.opening_hours_text_view);
OpeningHoursInfo openingHoursInfo = menu.getOpeningHoursInfo(); OpeningHoursInfo openingHoursInfo = menu.getOpeningHoursInfo();
if (openingHoursInfo != null) { if (openingHoursInfo != null && openingHoursInfo.containsInfo()) {
openingHoursTextView.setTextColor(ContextCompat.getColor(getContext(), openingHoursInfo.isOpened() ? R.color.ctx_menu_amenity_opened_text_color : R.color.ctx_menu_amenity_closed_text_color)); openingHoursTextView.setTextColor(ContextCompat.getColor(getContext(), openingHoursInfo.isOpened() ? R.color.ctx_menu_amenity_opened_text_color : R.color.ctx_menu_amenity_closed_text_color));
String openingHoursStr = ""; String openingHoursStr = "";
if (openingHoursInfo.isOpened24_7()) { if (openingHoursInfo.isOpened24_7()) {
openingHoursStr = getString(R.string.shared_string_is_open_24_7); openingHoursStr = getString(R.string.shared_string_is_open_24_7);
} else if (!Algorithms.isEmpty(openingHoursInfo.getOpenedFromTime())) { } else if (!Algorithms.isEmpty(openingHoursInfo.getOpeningTime())) {
openingHoursStr = getString(R.string.opened_from) + " " + openingHoursInfo.getOpenedFromTime(); openingHoursStr = getString(R.string.opened_from) + " " + openingHoursInfo.getOpeningTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getClosedAtTime())) { } else if (!Algorithms.isEmpty(openingHoursInfo.getNearToOpeningTime())) {
openingHoursStr = getString(R.string.will_be_closed_at) + " " + openingHoursInfo.getClosedAtTime(); openingHoursStr = getString(R.string.will_be_opened_at) + " " + openingHoursInfo.getNearToOpeningTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getOpenedTillTime())) { } else if (!Algorithms.isEmpty(openingHoursInfo.getClosingTime())) {
openingHoursStr = getString(R.string.opened_till) + " " + openingHoursInfo.getOpenedTillTime(); openingHoursStr = getString(R.string.opened_till) + " " + openingHoursInfo.getClosingTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getNearToClosingTime())) {
openingHoursStr = getString(R.string.will_be_closed_at) + " " + openingHoursInfo.getNearToClosingTime();
} }
openingHoursTextView.setText(openingHoursStr); openingHoursTextView.setText(openingHoursStr);
openingHoursView.setVisibility(View.VISIBLE); openingHoursView.setVisibility(View.VISIBLE);

View file

@ -1,12 +1,15 @@
package net.osmand.plus.mapcontextmenu; package net.osmand.plus.mapcontextmenu;
import net.osmand.util.Algorithms;
public class OpeningHoursInfo { public class OpeningHoursInfo {
private boolean opened; private boolean opened;
private boolean opened24_7; private boolean opened24_7;
private String openedFromTime = ""; private String openingTime = "";
private String closedAtTime = ""; private String nearToOpeningTime = "";
private String openedTillTime = ""; private String closingTime = "";
private String nearToClosingTime = "";
public boolean isOpened() { public boolean isOpened() {
return opened; return opened;
@ -24,27 +27,43 @@ public class OpeningHoursInfo {
this.opened24_7 = opened24_7; this.opened24_7 = opened24_7;
} }
public String getOpenedFromTime() { public String getOpeningTime() {
return openedFromTime; return openingTime;
} }
public void setOpenedFromTime(String openFromTime) { public void setOpeningTime(String openFromTime) {
this.openedFromTime = openFromTime; this.openingTime = openFromTime;
} }
public String getClosedAtTime() { public String getNearToOpeningTime() {
return closedAtTime; return nearToOpeningTime;
} }
public void setClosedAtTime(String closedAtTime) { public void setNearToOpeningTime(String nearToOpeningTime) {
this.closedAtTime = closedAtTime; this.nearToOpeningTime = nearToOpeningTime;
} }
public String getOpenedTillTime() { public String getClosingTime() {
return openedTillTime; return closingTime;
} }
public void setOpenedTillTime(String openedTillTime) { public void setClosingTime(String closingTime) {
this.openedTillTime = openedTillTime; this.closingTime = closingTime;
}
public String getNearToClosingTime() {
return nearToClosingTime;
}
public void setNearToClosingTime(String nearToClosingTime) {
this.nearToClosingTime = nearToClosingTime;
}
public boolean containsInfo() {
return opened24_7
|| !Algorithms.isEmpty(openingTime)
|| !Algorithms.isEmpty(nearToOpeningTime)
|| !Algorithms.isEmpty(closingTime)
|| !Algorithms.isEmpty(nearToClosingTime);
} }
} }

View file

@ -143,14 +143,12 @@ public class AmenityMenuController extends MenuController {
} else { } else {
OpeningHoursInfo info = new OpeningHoursInfo(); OpeningHoursInfo info = new OpeningHoursInfo();
Calendar cal = Calendar.getInstance(); Calendar cal = Calendar.getInstance();
if (openingHours.isOpenedForTime(cal)) { info.setOpened(openingHours.isOpenedForTime(cal));
info.setOpened(true); info.setOpened24_7(openingHours.isOpened24_7());
info.setOpened24_7(openingHours.isOpen24_7()); info.setOpeningTime(openingHours.getOpeningTime(cal));
// info.setOpenedFromTime(openingHours.getOpenedFromTime(cal)); info.setNearToOpeningTime(openingHours.getNearToOpeningTime(cal));
info.setOpenedFromTime(""); info.setClosingTime(openingHours.getClosingTime(cal));
info.setClosedAtTime(openingHours.getClosedAtTime(cal)); info.setNearToClosingTime(openingHours.getNearToClosingTime(cal));
info.setOpenedTillTime(openingHours.getOpenedTillTime(cal));
}
return info; return info;
} }
} }