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..1ab66b2c07 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,13 @@ public class DashFavoritesFragment extends DashBaseFragment { updateArrows(); } + @Override + public void updateFavourites() { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + setupFavorites(); + } + }); + } }