From e17cf751a765ef3c6048aa074bc457b8cb546117 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 25 Apr 2018 19:07:44 +0300 Subject: [PATCH] Refactor ExploreTabFragment --- .../explore/ExploreTabFragment.java | 70 +++++++------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index 4a24ed3eed..4be8b7a8c3 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -23,7 +23,6 @@ import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; @@ -33,8 +32,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment { private ExploreRvAdapter adapter = new ExploreRvAdapter(); - private AddDownloadUpdateCardTask addDownloadUpdateCardTask; - @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -49,19 +46,6 @@ public class ExploreTabFragment extends BaseOsmAndFragment { return mainView; } - @Override - public void onDestroyView() { - cancelAddDownloadUpdateCardTask(); - super.onDestroyView(); - } - - private void cancelAddDownloadUpdateCardTask() { - if (addDownloadUpdateCardTask != null) { - addDownloadUpdateCardTask.cancel(true); - addDownloadUpdateCardTask = null; - } - } - private List generateItems() { final List items = new ArrayList<>(); final OsmandApplication app = getMyApplication(); @@ -75,9 +59,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment { return items; } - private void addDownloadUpdateCard(boolean nightMode) { - addDownloadUpdateCardTask = new AddDownloadUpdateCardTask(getMyApplication(), adapter, nightMode); - addDownloadUpdateCardTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + private void addDownloadUpdateCard(final boolean nightMode) { + final OsmandApplication app = getMyApplication(); + new CheckWorldWikivoyageTask(app, new CheckWorldWikivoyageTask.Callback() { + @Override + public void onCheckFinished(boolean worldWikivoyageDownloaded) { + if (!worldWikivoyageDownloaded && adapter != null) { + TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true); + if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) { + adapter.notifyDataSetChanged(); + } + } + } + }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } private void addPopularDestinations(@NonNull List items, boolean nightMode) { @@ -91,23 +85,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment { } } - private static class AddDownloadUpdateCardTask extends AsyncTask { + private static class CheckWorldWikivoyageTask extends AsyncTask { private OsmandApplication app; - private WeakReference adapterWr; + private Callback callback; - private boolean nightMode; - - AddDownloadUpdateCardTask(OsmandApplication app, ExploreRvAdapter adapter, boolean nightMode) { + CheckWorldWikivoyageTask(OsmandApplication app, Callback callback) { this.app = app; - this.adapterWr = new WeakReference<>(adapter); - this.nightMode = nightMode; + this.callback = callback; } @Override - protected TravelDownloadUpdateCard doInBackground(Void... voids) { + protected Boolean doInBackground(Void... voids) { final boolean[] worldWikivoyageDownloaded = new boolean[1]; - new LocalIndexHelper(app).getLocalTravelFiles(new AbstractLoadLocalIndexTask() { @Override public void loadFile(LocalIndexInfo... loaded) { @@ -118,25 +108,19 @@ public class ExploreTabFragment extends BaseOsmAndFragment { } } }); - - if (!worldWikivoyageDownloaded[0] && !isCancelled()) { - TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true); - return card; - } - - return null; + return worldWikivoyageDownloaded[0]; } @Override - protected void onPostExecute(TravelDownloadUpdateCard card) { - if (!isCancelled() && card != null) { - ExploreRvAdapter adapter = adapterWr.get(); - if (adapter != null) { - if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) { - adapter.notifyItemInserted(DOWNLOAD_UPDATE_CARD_POSITION); - } - } + protected void onPostExecute(Boolean worldWikivoyageDownloaded) { + if (callback != null) { + callback.onCheckFinished(worldWikivoyageDownloaded); } + callback = null; + } + + interface Callback { + void onCheckFinished(boolean worldWikivoyageDownloaded); } } }