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/
armeabi-v7a/
mips/
arm64-v8a/
arm64-v8a/
x86_64/

View file

@ -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 : "";

View file

@ -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<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) {
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<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
public FavouritePoint getVisibleFavByLatLon(@NonNull LatLon latLon) {

View file

@ -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();
}

View file

@ -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());
}
}

View file

@ -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;

View file

@ -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);