refactor open hours display

This commit is contained in:
veliymolfar 2020-07-01 13:36:47 +03:00
parent 36008e7879
commit 979010f28c
3 changed files with 37 additions and 36 deletions

View file

@ -389,10 +389,6 @@ public class OpeningHoursParser {
return openingTime;
}
public String getOpeningTomorrow(Calendar calendar) {
return getOpeningTomorrow(calendar, ALL_SEQUENCES);
}
public String getOpeningDay(Calendar calendar, int sequenceIndex) {
Calendar cal = (Calendar) calendar.clone();
String openingTime = "";

View file

@ -559,27 +559,9 @@ public abstract class MenuController extends BaseMenuController implements Colla
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {
if (openingHoursInfo != null) {
StringBuilder sb = new StringBuilder();
int colorOpen = mapActivity.getResources().getColor(R.color.ctx_menu_amenity_opened_text_color);
int colorClosed = mapActivity.getResources().getColor(R.color.ctx_menu_amenity_closed_text_color);
int[] pos = new int[openingHoursInfo.size()];
for (int i = 0; i < openingHoursInfo.size(); i++) {
OpeningHours.Info info = openingHoursInfo.get(i);
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(info.getInfo());
pos[i] = sb.length();
}
SpannableString infoStr = new SpannableString(sb.toString());
int k = 0;
for (int i = 0; i < openingHoursInfo.size(); i++) {
OpeningHours.Info info = openingHoursInfo.get(i);
infoStr.setSpan(new ForegroundColorSpan(info.isOpened() ? colorOpen : colorClosed), k, pos[i], 0);
k = pos[i];
}
return infoStr;
return getSpannableOpeningHours(openingHoursInfo, colorOpen, colorClosed);
} else if (shouldShowMapSize()) {
return mapActivity.getString(R.string.file_size_in_mb, indexItem.getArchiveSizeMB());
}
@ -597,6 +579,29 @@ public abstract class MenuController extends BaseMenuController implements Colla
return 0;
}
public static SpannableString getSpannableOpeningHours(List<OpeningHours.Info> openingHoursInfo,
int colorOpen,
int colorClosed) {
StringBuilder sb = new StringBuilder();
int[] pos = new int[openingHoursInfo.size()];
for (int i = 0; i < openingHoursInfo.size(); i++) {
OpeningHours.Info info = openingHoursInfo.get(i);
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(info.getInfo());
pos[i] = sb.length();
}
SpannableString infoStr = new SpannableString(sb.toString());
int k = 0;
for (int i = 0; i < openingHoursInfo.size(); i++) {
OpeningHours.Info info = openingHoursInfo.get(i);
infoStr.setSpan(new ForegroundColorSpan(info.isOpened() ? colorOpen : colorClosed), k, pos[i], 0);
k = pos[i];
}
return infoStr;
}
private boolean shouldShowMapSize() {
return indexItem != null && !downloaded;
}

View file

@ -3,6 +3,7 @@ package net.osmand.plus.search;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -13,7 +14,9 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
import androidx.core.view.ViewCompat;
import net.osmand.AndroidUtils;
@ -26,6 +29,7 @@ import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.UiUtilities.UpdateLocationViewCache;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.search.listitems.QuickSearchHeaderListItem;
import net.osmand.plus.search.listitems.QuickSearchListItem;
import net.osmand.plus.search.listitems.QuickSearchListItemType;
@ -402,21 +406,17 @@ public class QuickSearchListAdapter extends ArrayAdapter<QuickSearchListItem> {
&& ((Amenity) listItem.getSearchResult().object).getOpeningHours() != null) {
Amenity amenity = (Amenity) listItem.getSearchResult().object;
OpeningHoursParser.OpeningHours rs = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (rs != null) {
Calendar inst = Calendar.getInstance();
inst.setTimeInMillis(System.currentTimeMillis());
boolean worksNow = !amenity.isClosed() && rs.isOpenedForTime(inst);
inst.setTimeInMillis(System.currentTimeMillis() + 30 * 60 * 1000); // 30 minutes later
boolean worksLater = rs.isOpenedForTime(inst);
int colorId = worksNow ? worksLater ? R.color.color_ok : R.color.color_intermediate : R.color.color_warning;
if (rs != null && rs.getInfo() != null) {
int colorOpen = R.color.ctx_menu_amenity_opened_text_color;
int colorClosed = R.color.ctx_menu_amenity_closed_text_color;
SpannableString openHours = MenuController.getSpannableOpeningHours(
rs.getInfo(),
ContextCompat.getColor(app, colorOpen),
ContextCompat.getColor(app, colorClosed));
int colorId = rs.isOpenedForTime(Calendar.getInstance()) ? colorOpen : colorClosed;
timeLayout.setVisibility(View.VISIBLE);
timeIcon.setImageDrawable(app.getUIUtilities().getIcon(R.drawable.ic_action_time_16, colorId));
timeText.setTextColor(app.getResources().getColor(colorId));
String rt = amenity.isClosed()
? app.getResources().getString(R.string.poi_operational_status_closed)
: worksNow ? rs.getCurrentRuleTime(inst) : app.getString(R.string.will_open_tomorrow_at) + " " + rs.getOpeningTomorrow(inst);
timeText.setText(rt == null ? "" : rt);
timeText.setText(openHours);
} else {
timeLayout.setVisibility(View.GONE);
}