From 642f0aeafa19980da5bbff4c67da42eb8b85baad Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 29 Mar 2018 13:46:52 +0300 Subject: [PATCH] Add small fixes to wikivoyage search logic --- .../WikivoyageSearchDialogFragment.java | 18 ++++++++--- .../search/WikivoyageSearchHelper.java | 32 ++++++------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java index a15b538ee4..894aefc742 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java @@ -131,18 +131,26 @@ public class WikivoyageSearchDialogFragment extends BaseOsmAndDialogFragment imp public void onPause() { super.onPause(); paused = true; - searchHelper.unregisterListener(this); + searchHelper.unregisterListener(); } @Override public void onSearchStarted() { - switchProgressBarVisibility(true); + getMyApplication().runInUIThread(new Runnable() { + public void run() { + switchProgressBarVisibility(true); + } + }); } @Override - public void onSearchFinished(@Nullable List results) { - adapter.setItems(results); - switchProgressBarVisibility(false); + public void onSearchFinished(@Nullable final List results) { + getMyApplication().runInUIThread(new Runnable() { + public void run() { + adapter.setItems(results); + switchProgressBarVisibility(false); + } + }); } private void cancelSearch() { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java index 7616b6686b..6bb805f9bf 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchHelper.java @@ -6,9 +6,7 @@ import net.osmand.ResultMatcher; import net.osmand.plus.OsmandApplication; import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult; -import java.util.HashSet; import java.util.List; -import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -20,18 +18,18 @@ public class WikivoyageSearchHelper { private ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); private OsmandApplication application; - private Set listeners = new HashSet<>(); + private SearchListener listener; WikivoyageSearchHelper(OsmandApplication application) { this.application = application; } public void registerListener(SearchListener listener) { - listeners.add(listener); + this.listener = listener; } - public void unregisterListener(SearchListener listener) { - listeners.remove(listener); + public void unregisterListener() { + this.listener = null; } public void search(final String query, final ResultMatcher rm) { @@ -39,14 +37,9 @@ public class WikivoyageSearchHelper { @Override public void run() { try { - application.runInUIThread(new Runnable() { - @Override - public void run() { - for (SearchListener listener : listeners) { - listener.onSearchStarted(); - } - } - }); + if (listener != null) { + listener.onSearchStarted(); + } rm.publish(null); @@ -60,14 +53,9 @@ public class WikivoyageSearchHelper { final List results = application.getWikivoyageDbHelper().search(query); - application.runInUIThread(new Runnable() { - @Override - public void run() { - for (SearchListener listener : listeners) { - listener.onSearchFinished(results); - } - } - }); + if (listener != null) { + listener.onSearchFinished(results); + } } catch (InterruptedException e) { e.printStackTrace(); }