From b88daa60acba33bd79dc732e6537ee037fd5d0d9 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 25 Apr 2018 14:43:52 +0300 Subject: [PATCH] add asynkTask for popular destinations --- .../wikivoyage/explore/ExploreRvAdapter.java | 1 + .../explore/ExploreTabFragment.java | 77 ++++++++++++++++--- .../travelcards/ArticleTravelCard.java | 7 ++ 3 files changed, 75 insertions(+), 10 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java index 16f41fb6e8..6392aa48c3 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java @@ -63,6 +63,7 @@ public class ExploreRvAdapter extends RecyclerView.Adapter items = new ArrayList<>(); final OsmandApplication app = getMyApplication(); final boolean nightMode = !getSettings().isLightContent(); - + startEditingTravelCard = new StartEditingTravelCard(app, nightMode); items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager())); - items.add(new StartEditingTravelCard(app, nightMode)); - addPopularDestinations(items, nightMode); + items.add(startEditingTravelCard); + addPopularDestinations(app, nightMode); return items; } - private void addPopularDestinations(@NonNull List items, boolean nightMode) { - OsmandApplication app = getMyApplication(); - List savedArticles = app.getTravelDbHelper().searchPopular(); - if (!savedArticles.isEmpty()) { - items.add(getString(R.string.popular_destinations)); - for (TravelArticle article : savedArticles) { - items.add(new ArticleTravelCard(app, nightMode, article, getFragmentManager())); + private void addPopularDestinations(OsmandApplication app, boolean nightMode) { + popularDestinationsSearchTask = new PopularDestinationsSearchTask(app.getTravelDbHelper(), getMyActivity(), adapter, nightMode, startEditingTravelCard); + popularDestinationsSearchTask.execute(); + } + + private static class PopularDestinationsSearchTask extends AsyncTask> { + + private TravelDbHelper travelDbHelper; + private WeakReference weakContext; + private WeakReference weakAdapter; + private WeakReference weakStartEditingTravelCard; + private boolean nightMode; + + PopularDestinationsSearchTask(TravelDbHelper travelDbHelper, + OsmandActionBarActivity context, ExploreRvAdapter adapter, boolean nightMode, StartEditingTravelCard startEditingTravelCard) { + this.travelDbHelper = travelDbHelper; + weakContext = new WeakReference<>(context); + weakAdapter = new WeakReference<>(adapter); + weakStartEditingTravelCard = new WeakReference<>(startEditingTravelCard); + this.nightMode = nightMode; + } + + @Override + protected List doInBackground(ExploreRvAdapter... exploreRvAdapters) { + List articles = travelDbHelper.searchPopular(); + List items = new ArrayList<>(); + if (!articles.isEmpty()) { + OsmandActionBarActivity activity = weakContext.get(); + if (activity != null) { + for (TravelArticle article : articles) { + items.add(new ArticleTravelCard(activity.getMyApplication(), nightMode, article, activity.getSupportFragmentManager())); + } + } } + return items; + } + + @Override + protected void onPostExecute(List items) { + OsmandActionBarActivity activity = weakContext.get(); + ExploreRvAdapter adapter = weakAdapter.get(); + + List adapterItems = adapter.getItems(); + StartEditingTravelCard startEditingTravelCard = weakStartEditingTravelCard.get(); + + adapterItems.remove(startEditingTravelCard); + adapterItems.add(activity.getResources().getString(R.string.popular_destinations)); + adapterItems.addAll(items); + adapterItems.add(startEditingTravelCard); + + adapter.notifyDataSetChanged(); + } + } + + @Override + public void onPause() { + super.onPause(); + if (popularDestinationsSearchTask != null && popularDestinationsSearchTask.getStatus() == AsyncTask.Status.RUNNING) { + popularDestinationsSearchTask.cancel(false); } } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/ArticleTravelCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/ArticleTravelCard.java index eb80af9b00..5c4649c0a5 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/ArticleTravelCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/ArticleTravelCard.java @@ -30,6 +30,7 @@ public class ArticleTravelCard extends BaseTravelCard { private TravelArticle article; private final Drawable readIcon; private FragmentManager fragmentManager; + private boolean isLastItem; public ArticleTravelCard(OsmandApplication app, boolean nightMode, TravelArticle article, FragmentManager fragmentManager) { super(app, nightMode); @@ -73,6 +74,8 @@ public class ArticleTravelCard extends BaseTravelCard { holder.itemView.setOnClickListener(readClickListener); holder.leftButton.setCompoundDrawablesWithIntrinsicBounds(readIcon, null, null, null); updateSaveButton(holder); + holder.divider.setVisibility(isLastItem ? View.GONE : View.VISIBLE); + holder.shadow.setVisibility(isLastItem ? View.VISIBLE : View.GONE); } } @@ -124,6 +127,10 @@ public class ArticleTravelCard extends BaseTravelCard { } } + public void setLastItem(boolean lastItem) { + isLastItem = lastItem; + } + @Override public int getCardType() { return TYPE;