add asynkTask for popular destinations

This commit is contained in:
Chumva 2018-04-25 14:43:52 +03:00
parent f93b60705f
commit b88daa60ac
3 changed files with 75 additions and 10 deletions

View file

@ -63,6 +63,7 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
holder.description.setText(String.valueOf(getArticleItemCount()));
} else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) {
ArticleTravelCard articleTravelCard = (ArticleTravelCard) item;
articleTravelCard.setLastItem(position == getItemCount() - 1);
articleTravelCard.bindViewHolder(viewHolder);
} else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) {
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) item;

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage.explore;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -11,18 +12,23 @@ import android.view.ViewGroup;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.base.BaseOsmAndFragment;
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 java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
public class ExploreTabFragment extends BaseOsmAndFragment {
private ExploreRvAdapter adapter = new ExploreRvAdapter();
private PopularDestinationsSearchTask popularDestinationsSearchTask;
private StartEditingTravelCard startEditingTravelCard;
@Nullable
@Override
@ -42,22 +48,73 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
final List<Object> 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<Object> items, boolean nightMode) {
OsmandApplication app = getMyApplication();
List<TravelArticle> 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<ExploreRvAdapter, TravelDbHelper, List<Object>> {
private TravelDbHelper travelDbHelper;
private WeakReference<OsmandActionBarActivity> weakContext;
private WeakReference<ExploreRvAdapter> weakAdapter;
private WeakReference<StartEditingTravelCard> 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<Object> doInBackground(ExploreRvAdapter... exploreRvAdapters) {
List<TravelArticle> articles = travelDbHelper.searchPopular();
List<Object> 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<Object> items) {
OsmandActionBarActivity activity = weakContext.get();
ExploreRvAdapter adapter = weakAdapter.get();
List<Object> 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);
}
}
}

View file

@ -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;