Add download / update functionality

This commit is contained in:
Alex Sytnyk 2018-04-26 23:13:09 +03:00
parent 4f9bcf5d79
commit 19171593ec
3 changed files with 65 additions and 20 deletions

View file

@ -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<RecyclerView.ViewHolder> {
private static final int DOWNLOAD_UPDATE_CARD_POSITION = 0;
private final List<BaseTravelCard> items = new ArrayList<>();
@NonNull
@ -119,6 +121,10 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
this.items.addAll(items);
}
private void removeItem(int position) {
items.remove(position);
}
public boolean addItem(int position, BaseTravelCard item) {
if (position >= 0 && position <= items.size()) {
items.add(position, item);
@ -126,4 +132,24 @@ public class ExploreRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
}
return false;
}
public void setDownloadUpdateCard(TravelDownloadUpdateCard card) {
if (addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) {
notifyDataSetChanged();
}
}
public void updateDownloadUpdateCard() {
notifyItemChanged(DOWNLOAD_UPDATE_CARD_POSITION);
}
public void removeDownloadUpdateCard() {
if (items.size() > 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);
}
}
}
}

View file

@ -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<BaseTravelCard> generateItems() {

View file

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