Merge pull request #5290 from osmandapp/download_update_card
Download update card
This commit is contained in:
commit
e951d3aa61
3 changed files with 113 additions and 43 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,16 +38,18 @@ 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;
|
||||
private boolean downloadUpdateCardAdded;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
|
@ -80,52 +83,79 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
|
|||
public void newDownloadIndexes() {
|
||||
if (downloadIndexesRequested) {
|
||||
downloadIndexesRequested = false;
|
||||
final OsmandApplication app = getMyApplication();
|
||||
|
||||
IndexItem wikivoyageItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem();
|
||||
boolean outdated = wikivoyageItem != null && wikivoyageItem.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();
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(app, "Update", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondaryButtonClick() {
|
||||
if (downloadUpdateCard.isLoadingInProgress()) {
|
||||
Toast.makeText(app, "Cancel", Toast.LENGTH_SHORT).show();
|
||||
} else if (!downloadUpdateCard.isDownload()) {
|
||||
Toast.makeText(app, "Later", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
});
|
||||
downloadUpdateCard.setIndexItem(wikivoyageItem);
|
||||
if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, downloadUpdateCard)) {
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
indexItem = getMyApplication().getDownloadThread().getIndexes().getWorldWikivoyageItem();
|
||||
addDownloadUpdateCard(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadInProgress() {
|
||||
|
||||
DownloadIndexesThread downloadThread = getMyApplication().getDownloadThread();
|
||||
IndexItem current = downloadThread.getCurrentDownloadingItem();
|
||||
indexItem = downloadThread.getIndexes().getWorldWikivoyageItem();
|
||||
if (current != null
|
||||
&& indexItem != null
|
||||
&& current == indexItem
|
||||
&& (!current.isDownloaded() || current.isOutdated())) {
|
||||
addDownloadUpdateCard(true);
|
||||
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);
|
||||
removeDownloadUpdateCard();
|
||||
}
|
||||
}
|
||||
|
||||
private void addDownloadUpdateCard(boolean loadingInProgress) {
|
||||
if (downloadUpdateCardAdded) {
|
||||
return;
|
||||
}
|
||||
|
||||
final OsmandApplication app = getMyApplication();
|
||||
|
||||
boolean outdated = indexItem != null && indexItem.isOutdated();
|
||||
|
||||
if (!worldWikivoyageDownloaded || outdated) {
|
||||
downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
|
||||
downloadUpdateCard.setLoadingInProgress(loadingInProgress);
|
||||
downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
|
||||
@Override
|
||||
public void onPrimaryButtonClick() {
|
||||
if (app.getSettings().isInternetConnectionAvailable()) {
|
||||
new DownloadValidationManager(app).startDownload(getMyActivity(), indexItem);
|
||||
downloadUpdateCard.setLoadingInProgress(true);
|
||||
adapter.updateDownloadUpdateCard();
|
||||
} else {
|
||||
Toast.makeText(app, app.getString(R.string.no_index_file_to_download), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSecondaryButtonClick() {
|
||||
if (downloadUpdateCard.isLoadingInProgress()) {
|
||||
app.getDownloadThread().cancelDownload(indexItem);
|
||||
downloadUpdateCard.setLoadingInProgress(false);
|
||||
adapter.updateDownloadUpdateCard();
|
||||
} else if (!downloadUpdateCard.isDownload()) {
|
||||
removeDownloadUpdateCard();
|
||||
}
|
||||
}
|
||||
});
|
||||
downloadUpdateCard.setIndexItem(indexItem);
|
||||
adapter.setDownloadUpdateCard(downloadUpdateCard);
|
||||
downloadUpdateCardAdded = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void removeDownloadUpdateCard() {
|
||||
adapter.removeDownloadUpdateCard();
|
||||
downloadUpdateCardAdded = false;
|
||||
}
|
||||
|
||||
private List<BaseTravelCard> generateItems() {
|
||||
|
@ -148,8 +178,16 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
|
|||
@Override
|
||||
public void onCheckFinished(boolean worldWikivoyageDownloaded) {
|
||||
ExploreTabFragment.this.worldWikivoyageDownloaded = worldWikivoyageDownloaded;
|
||||
downloadIndexesRequested = true;
|
||||
app.getDownloadThread().runReloadIndexFilesSilent();
|
||||
DownloadIndexesThread downloadThread = app.getDownloadThread();
|
||||
if (!downloadThread.getIndexes().isDownloadedFromInternet) {
|
||||
downloadIndexesRequested = true;
|
||||
app.getDownloadThread().runReloadIndexFilesSilent();
|
||||
} else {
|
||||
indexItem = downloadThread.getIndexes().getWorldWikivoyageItem();
|
||||
IndexItem current = downloadThread.getCurrentDownloadingItem();
|
||||
boolean loadingInProgress = current != null && indexItem != null && current == indexItem;
|
||||
addDownloadUpdateCard(loadingInProgress);
|
||||
}
|
||||
}
|
||||
}).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
@ -126,8 +132,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
|
|||
*/
|
||||
private boolean updateSecondaryButton(DownloadUpdateVH vh) {
|
||||
if (loadingInProgress || !download) {
|
||||
vh.secondaryBtnContainer.setVisibility(View.VISIBLE);
|
||||
vh.secondaryBtn.setText(loadingInProgress ? R.string.shared_string_cancel : R.string.later);
|
||||
vh.secondaryBtn.setVisibility(View.VISIBLE);
|
||||
vh.secondaryBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
@ -147,8 +153,8 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
|
|||
*/
|
||||
private boolean updatePrimaryButton(DownloadUpdateVH vh) {
|
||||
if (!loadingInProgress) {
|
||||
vh.primaryBtnContainer.setVisibility(View.VISIBLE);
|
||||
vh.primaryButton.setText(download ? R.string.shared_string_download : R.string.shared_string_update);
|
||||
vh.primaryButton.setVisibility(View.VISIBLE);
|
||||
vh.primaryButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
|
Loading…
Reference in a new issue