Merge pull request #5279 from osmandapp/WikivoyageExploreCards

add asynkTask for popular destinations
This commit is contained in:
vshcherb 2018-04-25 18:48:23 +02:00 committed by GitHub
commit da19a1f18c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 13 deletions

View file

@ -6,6 +6,13 @@
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"

View file

@ -8,8 +8,10 @@ import net.osmand.Collator;
import net.osmand.CollatorStringMatcher;
import net.osmand.CollatorStringMatcher.StringMatcherMode;
import net.osmand.IndexConstants;
import net.osmand.Location;
import net.osmand.OsmAndCollator;
import net.osmand.PlatformUtil;
import net.osmand.data.LatLon;
import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.OsmandApplication;
@ -17,6 +19,7 @@ import net.osmand.plus.R;
import net.osmand.plus.api.SQLiteAPI.SQLiteConnection;
import net.osmand.plus.api.SQLiteAPI.SQLiteCursor;
import net.osmand.util.Algorithms;
import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
@ -205,7 +208,11 @@ public class TravelDbHelper {
SQLiteConnection conn = openConnection();
if (conn != null) {
TravelArticle travelArticle;
SQLiteCursor cursor = conn.rawQuery("SELECT * FROM " + ARTICLES_TABLE_NAME + " ORDER BY RANDOM() LIMIT 100", null);
SQLiteCursor cursor = conn.rawQuery("SELECT * FROM "
+ ARTICLES_TABLE_NAME
+ " WHERE title IN (SELECT title FROM "
+ ARTICLES_TABLE_NAME
+ " ORDER BY RANDOM() LIMIT 100) LIMIT 100", null);
if (cursor.moveToFirst()) {
do {
travelArticle = readArticle(cursor);

View file

@ -68,6 +68,10 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
holder.description.setText(String.valueOf(getArticleItemCount()));
} else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) {
((ArticleTravelCard) item).bindViewHolder(viewHolder);
ArticleTravelCard articleTravelCard = (ArticleTravelCard) item;
articleTravelCard.setLastItem(position == getLastArticleItemIndex());
articleTravelCard.bindViewHolder(viewHolder);
((ArticleTravelCard) item).bindViewHolder(viewHolder);
} else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) {
((OpenBetaTravelCard) item).bindViewHolder(viewHolder);
} else if (viewHolder instanceof StartEditingTravelVH && item instanceof StartEditingTravelCard) {
@ -109,6 +113,16 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
return count;
}
private int getLastArticleItemIndex() {
for (int i = items.size() - 1; i > 0; i--) {
Object o = items.get(i);
if (o instanceof ArticleTravelCard) {
return i;
}
}
return 0;
}
private Object getItem(int position) {
return items.get(position);
}

View file

@ -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<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, progressBar);
popularDestinationsSearchTask.execute();
}
private static class CheckWorldWikivoyageTask extends AsyncTask<Void, Void, Boolean> {
@ -123,4 +126,58 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
void onCheckFinished(boolean worldWikivoyageDownloaded);
}
}
}
private static class PopularDestinationsSearchTask extends AsyncTask<Void, TravelDbHelper, List<TravelArticle>> {
private TravelDbHelper travelDbHelper;
private WeakReference<OsmandActionBarActivity> weakContext;
private WeakReference<ExploreRvAdapter> weakAdapter;
private WeakReference<StartEditingTravelCard> weakStartEditingTravelCard;
private WeakReference<View> 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<TravelArticle> doInBackground(Void... voids) {
return travelDbHelper.searchPopular();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
weakProgressBar.get().setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(List<TravelArticle> items) {
OsmandActionBarActivity activity = weakContext.get();
ExploreRvAdapter adapter = weakAdapter.get();
List<Object> 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();
}
}
}

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;