From 9c8aa3300c62547ce31b512e9caf7d251ab59a69 Mon Sep 17 00:00:00 2001 From: Denis Date: Fri, 9 Jan 2015 16:53:08 +0200 Subject: [PATCH 1/3] 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(); + } } From fe65fc3ce8f1e08fe6aab481aa65c52c2ff29616 Mon Sep 17 00:00:00 2001 From: Denis Date: Sat, 10 Jan 2015 12:25:16 +0200 Subject: [PATCH 2/3] Added run on ui thread --- .../osmand/plus/dashboard/DashFavoritesFragment.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java index 2268e64816..ab22ff2282 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java @@ -258,6 +258,14 @@ public class DashFavoritesFragment extends DashBaseFragment implements Favourite @Override public void updateFavourites() { - setupFavorites(); + if (getActivity() == null) { + return; + } + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + setupFavorites(); + } + }); } } From d3d4da2e0c7472978a29858c9c7e3852b632c820 Mon Sep 17 00:00:00 2001 From: Denis Date: Mon, 12 Jan 2015 13:05:36 +0200 Subject: [PATCH 3/3] Removed bad code --- .../src/net/osmand/plus/dashboard/DashFavoritesFragment.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java index ab22ff2282..1ab66b2c07 100644 --- a/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java +++ b/OsmAnd/src/net/osmand/plus/dashboard/DashFavoritesFragment.java @@ -258,9 +258,6 @@ public class DashFavoritesFragment extends DashBaseFragment implements Favourite @Override public void updateFavourites() { - if (getActivity() == null) { - return; - } getActivity().runOnUiThread(new Runnable() { @Override public void run() {