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())); holder.description.setText(String.valueOf(getArticleItemCount()));
} else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) { } else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) {
ArticleTravelCard articleTravelCard = (ArticleTravelCard) item; ArticleTravelCard articleTravelCard = (ArticleTravelCard) item;
articleTravelCard.setLastItem(position == getItemCount() - 1);
articleTravelCard.bindViewHolder(viewHolder); articleTravelCard.bindViewHolder(viewHolder);
} else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) { } else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) {
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) item; OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) item;

View file

@ -1,5 +1,6 @@
package net.osmand.plus.wikivoyage.explore; package net.osmand.plus.wikivoyage.explore;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -11,18 +12,23 @@ import android.view.ViewGroup;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.OsmandActionBarActivity;
import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.wikivoyage.data.TravelArticle; 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.ArticleTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ExploreTabFragment extends BaseOsmAndFragment { public class ExploreTabFragment extends BaseOsmAndFragment {
private ExploreRvAdapter adapter = new ExploreRvAdapter(); private ExploreRvAdapter adapter = new ExploreRvAdapter();
private PopularDestinationsSearchTask popularDestinationsSearchTask;
private StartEditingTravelCard startEditingTravelCard;
@Nullable @Nullable
@Override @Override
@ -42,22 +48,73 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
final List<Object> items = new ArrayList<>(); final List<Object> items = new ArrayList<>();
final OsmandApplication app = getMyApplication(); final OsmandApplication app = getMyApplication();
final boolean nightMode = !getSettings().isLightContent(); final boolean nightMode = !getSettings().isLightContent();
startEditingTravelCard = new StartEditingTravelCard(app, nightMode);
items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager())); items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager()));
items.add(new StartEditingTravelCard(app, nightMode)); items.add(startEditingTravelCard);
addPopularDestinations(items, nightMode); addPopularDestinations(app, nightMode);
return items; return items;
} }
private void addPopularDestinations(@NonNull List<Object> items, boolean nightMode) { private void addPopularDestinations(OsmandApplication app, boolean nightMode) {
OsmandApplication app = getMyApplication(); popularDestinationsSearchTask = new PopularDestinationsSearchTask(app.getTravelDbHelper(), getMyActivity(), adapter, nightMode, startEditingTravelCard);
List<TravelArticle> savedArticles = app.getTravelDbHelper().searchPopular(); popularDestinationsSearchTask.execute();
if (!savedArticles.isEmpty()) { }
items.add(getString(R.string.popular_destinations));
for (TravelArticle article : savedArticles) { private static class PopularDestinationsSearchTask extends AsyncTask<ExploreRvAdapter, TravelDbHelper, List<Object>> {
items.add(new ArticleTravelCard(app, nightMode, article, getFragmentManager()));
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 TravelArticle article;
private final Drawable readIcon; private final Drawable readIcon;
private FragmentManager fragmentManager; private FragmentManager fragmentManager;
private boolean isLastItem;
public ArticleTravelCard(OsmandApplication app, boolean nightMode, TravelArticle article, FragmentManager fragmentManager) { public ArticleTravelCard(OsmandApplication app, boolean nightMode, TravelArticle article, FragmentManager fragmentManager) {
super(app, nightMode); super(app, nightMode);
@ -73,6 +74,8 @@ public class ArticleTravelCard extends BaseTravelCard {
holder.itemView.setOnClickListener(readClickListener); holder.itemView.setOnClickListener(readClickListener);
holder.leftButton.setCompoundDrawablesWithIntrinsicBounds(readIcon, null, null, null); holder.leftButton.setCompoundDrawablesWithIntrinsicBounds(readIcon, null, null, null);
updateSaveButton(holder); 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 @Override
public int getCardType() { public int getCardType() {
return TYPE; return TYPE;