Favorites duplication with same name

This commit is contained in:
androiddevkkotlin 2020-10-19 22:15:00 +03:00
parent 2af22671a9
commit e21066eefe

View file

@ -34,6 +34,7 @@ class FavoritesImportTask extends BaseImportAsyncTask<Void, Void, GPXFile> {
protected GPXFile doInBackground(Void... nothing) { protected GPXFile doInBackground(Void... nothing) {
List<FavouritePoint> favourites = asFavourites(app, gpxFile.getPoints(), fileName, forceImportFavourites); List<FavouritePoint> favourites = asFavourites(app, gpxFile.getPoints(), fileName, forceImportFavourites);
FavouritesDbHelper favoritesHelper = app.getFavorites(); FavouritesDbHelper favoritesHelper = app.getFavorites();
checkDuplicateNames(favourites);
for (FavouritePoint favourite : favourites) { for (FavouritePoint favourite : favourites) {
favoritesHelper.deleteFavourite(favourite, false); favoritesHelper.deleteFavourite(favourite, false);
favoritesHelper.addFavourite(favourite, false); favoritesHelper.addFavourite(favourite, false);
@ -43,6 +44,27 @@ class FavoritesImportTask extends BaseImportAsyncTask<Void, Void, GPXFile> {
return null; return null;
} }
public void checkDuplicateNames(List<FavouritePoint> favourites) {
for (FavouritePoint fp : favourites) {
int number = 1;
String index;
String name = fp.getName();
boolean duplicatesFound = false;
for (FavouritePoint fp2 : favourites) {
if (name.equals(fp2.getName()) && fp.getCategory().equals(fp2.getCategory()) && !fp.equals(fp2)) {
if (!duplicatesFound) {
index = " (" + number + ")";
fp.setName(name + index);
}
duplicatesFound = true;
number++;
index = " (" + number + ")";
fp2.setName(fp2.getName() + index);
}
}
}
}
@Override @Override
protected void onPostExecute(GPXFile result) { protected void onPostExecute(GPXFile result) {
hideProgress(); hideProgress();