Refactor favorite points

This commit is contained in:
Victor Shcherb 2020-01-06 18:42:24 +01:00
parent e1ce9ec73e
commit 6d77381ee0
7 changed files with 101 additions and 160 deletions

View file

@ -2,4 +2,5 @@ armeabi/
x86/ x86/
armeabi-v7a/ armeabi-v7a/
mips/ mips/
arm64-v8a/ arm64-v8a/
x86_64/

View file

@ -3,7 +3,9 @@ package net.osmand.data;
import java.io.Serializable; import java.io.Serializable;
import android.content.Context; import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import net.osmand.GPXUtilities.WptPt; import net.osmand.GPXUtilities.WptPt;
import net.osmand.plus.FavouritesDbHelper; import net.osmand.plus.FavouritesDbHelper;
@ -11,12 +13,17 @@ import net.osmand.plus.R;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import static net.osmand.plus.FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY; import static net.osmand.plus.FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY;
import static net.osmand.plus.FavouritesDbHelper.PersonalPointType.HOME;
import static net.osmand.plus.FavouritesDbHelper.PersonalPointType.PARKING;
import static net.osmand.plus.FavouritesDbHelper.PersonalPointType.WORK;
public class FavouritePoint implements Serializable, LocationPoint { public class FavouritePoint implements Serializable, LocationPoint {
private static final long serialVersionUID = 729654300829771466L; private static final long serialVersionUID = 729654300829771466L;
private static final String HIDDEN = "hidden"; private static final String HIDDEN = "hidden";
protected String name = ""; protected String name = "";
protected String description; protected String description;
protected String category = ""; protected String category = "";
@ -26,8 +33,9 @@ public class FavouritePoint implements Serializable, LocationPoint {
private double longitude; private double longitude;
private int color; private int color;
private boolean visible = true; private boolean visible = true;
private SpecialPointType specialPointType = null;
public FavouritePoint(){ public FavouritePoint() {
} }
public FavouritePoint(double latitude, double longitude, String name, String category) { public FavouritePoint(double latitude, double longitude, String name, String category) {
@ -38,6 +46,7 @@ public class FavouritePoint implements Serializable, LocationPoint {
name = ""; name = "";
} }
this.name = name; this.name = name;
initPersonalType();
} }
public FavouritePoint(FavouritePoint favouritePoint) { public FavouritePoint(FavouritePoint favouritePoint) {
@ -49,6 +58,21 @@ public class FavouritePoint implements Serializable, LocationPoint {
this.description = favouritePoint.description; this.description = favouritePoint.description;
this.visible = favouritePoint.visible; this.visible = favouritePoint.visible;
this.originObjectName = favouritePoint.originObjectName; this.originObjectName = favouritePoint.originObjectName;
initPersonalType();
}
private void initPersonalType() {
if(FavouritesDbHelper.FavoriteGroup.PERSONAL_CATEGORY.equals(category)) {
for(SpecialPointType p : SpecialPointType.values()) {
if(p.typeName.equals(this.name)) {
this.specialPointType = p;
}
}
}
}
public SpecialPointType getSpecialPointType() {
return specialPointType;
} }
public int getColor() { public int getColor() {
@ -72,10 +96,7 @@ public class FavouritePoint implements Serializable, LocationPoint {
} }
public boolean isPersonalPoint() { public boolean isPersonalPoint() {
// TODO: HW return specialPointType != null;
return name.equals(FavouritesDbHelper.getHomePointName())
|| name.equals(FavouritesDbHelper.getWorkPointName())
|| name.equals(FavouritesDbHelper.getParkingPointName());
} }
@Override @Override
@ -141,11 +162,12 @@ public class FavouritePoint implements Serializable, LocationPoint {
public void setCategory(String category) { public void setCategory(String category) {
this.category = category; this.category = category;
initPersonalType();
} }
public String getDisplayName(@NonNull Context ctx) { public String getDisplayName(@NonNull Context ctx) {
if (isPersonalPoint()) { if (specialPointType != null) {
return FavouritesDbHelper.getPersonalName(this, ctx); return specialPointType.getHumanString(ctx);
} }
return name; return name;
} }
@ -156,6 +178,7 @@ public class FavouritePoint implements Serializable, LocationPoint {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
initPersonalType();
} }
public String getDescription () { public String getDescription () {
@ -223,6 +246,49 @@ public class FavouritePoint implements Serializable, LocationPoint {
return result; return result;
} }
public enum SpecialPointType {
HOME("home", R.string.home_button, R.drawable.ic_action_home_dark),
WORK("work", R.string.work_button, R.drawable.ic_action_work),
PARKING("parking", R.string.map_widget_parking, R.drawable.ic_action_parking_dark);
private String typeName;
@StringRes
private int resId;
@DrawableRes
private int iconId;
SpecialPointType(@NonNull String typeName, @StringRes int resId, @DrawableRes int iconId) {
this.typeName = typeName;
this.resId = resId;
this.iconId = iconId;
}
public String getCategory() { return PERSONAL_CATEGORY; }
public String getName() {
return typeName;
}
public static SpecialPointType valueOfTypeName(@NonNull String typeName) {
for (SpecialPointType pt : values()) {
if (pt.typeName.equals(typeName)) {
return pt;
}
}
return null;
}
public int getIconId() {
return iconId;
}
public String getHumanString(@NonNull Context ctx) {
return ctx.getString(resId);
}
}
public static FavouritePoint fromWpt(@NonNull WptPt pt) { public static FavouritePoint fromWpt(@NonNull WptPt pt) {
String name = pt.name; String name = pt.name;
String categoryName = pt.category != null ? pt.category : ""; String categoryName = pt.category != null ? pt.category : "";

View file

@ -1,10 +1,8 @@
package net.osmand.plus; package net.osmand.plus;
import android.content.Context; import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import net.osmand.GPXUtilities; import net.osmand.GPXUtilities;
@ -67,45 +65,6 @@ public class FavouritesDbHelper {
private Map<FavouritePoint, AddressLookupRequest> addressRequestMap = new ConcurrentHashMap<>(); private Map<FavouritePoint, AddressLookupRequest> addressRequestMap = new ConcurrentHashMap<>();
public enum PersonalPointType {
HOME("home", R.string.home_button, R.drawable.ic_action_home_dark),
WORK("work", R.string.work_button, R.drawable.ic_action_work),
PARKING("parking", R.string.map_widget_parking, R.drawable.ic_action_parking_dark);
private String typeName;
@StringRes
private int resId;
@DrawableRes
private int iconId;
PersonalPointType(@NonNull String typeName, @StringRes int resId, @DrawableRes int iconId) {
this.typeName = typeName;
this.resId = resId;
this.iconId = iconId;
}
public String getName() {
return typeName;
}
public static PersonalPointType valueOfTypeName(@NonNull String typeName) {
for (PersonalPointType pt : values()) {
if (pt.typeName.equals(typeName)) {
return pt;
}
}
return null;
}
public int getIconId() {
return iconId;
}
public String getHumanString(@NonNull Context ctx) {
return ctx.getString(resId);
}
}
public FavouritesDbHelper(OsmandApplication context) { public FavouritesDbHelper(OsmandApplication context) {
this.context = context; this.context = context;
} }
@ -148,26 +107,6 @@ public class FavouritesDbHelper {
} }
public static String getPersonalName(FavouritePoint fp, Context ctx) {
return PersonalPointType.valueOfTypeName(fp.getName()).getHumanString(ctx);
}
public static int getPersonalIconId(String pointName) {
return PersonalPointType.valueOfTypeName(pointName).iconId;
}
public static String getParkingPointName() {
return PARKING.getName();
}
public static String getHomePointName() {
return HOME.getName();
}
public static String getWorkPointName() {
return WORK.getName();
}
public void loadFavorites() { public void loadFavorites() {
flatGroups.clear(); flatGroups.clear();
favoriteGroups.clear(); favoriteGroups.clear();
@ -207,25 +146,9 @@ public class FavouritesDbHelper {
}); });
} }
public FavouritePoint getWorkPoint() { public FavouritePoint getSpecialPoint(PersonalPointType pointType) {
return getPersonalPoint(WORK);
}
public FavouritePoint getHomePoint() {
return getPersonalPoint(HOME);
}
public FavouritePoint getParkingPoint() {
return getPersonalPoint(PARKING);
}
public void deleteParkingPoint() {
deleteFavourite(getParkingPoint());
}
private FavouritePoint getPersonalPoint(PersonalPointType pointType) {
for (FavouritePoint fp : cachedFavoritePoints) { for (FavouritePoint fp : cachedFavoritePoints) {
if ((PersonalPointType.valueOfTypeName(fp.getName())) == pointType) { if (fp.getSpecialPointType() == pointType) {
return fp; return fp;
} }
} }
@ -327,45 +250,20 @@ public class FavouritesDbHelper {
return true; return true;
} }
public void setHomePoint(@NonNull LatLon latLon, @Nullable String description) { public void setSpecialPoint(@NonNull LatLon latLon, FavouritePoint.SpecialPointType personalType, @Nullable String address) {
FavouritePoint homePoint = getHomePoint(); FavouritePoint point = getSpecialPoint(personalType);
if (homePoint != null) { if (point != null) {
editFavourite(homePoint, latLon.getLatitude(), latLon.getLongitude(), description); editFavourite(point, latLon.getLatitude(), latLon.getLongitude(), description);
} else { } else {
homePoint = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), HOME.getName(), PERSONAL_CATEGORY); point = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), personalType, personalType.getCategory());
homePoint.setDescription(description); point.setDescription(description);
addFavourite(homePoint); addFavourite(point);
} }
if (description == null) { if (address == null) {
lookupAddress(homePoint); lookupAddress(point);
} }
} }
public void setWorkPoint(@NonNull LatLon latLon, @Nullable String description) {
FavouritePoint workPoint = getWorkPoint();
if (workPoint != null) {
editFavourite(workPoint, latLon.getLatitude(), latLon.getLongitude(), description);
} else {
workPoint = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), WORK.getName(), PERSONAL_CATEGORY);
workPoint.setDescription(description);
addFavourite(workPoint);
}
if (description == null) {
lookupAddress(workPoint);
}
}
public void setParkingPoint(@NonNull LatLon latLon) {
FavouritePoint parkingPoint = getParkingPoint();
if (parkingPoint != null) {
editFavourite(parkingPoint, latLon.getLatitude(), latLon.getLongitude(), null);
} else {
parkingPoint = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), PARKING.getName(), PERSONAL_CATEGORY);
addFavourite(parkingPoint);
}
lookupAddress(parkingPoint);
}
public boolean addFavourite(FavouritePoint p) { public boolean addFavourite(FavouritePoint p) {
return addFavourite(p, true); return addFavourite(p, true);
} }
@ -392,19 +290,12 @@ public class FavouritesDbHelper {
return true; return true;
} }
public void lookupAddressAllPersonalPoints() { public void lookupAddressAllSpecialPoints() {
if (!context.isApplicationInitializing()) { if (!context.isApplicationInitializing()) {
FavouritePoint workPoint = getWorkPoint(); for(FavouritePoint p : getFavouritePoints()) {
if (workPoint != null) { if(p.getSpecialPointType() != null) {
lookupAddress(workPoint); lookupAddress(p);
} }
FavouritePoint homePoint = getHomePoint();
if (homePoint != null) {
lookupAddress(homePoint);
}
FavouritePoint parkingPoint = getParkingPoint();
if (parkingPoint != null) {
lookupAddress(parkingPoint);
} }
} }
} }
@ -725,25 +616,6 @@ public class FavouritesDbHelper {
return fp; return fp;
} }
public List<FavouritePoint> getNonPersonalVisibleFavouritePoints() {
List<FavouritePoint> fp = new ArrayList<>();
for (FavouritePoint p : getNonPersonalFavouritePoints()) {
if (p.isVisible()) {
fp.add(p);
}
}
return fp;
}
public List<FavouritePoint> getNonPersonalFavouritePoints() {
List<FavouritePoint> fp = new ArrayList<>();
for (FavouritePoint p : cachedFavoritePoints) {
if (!p.isPersonalPoint()) {
fp.add(p);
}
}
return fp;
}
@Nullable @Nullable
public FavouritePoint getVisibleFavByLatLon(@NonNull LatLon latLon) { public FavouritePoint getVisibleFavByLatLon(@NonNull LatLon latLon) {

View file

@ -422,11 +422,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven
} }
if (event == InitEvents.MAPS_INITIALIZED) { if (event == InitEvents.MAPS_INITIALIZED) {
// TODO investigate if this false cause any issues! // TODO investigate if this false cause any issues!
// !!! TODO HW it shouldn't be called every time start!!!
mapView.refreshMap(false); mapView.refreshMap(false);
if (dashboardOnMap != null) { if (dashboardOnMap != null) {
dashboardOnMap.updateLocation(true, true, false); dashboardOnMap.updateLocation(true, true, false);
} }
app.getFavorites().lookupAddressAllPersonalPoints(); app.getFavorites().lookupAddressAllSpecialPoints();
app.getTargetPointsHelper().lookupAddessAll(); app.getTargetPointsHelper().lookupAddessAll();
app.getMapMarkersHelper().lookupAddressAll(); app.getMapMarkersHelper().lookupAddressAll();
} }

