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 "Lost favorites if one is saved before all are loaded #20957" #20977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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
Loading