Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix favorites saving lags #17746

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void copyToFavorites() {
if (plugin != null && point.getSpecialPointType() == SpecialPointType.PARKING) {
plugin.updateParkingPoint(point);
}
favouritesHelper.addFavourite(point, false);
favouritesHelper.addFavourite(point, true, false, false);
}
}
favouritesHelper.saveCurrentPointsIntoFile(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private void mergeFavorites() {

for (FavouritePoint favourite : favourites) {
favoritesHelper.deleteFavourite(favourite, false);
favoritesHelper.addFavourite(favourite, false, false);
favoritesHelper.addFavourite(favourite, false, false, false);

if (plugin != null && favourite.getSpecialPointType() == SpecialPointType.PARKING) {
plugin.updateParkingPoint(favourite);
Expand Down
45 changes: 22 additions & 23 deletions OsmAnd/src/net/osmand/plus/myplaces/favorites/FavouritesHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,11 @@ public void setSpecialPoint(@NonNull LatLon latLon, SpecialPointType specialType
}
}

public boolean addFavourite(FavouritePoint p) {
return addFavourite(p, true);
public boolean addFavourite(@NonNull FavouritePoint point) {
return addFavourite(point, true, true, true);
}

public boolean addFavourite(FavouritePoint p, boolean saveImmediately) {
return addFavourite(p, saveImmediately, true);
}

public boolean addFavourite(FavouritePoint point, boolean saveImmediately, boolean lookupAddress) {
public boolean addFavourite(@NonNull FavouritePoint point, boolean lookupAddress, boolean sortAndSave, boolean saveAsync) {
if (Double.isNaN(point.getAltitude()) || point.getAltitude() == 0) {
initAltitude(point);
}
Expand All @@ -366,23 +362,21 @@ public boolean addFavourite(FavouritePoint point, boolean saveImmediately, boole
lookupAddress(point);
}
app.getSettings().SHOW_FAVORITES.set(true);
FavoriteGroup group = getOrCreateGroup(point);

FavoriteGroup group = getOrCreateGroup(point);
if (!point.getName().isEmpty()) {
point.setVisible(group.isVisible());
if (SpecialPointType.PARKING == point.getSpecialPointType()) {
point.setColor(ContextCompat.getColor(app, R.color.parking_icon_background));
} else {
if (point.getColor() == 0) {
point.setColor(group.getColor());
}
} else if (point.getColor() == 0) {
point.setColor(group.getColor());
}
group.getPoints().add(point);
addFavouritePoint(point);
}
if (saveImmediately) {
if (sortAndSave) {
sortAll();
saveCurrentPointsIntoFile(false);
saveCurrentPointsIntoFile(saveAsync);
}

runSyncWithMarkers(group);
Expand Down Expand Up @@ -483,10 +477,9 @@ private boolean editFavourite(@NonNull FavouritePoint point, double lat, double
}

public void saveCurrentPointsIntoFile(boolean async) {
SaveFavoritesListener listener = () -> {
updateLastModifiedTime();
onFavouritePropertiesUpdated();
};
updateLastModifiedTime();
SaveFavoritesListener listener = this::onFavouritePropertiesUpdated;

List<FavoriteGroup> groups = new ArrayList<>(favoriteGroups);
if (async) {
fileHelper.saveFavoritesIntoFile(groups, listener);
Expand Down Expand Up @@ -635,19 +628,25 @@ public void recalculateCachedFavPoints() {
}

public void sortAll() {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
Collator collator = getCollator();
Collections.sort(favoriteGroups, (lhs, rhs) -> lhs.isPersonal() ? -1 : rhs.isPersonal() ? 1 : collator.compare(lhs.getName(), rhs.getName()));
Comparator<FavouritePoint> favoritesComparator = getComparator();
for (FavoriteGroup g : favoriteGroups) {
Collections.sort(g.getPoints(), favoritesComparator);
for (FavoriteGroup group : favoriteGroups) {
Collections.sort(group.getPoints(), favoritesComparator);
}
Collections.sort(cachedFavoritePoints, favoritesComparator);
}

public static Comparator<FavouritePoint> getComparator() {
@NonNull
private static Collator getCollator() {
Collator collator = Collator.getInstance();
collator.setStrength(Collator.SECONDARY);
return collator;
}

@NonNull
private static Comparator<FavouritePoint> getComparator() {
Collator collator = getCollator();
return (o1, o2) -> {
String s1 = o1.getName();
String s2 = o2.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void apply() {
}
List<FavouritePoint> favourites = FavouritesHelper.getPointsFromGroups(appliedItems);
for (FavouritePoint favourite : favourites) {
favoritesHelper.addFavourite(favourite, false, false);
favoritesHelper.addFavourite(favourite, false, false, false);
}
favoritesHelper.sortAll();
favoritesHelper.saveCurrentPointsIntoFile(false);
Expand Down