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;
}
public boolean isOpen24_7() {
boolean open24_7 = false;
public boolean isOpened24_7() {
boolean opened24_7 = false;
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) {
String openFrom = getOpenFromTimeDay(cal);
if (Algorithms.isEmpty(openFrom)) {
openFrom = getOpenFromTimePrevious(cal);
}
return openFrom;
public String getNearToOpeningTime(Calendar cal) {
return getOpeningTime(cal, false);
}
private String getOpenFromTimeDay(Calendar cal) {
String openFrom = "";
public String getOpeningTime(Calendar cal) {
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) {
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) {
String openFrom = "";
private String getOpeningTimePrevious(Calendar cal, boolean nearToOpening) {
String openingAt = "";
for (OpeningHoursRule r : rules) {
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) {
return getClosedTime(cal, true);
public String getNearToClosingTime(Calendar cal) {
return getClosingTime(cal, true);
}
public String getOpenedTillTime(Calendar cal) {
return getClosedTime(cal, false);
public String getClosingTime(Calendar cal) {
return getClosingTime(cal, false);
}
private String getClosedTime(Calendar cal, boolean nearToClosing) {
String closedAt = getClosedAtTimeDay(cal, nearToClosing);
if (Algorithms.isEmpty(closedAt)) {
closedAt = getClosedAtTimeNextDay(cal, nearToClosing);
private String getClosingTime(Calendar cal, boolean nearToClosing) {
String closingAt = getClosingTimeDay(cal, nearToClosing);
if (Algorithms.isEmpty(closingAt)) {
closingAt = getClosingTimeNext(cal, nearToClosing);
}
return closedAt;
return closingAt;
}
private String getClosedAtTimeDay(Calendar cal, boolean nearToClosing) {
String closedAt = "";
private String getClosingTimeDay(Calendar cal, boolean nearToClosing) {
String closingAt = "";
for (OpeningHoursRule r : rules) {
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) {
String closedAt = "";
private String getClosingTimeNext(Calendar cal, boolean nearToClosing) {
String closingAt = "";
for (OpeningHoursRule r : rules) {
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) {
@ -424,9 +432,7 @@ public class OpeningHoursParser {
boolean isOpened24_7();
String getOpenedFromTime(Calendar cal, boolean checkPrevious);
String getClosedAtTime(Calendar cal, boolean checkNext, boolean nearToClosing);
String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening);
}
/**
@ -857,62 +863,54 @@ public class OpeningHoursParser {
}
@Override
public String getOpenedFromTime(Calendar cal, boolean checkPrevious) {
int limit = 300;
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;
public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) {
int nearToEventLimit = 120;
int freeLimit = 300;
StringBuilder sb = new StringBuilder();
int d = getCurrentDay(cal);
int n = getNextDay(d);
int ad = opening ? getNextDay(d) : getPreviousDay(d);
int time = getCurrentTimeInMinutes(cal);
for (int i = 0; i < startTimes.size(); i++) {
int startTime = startTimes.get(i);
int endTime = endTimes.get(i);
int diff = endTime - time;
if (opening) {
if (startTime < endTime || endTime == -1) {
if (days[d] && !checkAnotherDay) {
int diff = startTime - time;
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;
}
}
} else {
if (startTime < endTime && endTime != -1) {
if (days[d] && !checkNext) {
if ((time <= endTime) && ((!nearToClosing && diff >= freeLimit) || (nearToClosing && diff <= nearToClosingLimit))) {
if (days[d] && !checkAnotherDay) {
int diff = endTime - time;
if ((time <= endTime) && ((!nearToEvent && diff >= freeLimit) || (nearToEvent && diff <= nearToEventLimit))) {
formatTime(endTime, sb);
break;
}
}
} else {
if (time <= endTime && days[d] && !checkNext) {
formatTime(endTime, sb);
break;
} else if (time < endTime && days[n] && checkNext) {
if ((!nearToClosing && diff >= freeLimit) || (nearToClosing && diff <= nearToClosingLimit)) {
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);
break;
}
@ -1123,12 +1121,7 @@ public class OpeningHoursParser {
}
@Override
public String getOpenedFromTime(Calendar cal, boolean checkPrevious) {
return "";
}
@Override
public String getClosedAtTime(Calendar cal, boolean checkNext, boolean nearToClosing) {
public String getTime(Calendar cal, boolean checkAnotherDay, boolean nearToEvent, boolean opening) {
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).
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_till">Opened till</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="release_3_0">
\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);
TextView openingHoursTextView = (TextView) view.findViewById(R.id.opening_hours_text_view);
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));
String openingHoursStr = "";
if (openingHoursInfo.isOpened24_7()) {
openingHoursStr = getString(R.string.shared_string_is_open_24_7);
} else if (!Algorithms.isEmpty(openingHoursInfo.getOpenedFromTime())) {
openingHoursStr = getString(R.string.opened_from) + " " + openingHoursInfo.getOpenedFromTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getClosedAtTime())) {
openingHoursStr = getString(R.string.will_be_closed_at) + " " + openingHoursInfo.getClosedAtTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getOpenedTillTime())) {
openingHoursStr = getString(R.string.opened_till) + " " + openingHoursInfo.getOpenedTillTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getOpeningTime())) {
openingHoursStr = getString(R.string.opened_from) + " " + openingHoursInfo.getOpeningTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getNearToOpeningTime())) {
openingHoursStr = getString(R.string.will_be_opened_at) + " " + openingHoursInfo.getNearToOpeningTime();
} else if (!Algorithms.isEmpty(openingHoursInfo.getClosingTime())) {
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);
openingHoursView.setVisibility(View.VISIBLE);

View file

@ -1,12 +1,15 @@
package net.osmand.plus.mapcontextmenu;
import net.osmand.util.Algorithms;
public class OpeningHoursInfo {
private boolean opened;
private boolean opened24_7;
private String openedFromTime = "";
private String closedAtTime = "";
private String openedTillTime = "";
private String openingTime = "";
private String nearToOpeningTime = "";
private String closingTime = "";
private String nearToClosingTime = "";
public boolean isOpened() {
return opened;
@ -24,27 +27,43 @@ public class OpeningHoursInfo {
this.opened24_7 = opened24_7;
}
public String getOpenedFromTime() {
return openedFromTime;
public String getOpeningTime() {
return openingTime;
}
public void setOpenedFromTime(String openFromTime) {
this.openedFromTime = openFromTime;
public void setOpeningTime(String openFromTime) {
this.openingTime = openFromTime;
}
public String getClosedAtTime() {
return closedAtTime;
public String getNearToOpeningTime() {
return nearToOpeningTime;
}
public void setClosedAtTime(String closedAtTime) {
this.closedAtTime = closedAtTime;
public void setNearToOpeningTime(String nearToOpeningTime) {
this.nearToOpeningTime = nearToOpeningTime;
}
public String getOpenedTillTime() {
return openedTillTime;
public String getClosingTime() {
return closingTime;
}
public void setOpenedTillTime(String openedTillTime) {
this.openedTillTime = openedTillTime;
public void setClosingTime(String closingTime) {
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 {
OpeningHoursInfo info = new OpeningHoursInfo();
Calendar cal = Calendar.getInstance();
if (openingHours.isOpenedForTime(cal)) {
info.setOpened(true);
info.setOpened24_7(openingHours.isOpen24_7());
// info.setOpenedFromTime(openingHours.getOpenedFromTime(cal));
info.setOpenedFromTime("");
info.setClosedAtTime(openingHours.getClosedAtTime(cal));
info.setOpenedTillTime(openingHours.getOpenedTillTime(cal));
}
info.setOpened(openingHours.isOpenedForTime(cal));
info.setOpened24_7(openingHours.isOpened24_7());
info.setOpeningTime(openingHours.getOpeningTime(cal));
info.setNearToOpeningTime(openingHours.getNearToOpeningTime(cal));
info.setClosingTime(openingHours.getClosingTime(cal));
info.setNearToClosingTime(openingHours.getNearToClosingTime(cal));
return info;
}
}