Compare commits

...

2 commits

Author SHA1 Message Date
Victor Shcherb
51857e827e Refactor group markers 2021-04-19 14:45:45 +02:00
Victor Shcherb
beb8058495 Remove CategoriesSubHeader 2021-04-19 13:46:14 +02:00
4 changed files with 111 additions and 96 deletions

View file

@ -2,23 +2,3 @@ package net.osmand.plus.mapmarkers;
import androidx.annotation.DrawableRes;
public class CategoriesSubHeader {
@DrawableRes
private int iconRes;
private MapMarkersGroup group;
public CategoriesSubHeader(int iconRes, MapMarkersGroup group) {
this.iconRes = iconRes;
this.group = group;
}
@DrawableRes
public int getIconRes() {
return iconRes;
}
public MapMarkersGroup getGroup() {
return group;
}
}

View file

@ -31,7 +31,6 @@ public class MapMarkersGroup {
private TravelArticle wikivoyageArticle;
// TODO should be removed from this class:
private GroupHeader header;
private CategoriesSubHeader categoriesSubHeader;
private ShowHideHistoryButton showHideHistoryButton;
public MapMarkersGroup() {
@ -76,10 +75,6 @@ public class MapMarkersGroup {
this.header = header;
}
public void setCategoriesSubHeader(CategoriesSubHeader categoriesSubHeader) {
this.categoriesSubHeader = categoriesSubHeader;
}
public void setShowHideHistoryButton(ShowHideHistoryButton showHideHistoryButton) {
this.showHideHistoryButton = showHideHistoryButton;
}
@ -144,10 +139,6 @@ public class MapMarkersGroup {
return header;
}
public CategoriesSubHeader getCategoriesSubHeader() {
return categoriesSubHeader;
}
public ShowHideHistoryButton getShowHideHistoryButton() {
return showHideHistoryButton;
}

View file

@ -481,17 +481,12 @@ public class MapMarkersHelper {
private void createHeadersInGroup(@NonNull MapMarkersGroup group) {
int type = group.getType();
int headerIconId = 0;
int subHeaderIconId = 0;
if (type != -1) {
headerIconId = type == MapMarkersGroup.FAVORITES_TYPE
? R.drawable.ic_action_favorite : R.drawable.ic_action_polygom_dark;
subHeaderIconId = R.drawable.ic_action_filter;
}
GroupHeader header = new GroupHeader(headerIconId, group);
CategoriesSubHeader categoriesSubHeader = new CategoriesSubHeader(subHeaderIconId, group);
group.setHeader(header);
group.setCategoriesSubHeader(categoriesSubHeader);
}
private void removeMarkerFromGroup(MapMarker marker) {

View file

@ -7,8 +7,10 @@ import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
@ -21,7 +23,6 @@ import net.osmand.IndexConstants;
import net.osmand.data.LatLon;
import net.osmand.plus.GpxSelectionHelper;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.mapmarkers.CategoriesSubHeader;
import net.osmand.plus.mapmarkers.MapMarkersHelper;
import net.osmand.plus.mapmarkers.GroupHeader;
import net.osmand.plus.mapmarkers.MapMarker;
@ -37,6 +38,7 @@ import net.osmand.plus.mapmarkers.SelectWptCategoriesBottomSheetDialogFragment;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.data.TravelHelper;
import net.osmand.util.Algorithms;
import java.io.File;
import java.text.SimpleDateFormat;
@ -55,10 +57,6 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
private static final int SHOW_HIDE_HISTORY_TYPE = 3;
private static final int CATEGORIES_TYPE = 4;
private static final int TODAY_HEADER = 56;
private static final int YESTERDAY_HEADER = 57;
private static final int LAST_SEVEN_DAYS_HEADER = 58;
private static final int THIS_YEAR_HEADER = 59;
private MapActivity mapActivity;
private OsmandApplication app;
@ -108,50 +106,85 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
List<MapMarkersGroup> groups = new ArrayList<>(helper.getMapMarkersGroups());
groups.addAll(helper.getGroupsForDisplayedGpx());
groups.addAll(helper.getGroupsForSavedArticlesTravelBook());
// evaluate time constants
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
long currentTimeMillis = System.currentTimeMillis();
Calendar currentDateCalendar = Calendar.getInstance();
currentDateCalendar.setTimeInMillis(currentTimeMillis);
// evaluate today, yesterday, last 7 days
String today = dateFormat.format(currentDateCalendar.getTime());
currentDateCalendar.add(Calendar.DAY_OF_YEAR, -1);
String yesterday = dateFormat.format(currentDateCalendar.getTime());
currentDateCalendar.set(Calendar.HOUR_OF_DAY, 0);
currentDateCalendar.set(Calendar.MINUTE, 0);
currentDateCalendar.add(Calendar.DAY_OF_YEAR, -6);
long last7Days = currentDateCalendar.getTimeInMillis();
// evaluate this year & last 3 months
currentDateCalendar.setTimeInMillis(currentTimeMillis);
String thisYear = dateFormat.format(currentDateCalendar.getTime()).substring(0, 5);
currentDateCalendar.add(Calendar.MONTH, -1);
String monthMinus1 = dateFormat.format(currentDateCalendar.getTime()).substring(0, 8);
currentDateCalendar.add(Calendar.MONTH, -1);
String monthMinus2 = dateFormat.format(currentDateCalendar.getTime()).substring(0, 8);
currentDateCalendar.add(Calendar.MONTH, -1);
String monthMinus3 = dateFormat.format(currentDateCalendar.getTime()).substring(0, 8);
Calendar markerCalendar = Calendar.getInstance();
for (int i = 0; i < groups.size(); i++) {
MapMarkersGroup group = groups.get(i);
if (!group.isVisible()) {
continue;
}
String markerGroupName = group.getName();
if (markerGroupName == null) {
int previousDateHeader = -1;
int monthsDisplayed = 0;
Calendar currentDateCalendar = Calendar.getInstance();
currentDateCalendar.setTimeInMillis(System.currentTimeMillis());
int currentDay = currentDateCalendar.get(Calendar.DAY_OF_YEAR);
int currentMonth = currentDateCalendar.get(Calendar.MONTH);
int currentYear = currentDateCalendar.get(Calendar.YEAR);
Calendar markerCalendar = Calendar.getInstance();
if (markerGroupName == null) {
int previousGroupDateId = 0;
int monthsDisplayed = 0;
List<MapMarker> groupMarkers = group.getActiveMarkers();
for (int j = 0; j < groupMarkers.size(); j++) {
MapMarker marker = groupMarkers.get(j);
String markerDate = dateFormat.format(new Date(marker.creationDate));
int currentGroupDateId;
MarkerGroupItem currentGroupItem = null ;
if (marker.creationDate >= currentTimeMillis || (today.equals(markerDate))) {
currentGroupDateId = -1;
currentGroupItem = MarkerGroupItem.TODAY_HEADER;
} else if (yesterday.equals(markerDate)) {
currentGroupDateId = -2;
currentGroupItem = MarkerGroupItem.YESTERDAY_HEADER;
} else if (marker.creationDate >= last7Days) {
currentGroupDateId = -3;
currentGroupItem = MarkerGroupItem.LAST_SEVEN_DAYS_HEADER;
} else if (markerDate.startsWith(monthMinus1)) {
currentGroupDateId = -5;
} else if (markerDate.startsWith(monthMinus2)) {
currentGroupDateId = -6;
} else if (markerDate.startsWith(monthMinus3)) {
currentGroupDateId = -7;
} else if (markerDate.startsWith(thisYear)) {
currentGroupItem = MarkerGroupItem.THIS_YEAR_HEADER;
currentGroupDateId = -4;
} else {
markerCalendar.setTimeInMillis(marker.creationDate);
int markerDay = markerCalendar.get(Calendar.DAY_OF_YEAR);
int markerMonth = markerCalendar.get(Calendar.MONTH);
int markerYear = markerCalendar.get(Calendar.YEAR);
if (markerYear == currentYear) {
if (markerDay == currentDay && previousDateHeader != TODAY_HEADER) {
items.add(TODAY_HEADER);
previousDateHeader = TODAY_HEADER;
} else if (markerDay == currentDay - 1 && previousDateHeader != YESTERDAY_HEADER) {
items.add(YESTERDAY_HEADER);
previousDateHeader = YESTERDAY_HEADER;
} else if (currentDay - markerDay >= 2 && currentDay - markerDay <= 8 && previousDateHeader != LAST_SEVEN_DAYS_HEADER) {
items.add(LAST_SEVEN_DAYS_HEADER);
previousDateHeader = LAST_SEVEN_DAYS_HEADER;
} else if (currentDay - markerDay > 8 && monthsDisplayed < 3 && previousDateHeader != markerMonth) {
items.add(markerMonth);
previousDateHeader = markerMonth;
monthsDisplayed += 1;
} else if (currentMonth - markerMonth >= 4 && previousDateHeader != markerMonth && previousDateHeader != THIS_YEAR_HEADER) {
items.add(THIS_YEAR_HEADER);
previousDateHeader = THIS_YEAR_HEADER;
currentGroupDateId = markerCalendar.get(Calendar.YEAR);
}
} else if (previousDateHeader != markerYear) {
items.add(markerYear);
previousDateHeader = markerYear;
if (previousGroupDateId != currentGroupDateId) {
if (currentGroupItem != null) {
items.add(currentGroupItem);
} else if(currentGroupDateId < 0) {
SimpleDateFormat monthdateFormat = new SimpleDateFormat("LLLL", Locale.getDefault());
String monthStr = monthdateFormat.format(new Date(marker.creationDate));
if (monthStr.length() > 1) {
monthStr = Algorithms.capitalizeFirstLetter(monthStr);
}
items.add(new MarkerGroupItem(monthStr));
} else {
items.add(new MarkerGroupItem(currentGroupDateId + ""));
}
previousGroupDateId = currentGroupDateId;
}
items.add(marker);
}
@ -160,7 +193,7 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
items.add(header);
if (!group.isDisabled()) {
if (group.getWptCategories() != null && !group.getWptCategories().isEmpty()) {
CategoriesSubHeader categoriesSubHeader = group.getCategoriesSubHeader();
CategoriesSubHeader categoriesSubHeader = new CategoriesSubHeader(group);
items.add(categoriesSubHeader);
}
TravelHelper travelHelper = mapActivity.getMyApplication().getTravelHelper();
@ -380,23 +413,10 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
final MapMarkerHeaderViewHolder headerViewHolder = (MapMarkerHeaderViewHolder) holder;
final Object header = getItem(position);
String headerString;
if (header instanceof Integer) {
if (header instanceof MarkerGroupItem) {
headerViewHolder.icon.setVisibility(View.GONE);
headerViewHolder.iconSpace.setVisibility(View.VISIBLE);
Integer dateHeader = (Integer) header;
if (dateHeader == TODAY_HEADER) {
headerString = app.getString(R.string.today);
} else if (dateHeader == YESTERDAY_HEADER) {
headerString = app.getString(R.string.yesterday);
} else if (dateHeader == LAST_SEVEN_DAYS_HEADER) {
headerString = app.getString(R.string.last_seven_days);
} else if (dateHeader == THIS_YEAR_HEADER) {
headerString = app.getString(R.string.this_year);
} else if (dateHeader / 100 == 0) {
headerString = getMonth(dateHeader);
} else {
headerString = String.valueOf(dateHeader);
}
headerString = ((MarkerGroupItem) header).getName(app);
headerViewHolder.disableGroupSwitch.setVisibility(View.GONE);
headerViewHolder.articleDescription.setVisibility(View.GONE);
} else if (header instanceof GroupHeader) {
@ -616,17 +636,46 @@ public class MapMarkersGroupsAdapter extends RecyclerView.Adapter<RecyclerView.V
return items.get(position);
}
private String getMonth(int month) {
SimpleDateFormat dateFormat = new SimpleDateFormat("LLLL", Locale.getDefault());
Date date = new Date();
date.setMonth(month);
String monthStr = dateFormat.format(date);
if (monthStr.length() > 1) {
monthStr = Character.toUpperCase(monthStr.charAt(0)) + monthStr.substring(1);
public static class MarkerGroupItem {
static final MarkerGroupItem TODAY_HEADER = new MarkerGroupItem(R.string.today);
static final MarkerGroupItem YESTERDAY_HEADER = new MarkerGroupItem(R.string.yesterday);
static final MarkerGroupItem LAST_SEVEN_DAYS_HEADER = new MarkerGroupItem(R.string.last_seven_days);
static final MarkerGroupItem THIS_YEAR_HEADER = new MarkerGroupItem(R.string.this_year);
private @StringRes int iname;
protected String name;
public MarkerGroupItem(@StringRes int name) {
this.iname = name;
}
return monthStr;
public MarkerGroupItem(String name) {
this.name = name;
}
public String getName(OsmandApplication app) {
if (name == null && iname != 0) {
name = app.getString(iname);
}
return name;
}
}
public class CategoriesSubHeader {
private MapMarkersGroup group;
public CategoriesSubHeader(MapMarkersGroup group) {
this.group = group;
}
public MapMarkersGroup getGroup() {
return group;
}
}
public interface MapMarkersGroupsAdapterListener {
void onItemClick(View view);