diff --git a/OsmAnd/res/layout/fragment_explore_tab.xml b/OsmAnd/res/layout/fragment_explore_tab.xml index 0d82ee83b9..97565b853f 100644 --- a/OsmAnd/res/layout/fragment_explore_tab.xml +++ b/OsmAnd/res/layout/fragment_explore_tab.xml @@ -6,6 +6,13 @@ android:gravity="center" android:orientation="vertical"> + + 0; i--) { + Object o = items.get(i); + if (o instanceof ArticleTravelCard) { + return i; + } + } + return 0; + } + private Object getItem(int position) { return items.get(position); } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index 4be8b7a8c3..7df07075b9 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -9,20 +9,24 @@ import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ProgressBar; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.LocalIndexHelper; import net.osmand.plus.activities.LocalIndexInfo; +import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.wikivoyage.data.TravelArticle; +import net.osmand.plus.wikivoyage.data.TravelDbHelper; import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard; 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; @@ -31,6 +35,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment { private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0; private ExploreRvAdapter adapter = new ExploreRvAdapter(); + private PopularDestinationsSearchTask popularDestinationsSearchTask; + private StartEditingTravelCard startEditingTravelCard; + private ProgressBar progressBar; @Nullable @Override @@ -40,6 +47,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment { adapter.setItems(generateItems()); final RecyclerView rv = (RecyclerView) mainView.findViewById(R.id.recycler_view); + progressBar = (ProgressBar) mainView.findViewById(R.id.progressBar); rv.setLayoutManager(new LinearLayoutManager(getContext())); rv.setAdapter(adapter); @@ -52,9 +60,10 @@ public class ExploreTabFragment extends BaseOsmAndFragment { final boolean nightMode = !getSettings().isLightContent(); addDownloadUpdateCard(nightMode); + 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; } @@ -74,15 +83,9 @@ public class ExploreTabFragment extends BaseOsmAndFragment { }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } - 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, progressBar); + popularDestinationsSearchTask.execute(); } private static class CheckWorldWikivoyageTask extends AsyncTask { @@ -123,4 +126,58 @@ public class ExploreTabFragment extends BaseOsmAndFragment { void onCheckFinished(boolean worldWikivoyageDownloaded); } } -} + + private static class PopularDestinationsSearchTask extends AsyncTask> { + + private TravelDbHelper travelDbHelper; + private WeakReference weakContext; + private WeakReference weakAdapter; + private WeakReference weakStartEditingTravelCard; + private WeakReference weakProgressBar; + private boolean nightMode; + + PopularDestinationsSearchTask(TravelDbHelper travelDbHelper, + OsmandActionBarActivity context, ExploreRvAdapter adapter, boolean nightMode, StartEditingTravelCard startEditingTravelCard, View progressBar) { + this.travelDbHelper = travelDbHelper; + weakContext = new WeakReference<>(context); + weakAdapter = new WeakReference<>(adapter); + weakStartEditingTravelCard = new WeakReference<>(startEditingTravelCard); + weakProgressBar = new WeakReference<>(progressBar); + this.nightMode = nightMode; + } + + @Override + protected List doInBackground(Void... voids) { + return travelDbHelper.searchPopular(); + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + weakProgressBar.get().setVisibility(View.VISIBLE); + } + + @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); + + if (!items.isEmpty()) { + if (activity != null) { + adapterItems.add(activity.getResources().getString(R.string.popular_destinations)); + for (TravelArticle article : items) { + adapterItems.add(new ArticleTravelCard(activity.getMyApplication(), nightMode, article, activity.getSupportFragmentManager())); + } + } + } + weakProgressBar.get().setVisibility(View.GONE); + adapterItems.add(startEditingTravelCard); + adapter.notifyDataSetChanged(); + } + } +} \ No newline at end of file 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;