From 3380057be4c8f9ce1a2acda4d0a28672da6bbeaa Mon Sep 17 00:00:00 2001 From: nazar-kutz Date: Fri, 4 Oct 2024 13:55:07 +0300 Subject: [PATCH] Fix "Lost favorites if one is saved before all are loaded #20957" --- .../plus/dashboard/DashFavoritesFragment.java | 5 ++-- .../other/SelectFavouriteBottomSheet.java | 5 ++-- .../myplaces/favorites/FavouritesHelper.java | 23 ++++++++++++++++--- .../dialogs/FavoritesTreeFragment.java | 7 +++--- .../AddPointBottomSheetDialog.java | 5 ++-- .../plus/views/layers/FavouritesLayer.java | 4 ++-- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java index 6631d6eff98..560a2c31fe2 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java @@ -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() { diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/SelectFavouriteBottomSheet.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/SelectFavouriteBottomSheet.java index 777c428616c..3294921c5ce 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/SelectFavouriteBottomSheet.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/other/SelectFavouriteBottomSheet.java @@ -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()); diff --git a/OsmAnd/src/net/osmand/plus/myplaces/favorites/FavouritesHelper.java b/OsmAnd/src/net/osmand/plus/myplaces/favorites/FavouritesHelper.java index c96ac599002..37419dd57b0 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/favorites/FavouritesHelper.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/favorites/FavouritesHelper.java @@ -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()); @@ -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(); @@ -207,7 +214,8 @@ public void removeListener(@NonNull FavoritesListener listener) { listeners.remove(listener); } - private boolean merge(Map source, Map destination) { + private boolean merge(@NonNull Map source, + @NonNull Map destination) { boolean changed = false; for (Map.Entry entry : source.entrySet()) { String key = entry.getKey(); @@ -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 groups = new ArrayList<>(favoriteGroups); + SaveFavoritesListener listener = this::onFavouritePropertiesUpdated; if (async) { fileHelper.saveFavoritesIntoFile(groups, listener); } else { diff --git a/OsmAnd/src/net/osmand/plus/myplaces/favorites/dialogs/FavoritesTreeFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/favorites/dialogs/FavoritesTreeFragment.java index a9d9249409f..21cc073c370 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/favorites/dialogs/FavoritesTreeFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/favorites/dialogs/FavoritesTreeFragment.java @@ -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() { diff --git a/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java b/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java index 5a961186133..0402a28bfd7 100644 --- a/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java +++ b/OsmAnd/src/net/osmand/plus/routepreparationmenu/AddPointBottomSheetDialog.java @@ -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() { diff --git a/OsmAnd/src/net/osmand/plus/views/layers/FavouritesLayer.java b/OsmAnd/src/net/osmand/plus/views/layers/FavouritesLayer.java index 35b3e1ea139..4e2ce2ccecc 100644 --- a/OsmAnd/src/net/osmand/plus/views/layers/FavouritesLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/layers/FavouritesLayer.java @@ -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 boundIntersections = initBoundIntersections(tileBox); @@ -265,7 +265,7 @@ public synchronized void showFavorites() { List 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 points = new ArrayList<>(group.getPoints());