diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java index dcf4ee29bb..5e6548642b 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreRvAdapter.java @@ -8,9 +8,9 @@ import android.view.View; import android.view.ViewGroup; import net.osmand.plus.R; -import net.osmand.plus.wikivoyage.explore.travelcards.BaseTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.ArticleTravelCard.ArticleTravelVH; +import net.osmand.plus.wikivoyage.explore.travelcards.BaseTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.HeaderTravelCard; import net.osmand.plus.wikivoyage.explore.travelcards.HeaderTravelCard.HeaderTravelVH; import net.osmand.plus.wikivoyage.explore.travelcards.OpenBetaTravelCard; @@ -25,6 +25,8 @@ import java.util.List; public class ExploreRvAdapter extends RecyclerView.Adapter { + private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0; + private final List items = new ArrayList<>(); @NonNull @@ -119,6 +121,10 @@ public class ExploreRvAdapter extends RecyclerView.Adapter= 0 && position <= items.size()) { items.add(position, item); @@ -126,4 +132,24 @@ public class ExploreRvAdapter extends RecyclerView.Adapter DOWNLOAD_UPDATE_CARD_POSITION) { + BaseTravelCard card = getItem(DOWNLOAD_UPDATE_CARD_POSITION); + if (card.getCardType() == TravelDownloadUpdateCard.TYPE) { + removeItem(DOWNLOAD_UPDATE_CARD_POSITION); + notifyItemRemoved(DOWNLOAD_UPDATE_CARD_POSITION); + } + } + } } diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java index 9e588ab252..87fd3dd9b8 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/ExploreTabFragment.java @@ -20,6 +20,7 @@ import net.osmand.plus.activities.OsmandActionBarActivity; import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.download.DownloadIndexesThread; import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.DownloadValidationManager; import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.ui.AbstractLoadLocalIndexTask; import net.osmand.plus.wikivoyage.data.TravelArticle; @@ -37,14 +38,15 @@ import java.util.List; public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIndexesThread.DownloadEvents { - private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0; - private ExploreRvAdapter adapter = new ExploreRvAdapter(); + private StartEditingTravelCard startEditingTravelCard; private TravelDownloadUpdateCard downloadUpdateCard; private boolean nightMode; + private IndexItem indexItem; + private boolean worldWikivoyageDownloaded; private boolean downloadIndexesRequested; @@ -82,50 +84,61 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn downloadIndexesRequested = false; final OsmandApplication app = getMyApplication(); - IndexItem wikivoyageItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem(); - boolean outdated = wikivoyageItem != null && wikivoyageItem.isOutdated(); + indexItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem(); + boolean outdated = indexItem != null && indexItem.isOutdated(); if (!worldWikivoyageDownloaded || outdated) { downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated); downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() { @Override public void onPrimaryButtonClick() { - if (downloadUpdateCard.isDownload()) { - if (app.getSettings().isInternetConnectionAvailable()) { - Toast.makeText(app, "Download", Toast.LENGTH_SHORT).show(); - } else { - Toast.makeText(app, app.getString(R.string.no_index_file_to_download), Toast.LENGTH_SHORT).show(); - } + if (app.getSettings().isInternetConnectionAvailable()) { + new DownloadValidationManager(app).startDownload(getMyActivity(), indexItem); + downloadUpdateCard.setLoadingInProgress(true); + adapter.updateDownloadUpdateCard(); } else { - Toast.makeText(app, "Update", Toast.LENGTH_SHORT).show(); + Toast.makeText(app, app.getString(R.string.no_index_file_to_download), Toast.LENGTH_SHORT).show(); } } @Override public void onSecondaryButtonClick() { if (downloadUpdateCard.isLoadingInProgress()) { - Toast.makeText(app, "Cancel", Toast.LENGTH_SHORT).show(); + app.getDownloadThread().cancelDownload(indexItem); + downloadUpdateCard.setLoadingInProgress(false); + adapter.updateDownloadUpdateCard(); } else if (!downloadUpdateCard.isDownload()) { - Toast.makeText(app, "Later", Toast.LENGTH_SHORT).show(); + adapter.removeDownloadUpdateCard(); } } }); - downloadUpdateCard.setIndexItem(wikivoyageItem); - if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, downloadUpdateCard)) { - adapter.notifyDataSetChanged(); - } + downloadUpdateCard.setIndexItem(indexItem); + adapter.setDownloadUpdateCard(downloadUpdateCard); } } } @Override public void downloadInProgress() { - + DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread(); + IndexItem current = downloadThread.getCurrentDownloadingItem(); + if (downloadUpdateCard != null + && current != null + && (!current.isDownloaded() || current.isOutdated()) + && indexItem != null + && current == indexItem) { + downloadUpdateCard.setProgress(downloadThread.getCurrentDownloadingItemProgress()); + adapter.updateDownloadUpdateCard(); + } } @Override public void downloadHasFinished() { - + IndexItem current = getMyApplication().getDownloadThread().getCurrentDownloadingItem(); + if (downloadUpdateCard != null && current != null && indexItem != null && current == indexItem) { + downloadUpdateCard.setLoadingInProgress(false); + adapter.removeDownloadUpdateCard(); + } } private List generateItems() { diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java index fc93810154..318134a0e1 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/travelcards/TravelDownloadUpdateCard.java @@ -22,6 +22,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { private boolean download; private boolean loadingInProgress; + private int progress; private ClickListener listener; @@ -42,6 +43,10 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { this.loadingInProgress = loadingInProgress; } + public void setProgress(int progress) { + this.progress = progress; + } + public void setListener(ClickListener listener) { this.listener = listener; } @@ -71,6 +76,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard { holder.fileTitle.setText(getFileTitle()); holder.fileDescription.setText(getFileDescription()); holder.progressBar.setVisibility(loadingInProgress ? View.VISIBLE : View.GONE); + holder.progressBar.setProgress(progress < 0 ? 0 : progress); } boolean primaryBtnVisible = updatePrimaryButton(holder); boolean secondaryBtnVisible = updateSecondaryButton(holder);