View file

@ -131,9 +131,9 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag
private void loadFavorites() { private void loadFavorites() {
favouritePoints.clear(); favouritePoints.clear();
favouritePoints.addAll(getMyApplication().getFavorites().getNonPersonalVisibleFavouritePoints()); favouritePoints.addAll(getMyApplication().getFavorites().getVisibleFavouritePoints());
if (favouritePoints.isEmpty()) { if (favouritePoints.isEmpty()) {
favouritePoints.addAll(getMyApplication().getFavorites().getNonPersonalFavouritePoints()); favouritePoints.addAll(getMyApplication().getFavorites().getFavouritePoints());
} }
} }

View file

@ -480,11 +480,11 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment {
FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites(); FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites();
FavouritePoint point = null; FavouritePoint point = null;
if (item == PointType.HOME) { if (item == PointType.HOME) {
point = favorites.getHomePoint(); point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME);
} else if (item == PointType.WORK) { } else if (item == PointType.WORK) {
point = favorites.getWorkPoint(); point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK);
} else if (item == PointType.PARKING) { } else if (item == PointType.PARKING) {
point = favorites.getParkingPoint(); point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.PARKING);
} }
if (point != null) { if (point != null) {
ll = new LatLon(point.getLatitude(), point.getLongitude()); ll = new LatLon(point.getLatitude(), point.getLongitude());
@ -642,6 +642,7 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment {
if (item instanceof FavouritePoint) { if (item instanceof FavouritePoint) {
FavouritePoint point = (FavouritePoint) item; FavouritePoint point = (FavouritePoint) item;
favoriteViewHolder.title.setText(point.getDisplayName(app)); favoriteViewHolder.title.setText(point.getDisplayName(app));
// TODO HW: similar to overlay icon id
if (((FavouritePoint) item).isPersonalPoint()) { if (((FavouritePoint) item).isPersonalPoint()) {
int iconColor = app.getSettings().isLightContent() int iconColor = app.getSettings().isLightContent()
? R.color.icon_color_default_light : R.color.icon_color_default_dark; ? R.color.icon_color_default_light : R.color.icon_color_default_dark;

View file

@ -27,8 +27,8 @@ public class HomeWorkCard extends BaseCard {
protected void updateContent() { protected void updateContent() {
final TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper(); final TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper();
final FavouritesDbHelper favorites = getMyApplication().getFavorites(); final FavouritesDbHelper favorites = getMyApplication().getFavorites();
final FavouritePoint homePoint = favorites.getHomePoint(); final FavouritePoint homePoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME);
final FavouritePoint workPoint = favorites.getWorkPoint(); final FavouritePoint workPoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK);
TextView homeDescr = view.findViewById(R.id.home_button_descr); TextView homeDescr = view.findViewById(R.id.home_button_descr);
final TextView workDescr = view.findViewById(R.id.work_button_descr); final TextView workDescr = view.findViewById(R.id.work_button_descr);