Skip to content

Commit

Permalink
Fix "Lost favorites if one is saved before all are loaded #20957"
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-kutz committed Oct 4, 2024
1 parent 39c703b commit 3380057
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nu
@Override
public void onOpenDash() {
FavouritesHelper helper = getMyApplication().getFavoritesHelper();
if (helper.isFavoritesLoaded()) {
setupFavorites();
} else {
setupFavorites();
if (!helper.isFavoritesLoaded()) {
helper.addListener(favoritesListener = new FavoritesListener() {
@Override
public void onFavoritesLoaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public void createMenuItems(@Nullable Bundle savedInstanceState) {
}
mAdapter = new FavouritesAdapter(requireContext(), mPoints);
mFavouritesHelper = app.getFavoritesHelper();
if (mFavouritesHelper.isFavoritesLoaded()) {
loadFavorites();
} else {
loadFavorites();
if (!mFavouritesHelper.isFavoritesLoaded()) {
mFavouritesHelper.addListener(getFavouritesListener());
}
rvPoints = new RecyclerView(getContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ public void loadFavorites() {

boolean changed = merge(extGroups, groups);

// Fix issue #20957
// Prevent the loss of favorites and groups added when loading favorites from file
if (!Algorithms.isEmpty(flatGroups)) {
merge(flatGroups, groups);
changed = true;
}

flatGroups = groups;
favoriteGroups = new ArrayList<>(groups.values());

Expand All @@ -131,7 +138,7 @@ public void loadFavorites() {
// Force save favorites to file if internals are different from externals
// or no favorites created yet or legacy favourites.gpx present
if (changed || !fileHelper.getExternalDir().exists() || legacyExternalFile.exists()) {
saveCurrentPointsIntoFile(false);
forceSaveCurrentPointsIntoFile();
// Delete legacy favourites.gpx if exists
if (legacyExternalFile.exists()) {
legacyExternalFile.delete();
Expand Down Expand Up @@ -207,7 +214,8 @@ public void removeListener(@NonNull FavoritesListener listener) {
listeners.remove(listener);
}

private boolean merge(Map<String, FavoriteGroup> source, Map<String, FavoriteGroup> destination) {
private boolean merge(@NonNull Map<String, FavoriteGroup> source,
@NonNull Map<String, FavoriteGroup> destination) {
boolean changed = false;
for (Map.Entry<String, FavoriteGroup> entry : source.entrySet()) {
String key = entry.getKey();
Expand Down Expand Up @@ -481,10 +489,19 @@ private boolean editFavourite(@NonNull FavouritePoint point, double lat, double
}

public void saveCurrentPointsIntoFile(boolean async) {
saveCurrentPointsIntoFileImpl(false, async);
}

private void forceSaveCurrentPointsIntoFile() {
saveCurrentPointsIntoFileImpl(true, false);
}

private void saveCurrentPointsIntoFileImpl(boolean force, boolean async) {
updateLastModifiedTime();
SaveFavoritesListener listener = this::onFavouritePropertiesUpdated;
if (!force && !isFavoritesLoaded()) return;

List<FavoriteGroup> groups = new ArrayList<>(favoriteGroups);
SaveFavoritesListener listener = this::onFavouritePropertiesUpdated;
if (async) {
fileHelper.saveFavoritesIntoFile(groups, listener);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new FavouritesAdapter(requireActivity());
importHelper = app.getImportHelper();

helper = app.getFavoritesHelper();
if (helper.isFavoritesLoaded()) {
adapter.synchronizeGroups();
} else {

adapter.synchronizeGroups();
if (!helper.isFavoritesLoaded()) {
helper.addListener(favoritesListener = new FavoritesListener() {
@Override
public void onFavoritesLoaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,8 @@ private void createFavoritesScrollItem() {
FavoritesItemsAdapter adapter = new FavoritesItemsAdapter(app, items);
adapter.setItemClickListener(getAdapterOnClickListener(items));
FavouritesHelper helper = app.getFavoritesHelper();
if (helper.isFavoritesLoaded()) {
loadFavoritesItems(items, helper);
} else {
loadFavoritesItems(items, helper);
if (!helper.isFavoritesLoaded()) {
addMainScrollItems(items);
helper.addListener(new FavoritesListener() {

Expand Down
4 changes: 2 additions & 2 deletions OsmAnd/src/net/osmand/plus/views/layers/FavouritesLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void onPrepareBufferImage(Canvas canvas, RotatedTileBox tileBox, DrawSett
}
} else {
cache.clear();
if (showFavorites && favouritesHelper.isFavoritesLoaded()) {
if (showFavorites) {
if (tileBox.getZoom() >= START_ZOOM || customObjectsDelegate != null) {
float iconSize = getIconSize(view.getApplication());
QuadTree<QuadRect> boundIntersections = initBoundIntersections(tileBox);
Expand Down Expand Up @@ -265,7 +265,7 @@ public synchronized void showFavorites() {
List<FavouritePoint> points = customObjectsDelegate.getMapObjects();
showFavoritePoints(textScale, false, points);
favoritesMapLayerProvider.drawSymbols(mapRenderer);
} else if (settings.SHOW_FAVORITES.get() && favouritesHelper.isFavoritesLoaded()) {
} else if (settings.SHOW_FAVORITES.get()) {
for (FavoriteGroup group : getFavoriteGroups()) {
boolean synced = isSynced(group);
List<FavouritePoint> points = new ArrayList<>(group.getPoints());
Expand Down

0 comments on commit 3380057

Please sign in to comment.