From 6d77381ee0576513a779e607b4ed90e07dffea2b Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 6 Jan 2020 18:42:24 +0100 Subject: [PATCH] Refactor favorite points --- OsmAnd/libs/.gitignore | 3 +- .../src/net/osmand/data/FavouritePoint.java | 80 ++++++++- .../net/osmand/plus/FavouritesDbHelper.java | 160 ++---------------- .../osmand/plus/activities/MapActivity.java | 3 +- .../FavouritesBottomSheetMenuFragment.java | 4 +- .../AddPointBottomSheetDialog.java | 7 +- .../cards/HomeWorkCard.java | 4 +- 7 files changed, 101 insertions(+), 160 deletions(-) diff --git a/OsmAnd/libs/.gitignore b/OsmAnd/libs/.gitignore index 0ebe3445ff..48b65a02a5 100644 --- a/OsmAnd/libs/.gitignore +++ b/OsmAnd/libs/.gitignore @@ -2,4 +2,5 @@ armeabi/ x86/ armeabi-v7a/ mips/ -arm64-v8a/ \ No newline at end of file +arm64-v8a/ +x86_64/ diff --git a/OsmAnd/src/net/osmand/data/FavouritePoint.java b/OsmAnd/src/net/osmand/data/FavouritePoint.java index ab7b47d072..c2e7624267 100644 --- a/OsmAnd/src/net/osmand/data/FavouritePoint.java +++ b/OsmAnd/src/net/osmand/data/FavouritePoint.java @@ -3,7 +3,9 @@ package net.osmand.data; import java.io.Serializable; import android.content.Context; +import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; +import android.support.annotation.StringRes; import net.osmand.GPXUtilities.WptPt; import net.osmand.plus.FavouritesDbHelper; @@ -11,12 +13,17 @@ import net.osmand.plus.R; import net.osmand.util.Algorithms; 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 { private static final long serialVersionUID = 729654300829771466L; private static final String HIDDEN = "hidden"; + + protected String name = ""; protected String description; protected String category = ""; @@ -26,8 +33,9 @@ public class FavouritePoint implements Serializable, LocationPoint { private double longitude; private int color; private boolean visible = true; + private SpecialPointType specialPointType = null; - public FavouritePoint(){ + public FavouritePoint() { } public FavouritePoint(double latitude, double longitude, String name, String category) { @@ -38,6 +46,7 @@ public class FavouritePoint implements Serializable, LocationPoint { name = ""; } this.name = name; + initPersonalType(); } public FavouritePoint(FavouritePoint favouritePoint) { @@ -49,6 +58,21 @@ public class FavouritePoint implements Serializable, LocationPoint { this.description = favouritePoint.description; this.visible = favouritePoint.visible; 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() { @@ -72,10 +96,7 @@ public class FavouritePoint implements Serializable, LocationPoint { } public boolean isPersonalPoint() { - // TODO: HW - return name.equals(FavouritesDbHelper.getHomePointName()) - || name.equals(FavouritesDbHelper.getWorkPointName()) - || name.equals(FavouritesDbHelper.getParkingPointName()); + return specialPointType != null; } @Override @@ -141,11 +162,12 @@ public class FavouritePoint implements Serializable, LocationPoint { public void setCategory(String category) { this.category = category; + initPersonalType(); } public String getDisplayName(@NonNull Context ctx) { - if (isPersonalPoint()) { - return FavouritesDbHelper.getPersonalName(this, ctx); + if (specialPointType != null) { + return specialPointType.getHumanString(ctx); } return name; } @@ -156,6 +178,7 @@ public class FavouritePoint implements Serializable, LocationPoint { public void setName(String name) { this.name = name; + initPersonalType(); } public String getDescription () { @@ -223,6 +246,49 @@ public class FavouritePoint implements Serializable, LocationPoint { 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) { String name = pt.name; String categoryName = pt.category != null ? pt.category : ""; diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 26205518db..026bf5c889 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -1,10 +1,8 @@ package net.osmand.plus; import android.content.Context; -import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.support.annotation.StringRes; import android.support.v7.app.AlertDialog; import net.osmand.GPXUtilities; @@ -67,45 +65,6 @@ public class FavouritesDbHelper { private Map 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) { 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() { flatGroups.clear(); favoriteGroups.clear(); @@ -207,25 +146,9 @@ public class FavouritesDbHelper { }); } - public FavouritePoint getWorkPoint() { - 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) { + public FavouritePoint getSpecialPoint(PersonalPointType pointType) { for (FavouritePoint fp : cachedFavoritePoints) { - if ((PersonalPointType.valueOfTypeName(fp.getName())) == pointType) { + if (fp.getSpecialPointType() == pointType) { return fp; } } @@ -327,45 +250,20 @@ public class FavouritesDbHelper { return true; } - public void setHomePoint(@NonNull LatLon latLon, @Nullable String description) { - FavouritePoint homePoint = getHomePoint(); - if (homePoint != null) { - editFavourite(homePoint, latLon.getLatitude(), latLon.getLongitude(), description); + public void setSpecialPoint(@NonNull LatLon latLon, FavouritePoint.SpecialPointType personalType, @Nullable String address) { + FavouritePoint point = getSpecialPoint(personalType); + if (point != null) { + editFavourite(point, latLon.getLatitude(), latLon.getLongitude(), description); } else { - homePoint = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), HOME.getName(), PERSONAL_CATEGORY); - homePoint.setDescription(description); - addFavourite(homePoint); + point = new FavouritePoint(latLon.getLatitude(), latLon.getLongitude(), personalType, personalType.getCategory()); + point.setDescription(description); + addFavourite(point); } - if (description == null) { - lookupAddress(homePoint); + if (address == null) { + 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) { return addFavourite(p, true); } @@ -392,19 +290,12 @@ public class FavouritesDbHelper { return true; } - public void lookupAddressAllPersonalPoints() { + public void lookupAddressAllSpecialPoints() { if (!context.isApplicationInitializing()) { - FavouritePoint workPoint = getWorkPoint(); - if (workPoint != null) { - lookupAddress(workPoint); - } - FavouritePoint homePoint = getHomePoint(); - if (homePoint != null) { - lookupAddress(homePoint); - } - FavouritePoint parkingPoint = getParkingPoint(); - if (parkingPoint != null) { - lookupAddress(parkingPoint); + for(FavouritePoint p : getFavouritePoints()) { + if(p.getSpecialPointType() != null) { + lookupAddress(p); + } } } } @@ -725,25 +616,6 @@ public class FavouritesDbHelper { return fp; } - public List getNonPersonalVisibleFavouritePoints() { - List fp = new ArrayList<>(); - for (FavouritePoint p : getNonPersonalFavouritePoints()) { - if (p.isVisible()) { - fp.add(p); - } - } - return fp; - } - - public List getNonPersonalFavouritePoints() { - List fp = new ArrayList<>(); - for (FavouritePoint p : cachedFavoritePoints) { - if (!p.isPersonalPoint()) { - fp.add(p); - } - } - return fp; - } @Nullable public FavouritePoint getVisibleFavByLatLon(@NonNull LatLon latLon) { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 8e1e426a2f..14bcdb122a 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -422,11 +422,12 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven } if (event == InitEvents.MAPS_INITIALIZED) { // TODO investigate if this false cause any issues! + // !!! TODO HW it shouldn't be called every time start!!! mapView.refreshMap(false); if (dashboardOnMap != null) { dashboardOnMap.updateLocation(true, true, false); } - app.getFavorites().lookupAddressAllPersonalPoints(); + app.getFavorites().lookupAddressAllSpecialPoints(); app.getTargetPointsHelper().lookupAddessAll(); app.getMapMarkersHelper().lookupAddressAll(); } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java index 1a6f481f59..76148821a8 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/FavouritesBottomSheetMenuFragment.java @@ -131,9 +131,9 @@ public class FavouritesBottomSheetMenuFragment extends MenuBottomSheetDialogFrag private void loadFavorites() { favouritePoints.clear(); - favouritePoints.addAll(getMyApplication().getFavorites().getNonPersonalVisibleFavouritePoints()); + favouritePoints.addAll(getMyApplication().getFavorites().getVisibleFavouritePoints()); if (favouritePoints.isEmpty()) { - favouritePoints.addAll(getMyApplication().getFavorites().getNonPersonalFavouritePoints()); + favouritePoints.addAll(getMyApplication().getFavorites().getFavouritePoints()); } } diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java index 675159f6e6..513dd032da 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java @@ -480,11 +480,11 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment { FavouritesDbHelper favorites = mapActivity.getMyApplication().getFavorites(); FavouritePoint point = null; if (item == PointType.HOME) { - point = favorites.getHomePoint(); + point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME); } else if (item == PointType.WORK) { - point = favorites.getWorkPoint(); + point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK); } else if (item == PointType.PARKING) { - point = favorites.getParkingPoint(); + point = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.PARKING); } if (point != null) { ll = new LatLon(point.getLatitude(), point.getLongitude()); @@ -642,6 +642,7 @@ public class AddPointBottomSheetDialog extends MenuBottomSheetDialogFragment { if (item instanceof FavouritePoint) { FavouritePoint point = (FavouritePoint) item; favoriteViewHolder.title.setText(point.getDisplayName(app)); + // TODO HW: similar to overlay icon id if (((FavouritePoint) item).isPersonalPoint()) { int iconColor = app.getSettings().isLightContent() ? R.color.icon_color_default_light : R.color.icon_color_default_dark; diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/HomeWorkCard.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/HomeWorkCard.java index 91db2e0313..f89db9d4cf 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/HomeWorkCard.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/cards/HomeWorkCard.java @@ -27,8 +27,8 @@ public class HomeWorkCard extends BaseCard { protected void updateContent() { final TargetPointsHelper targetPointsHelper = mapActivity.getMyApplication().getTargetPointsHelper(); final FavouritesDbHelper favorites = getMyApplication().getFavorites(); - final FavouritePoint homePoint = favorites.getHomePoint(); - final FavouritePoint workPoint = favorites.getWorkPoint(); + final FavouritePoint homePoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.HOME); + final FavouritePoint workPoint = favorites.getSpecialPoint(FavouritePoint.SpecialPointType.WORK); TextView homeDescr = view.findViewById(R.id.home_button_descr); final TextView workDescr = view.findViewById(R.id.work_button_descr);