From 9c8aa3300c62547ce31b512e9caf7d251ab59a69 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 9 Jan 2015 16:53:08 +0200 Subject: [PATCH] Added favourites updated event --- .../net/osmand/plus/FavouritesDbHelper.java | 21 +++++++++++++++++++ .../plus/activities/MainMenuActivity.java | 5 ----- .../plus/dashboard/DashFavoritesFragment.java | 14 +++++++++++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java index 92b98c0f24..3c8d580894 100644 --- a/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java +++ b/OsmAnd/src/net/osmand/plus/FavouritesDbHelper.java @@ -30,6 +30,12 @@ import net.osmand.plus.api.SQLiteAPI.SQLiteCursor; public class FavouritesDbHelper { + public interface FavoritesUpdatedListener { + void updateFavourites(); + } + + private List favoritesUpdatedListeners = new ArrayList(); + private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(FavouritesDbHelper.class); public static final String FILE_TO_SAVE = "favourites.gpx"; //$NON-NLS-1$ @@ -82,10 +88,25 @@ public class FavouritesDbHelper { if(changed) { saveCurrentPointsIntoFile(); } + favouritesUpdated(); } + private void favouritesUpdated(){ + for (FavoritesUpdatedListener listener : favoritesUpdatedListeners){ + listener.updateFavourites(); + } + } + public void addFavoritesUpdatedListener(FavoritesUpdatedListener listener){ + if (!favoritesUpdatedListeners.contains(listener)){ + favoritesUpdatedListeners.add(listener); + } + } + + public void removeFavoritesUpdatedListener(FavoritesUpdatedListener listener){ + favoritesUpdatedListeners.remove(listener); + } private boolean merge(Map source, Map destination) { boolean changed = false; diff --git a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java index 6c1e318d5e..ab4b9dc5cf 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MainMenuActivity.java @@ -491,11 +491,6 @@ public class MainMenuActivity extends BaseDownloadActivity implements OsmAndLoca if (f instanceof DashDownloadMapsFragment && !f.isDetached()) { ((DashDownloadMapsFragment) f).refreshData(); } - //Needed to reliably initialize DashFavoritesFragement on devices without compass - // [Victor]: This doesn't look as a proper place, then it highly depends on internet connection and download list - if (f instanceof DashFavoritesFragment && !f.isDetached()) { - ((DashFavoritesFragment) f).setupFavorites(); - } } } diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java index dd490b8a63..2268e64816 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java @@ -38,7 +38,7 @@ import android.widget.TextView; /** * Created by Denis on 24.11.2014. */ -public class DashFavoritesFragment extends DashBaseFragment { +public class DashFavoritesFragment extends DashBaseFragment implements FavouritesDbHelper.FavoritesUpdatedListener { public static final String TAG = "DASH_FAVORITES_FRAGMENT"; private net.osmand.Location location = null; private LatLon loc = null; @@ -92,10 +92,16 @@ public class DashFavoritesFragment extends DashBaseFragment { } else { loc = new LatLon(0f, 0f); } + + getMyApplication().getFavorites().addFavoritesUpdatedListener(this); setupFavorites(); } - + @Override + public void onPause() { + super.onPause(); + getMyApplication().getFavorites().removeFavoritesUpdatedListener(this); + } public void setupFavorites(){ View mainView = getView(); @@ -250,4 +256,8 @@ public class DashFavoritesFragment extends DashBaseFragment { updateArrows(); } + @Override + public void updateFavourites() { + setupFavorites(); + } }