Add download/update wikivoyage card

This commit is contained in:
Alex Sytnyk 2018-04-25 17:21:05 +03:00
parent 4fcf5cd34f
commit bed359e96c
6 changed files with 116 additions and 15 deletions

View file

@ -199,6 +199,12 @@ public class LocalIndexHelper {
return result;
}
public List<LocalIndexInfo> getLocalTravelFiles(AbstractLoadLocalIndexTask loadTask) {
List<LocalIndexInfo> result = new ArrayList<>();
loadTravelData(app.getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR), result, loadTask);
return result;
}
public List<LocalIndexInfo> getLocalFullMaps(AbstractLoadLocalIndexTask loadTask) {
Map<String, String> loadedMaps = app.getResourceManager().getIndexFileNames();
List<LocalIndexInfo> result = new ArrayList<>();

View file

@ -11,6 +11,8 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem;
import net.osmand.util.MapUtils;
import org.apache.commons.logging.Log;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
@ -23,8 +25,6 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
public class DownloadResources extends DownloadResourceGroup {
public boolean isDownloadedFromInternet = false;
public boolean downloadFromInternetFailed = false;
@ -39,6 +39,7 @@ public class DownloadResources extends DownloadResourceGroup {
public static final String WORLD_SEAMARKS_NAME = "World_seamarks";
public static final String WORLD_SEAMARKS_OLD_KEY = "world_seamarks_basemap";
public static final String WORLD_SEAMARKS_OLD_NAME = "World_seamarks_basemap";
public static final String WORLD_WIKIVOYAGE_NAME = "world_wikivoyage";
private static final Log LOG = PlatformUtil.getLog(DownloadResources.class);

View file

@ -220,7 +220,7 @@ public class FileNameTranslationHelper {
} else if (basename.equals(DownloadResources.WORLD_SEAMARKS_KEY) ||
basename.equals(DownloadResources.WORLD_SEAMARKS_OLD_KEY)) {
return ctx.getString(R.string.index_item_world_seamarks);
} else if (basename.equals("world_wikivoyage")) {
} else if (basename.equals(DownloadResources.WORLD_WIKIVOYAGE_NAME)) {
return ctx.getString(R.string.index_item_world_wikivoyage);
} else if (basename.equals("depth_contours_osmand_ext")) {
return ctx.getString(R.string.index_item_depth_contours_osmand_ext);

View file

@ -15,6 +15,8 @@ import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard.OpenBetaTravelVH;
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard;
import net.osmand.plus.wikivoyage.explore.travelcards.StartEditingTravelCard.StartEditingTravelVH;
import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard;
import net.osmand.plus.wikivoyage.explore.travelcards.TravelDownloadUpdateCard.DownloadUpdateVH;
import java.util.ArrayList;
import java.util.List;
@ -41,6 +43,9 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
: R.layout.wikivoyage_article_card;
return new ArticleTravelVH(inflate(parent, layoutId));
case TravelDownloadUpdateCard.TYPE:
return new DownloadUpdateVH(inflate(parent, R.layout.travel_download_update_card));
case HEADER_TYPE:
return new HeaderVH(inflate(parent, R.layout.wikivoyage_list_header));
@ -62,14 +67,13 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
holder.title.setText((String) item);
holder.description.setText(String.valueOf(getArticleItemCount()));
} else if (viewHolder instanceof ArticleTravelVH && item instanceof ArticleTravelCard) {
ArticleTravelCard articleTravelCard = (ArticleTravelCard) item;
articleTravelCard.bindViewHolder(viewHolder);
((ArticleTravelCard) item).bindViewHolder(viewHolder);
} else if (viewHolder instanceof OpenBetaTravelVH && item instanceof OpenBetaTravelCard) {
OpenBetaTravelCard openBetaTravelCard = (OpenBetaTravelCard) item;
openBetaTravelCard.bindViewHolder(viewHolder);
((OpenBetaTravelCard) item).bindViewHolder(viewHolder);
} else if (viewHolder instanceof StartEditingTravelVH && item instanceof StartEditingTravelCard) {
StartEditingTravelCard startEditingTravelCard = (StartEditingTravelCard) item;
startEditingTravelCard.bindViewHolder(viewHolder);
((StartEditingTravelCard) item).bindViewHolder(viewHolder);
} else if (viewHolder instanceof DownloadUpdateVH && item instanceof TravelDownloadUpdateCard) {
((TravelDownloadUpdateCard) item).bindViewHolder(viewHolder);
}
}
@ -84,6 +88,8 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
return ((StartEditingTravelCard) object).getCardType();
} else if (object instanceof ArticleTravelCard) {
return ((ArticleTravelCard) object).getCardType();
} else if (object instanceof TravelDownloadUpdateCard) {
return ((TravelDownloadUpdateCard) object).getCardType();
}
return -1;
}
@ -117,6 +123,14 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
this.items.addAll(items);
}
public boolean addItem(int position, Object item) {
if (position >= 0 && position <= items.size()) {
items.add(position, item);
return true;
}
return false;
}
static class HeaderVH extends RecyclerView.ViewHolder {
final TextView title;

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,19 +12,29 @@ import android.view.ViewGroup;
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.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.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;
public class ExploreTabFragment extends BaseOsmAndFragment {
private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0;
private ExploreRvAdapter adapter = new ExploreRvAdapter();
private AddDownloadUpdateCardTask addDownloadUpdateCardTask;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -38,11 +49,25 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
return mainView;
}
@Override
public void onDestroyView() {
cancelAddDownloadUpdateCardTask();
super.onDestroyView();
}
private void cancelAddDownloadUpdateCardTask() {
if (addDownloadUpdateCardTask != null) {
addDownloadUpdateCardTask.cancel(true);
addDownloadUpdateCardTask = null;
}
}
private List<Object> generateItems() {
final List<Object> items = new ArrayList<>();
final OsmandApplication app = getMyApplication();
final boolean nightMode = !getSettings().isLightContent();
addDownloadUpdateCard(nightMode);
items.add(new OpenBetaTravelCard(app, nightMode, getFragmentManager()));
items.add(new StartEditingTravelCard(app, nightMode));
addPopularDestinations(items, nightMode);
@ -50,6 +75,11 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
return items;
}
private void addDownloadUpdateCard(boolean nightMode) {
addDownloadUpdateCardTask = new AddDownloadUpdateCardTask(getMyApplication(), adapter, nightMode);
addDownloadUpdateCardTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
private void addPopularDestinations(@NonNull List<Object> items, boolean nightMode) {
OsmandApplication app = getMyApplication();
List<TravelArticle> savedArticles = app.getTravelDbHelper().searchPopular();
@ -60,4 +90,53 @@ public class ExploreTabFragment extends BaseOsmAndFragment {
}
}
}
private static class AddDownloadUpdateCardTask extends AsyncTask<Void, Void, TravelDownloadUpdateCard> {
private OsmandApplication app;
private WeakReference<ExploreRvAdapter> adapterWr;
private boolean nightMode;
AddDownloadUpdateCardTask(OsmandApplication app, ExploreRvAdapter adapter, boolean nightMode) {
this.app = app;
this.adapterWr = new WeakReference<>(adapter);
this.nightMode = nightMode;
}
@Override
protected TravelDownloadUpdateCard doInBackground(Void... voids) {
final boolean[] worldWikivoyageDownloaded = new boolean[1];
new LocalIndexHelper(app).getLocalTravelFiles(new AbstractLoadLocalIndexTask() {
@Override
public void loadFile(LocalIndexInfo... loaded) {
for (LocalIndexInfo lii : loaded) {
if (lii.getBaseName().toLowerCase().equals(DownloadResources.WORLD_WIKIVOYAGE_NAME)) {
worldWikivoyageDownloaded[0] = true;
}
}
}
});
if (!worldWikivoyageDownloaded[0] && !isCancelled()) {
TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, true);
return card;
}
return null;
}
@Override
protected void onPostExecute(TravelDownloadUpdateCard card) {
if (!isCancelled() && card != null) {
ExploreRvAdapter adapter = adapterWr.get();
if (adapter != null) {
if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) {
adapter.notifyItemInserted(DOWNLOAD_UPDATE_CARD_POSITION);
}
}
}
}
}
}

View file

@ -8,6 +8,7 @@ import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
@ -26,8 +27,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
@Override
public void bindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof CardViewHolder) {
CardViewHolder holder = (CardViewHolder) viewHolder;
if (viewHolder instanceof DownloadUpdateVH) {
DownloadUpdateVH holder = (DownloadUpdateVH) viewHolder;
holder.title.setText(getTitle());
holder.icon.setImageDrawable(getIcon());
holder.description.setText(getDescription());
@ -116,14 +117,14 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
}
private void onSecondaryBtnClick() {
Toast.makeText(app, "Secondary button", Toast.LENGTH_SHORT).show();
}
private void onPrimaryBtnClick() {
Toast.makeText(app, "Primary button", Toast.LENGTH_SHORT).show();
}
public class CardViewHolder extends RecyclerView.ViewHolder {
public static class DownloadUpdateVH extends RecyclerView.ViewHolder {
final TextView title;
final ImageView icon;
@ -137,7 +138,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
final TextView primaryButton;
@SuppressWarnings("RedundantCast")
public CardViewHolder(View itemView) {
public DownloadUpdateVH(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
icon = (ImageView) itemView.findViewById(R.id.icon);