Extract clicks handling to the fragment

This commit is contained in:
Alex Sytnyk 2018-04-26 22:41:53 +03:00
parent 26235c3b1c
commit 4f9bcf5d79
2 changed files with 59 additions and 21 deletions

View file

@ -9,6 +9,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Toast;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -40,6 +41,7 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
private ExploreRvAdapter adapter = new ExploreRvAdapter(); private ExploreRvAdapter adapter = new ExploreRvAdapter();
private StartEditingTravelCard startEditingTravelCard; private StartEditingTravelCard startEditingTravelCard;
private TravelDownloadUpdateCard downloadUpdateCard;
private boolean nightMode; private boolean nightMode;
@ -78,15 +80,38 @@ public class ExploreTabFragment extends BaseOsmAndFragment implements DownloadIn
public void newDownloadIndexes() { public void newDownloadIndexes() {
if (downloadIndexesRequested) { if (downloadIndexesRequested) {
downloadIndexesRequested = false; downloadIndexesRequested = false;
OsmandApplication app = getMyApplication(); final OsmandApplication app = getMyApplication();
IndexItem wikivoyageItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem(); IndexItem wikivoyageItem = app.getDownloadThread().getIndexes().getWorldWikivoyageItem();
boolean outdated = wikivoyageItem != null && wikivoyageItem.isOutdated(); boolean outdated = wikivoyageItem != null && wikivoyageItem.isOutdated();
if (!worldWikivoyageDownloaded || outdated) { if (!worldWikivoyageDownloaded || outdated) {
TravelDownloadUpdateCard card = new TravelDownloadUpdateCard(app, nightMode, !outdated); downloadUpdateCard = new TravelDownloadUpdateCard(app, nightMode, !outdated);
card.setIndexItem(wikivoyageItem); downloadUpdateCard.setListener(new TravelDownloadUpdateCard.ClickListener() {
if (adapter.addItem(DOWNLOAD_UPDATE_CARD_POSITION, card)) { @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(); adapter.notifyDataSetChanged();
} }
} }

View file

@ -9,7 +9,6 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
@ -24,11 +23,29 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
private boolean download; private boolean download;
private boolean loadingInProgress; private boolean loadingInProgress;
private ClickListener listener;
@Nullable @Nullable
private IndexItem indexItem; private IndexItem indexItem;
private DateFormat dateFormat; private DateFormat dateFormat;
public boolean isDownload() {
return download;
}
public boolean isLoadingInProgress() {
return loadingInProgress;
}
public void setLoadingInProgress(boolean loadingInProgress) {
this.loadingInProgress = loadingInProgress;
}
public void setListener(ClickListener listener) {
this.listener = listener;
}
public void setIndexItem(@Nullable IndexItem indexItem) { public void setIndexItem(@Nullable IndexItem indexItem) {
this.indexItem = indexItem; this.indexItem = indexItem;
} }
@ -53,6 +70,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
holder.fileIcon.setImageDrawable(getFileIcon()); holder.fileIcon.setImageDrawable(getFileIcon());
holder.fileTitle.setText(getFileTitle()); holder.fileTitle.setText(getFileTitle());
holder.fileDescription.setText(getFileDescription()); holder.fileDescription.setText(getFileDescription());
holder.progressBar.setVisibility(loadingInProgress ? View.VISIBLE : View.GONE);
} }
boolean primaryBtnVisible = updatePrimaryButton(holder); boolean primaryBtnVisible = updatePrimaryButton(holder);
boolean secondaryBtnVisible = updateSecondaryButton(holder); boolean secondaryBtnVisible = updateSecondaryButton(holder);
@ -113,7 +131,9 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
vh.secondaryBtn.setOnClickListener(new View.OnClickListener() { vh.secondaryBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
onSecondaryBtnClick(); if (listener != null) {
listener.onSecondaryButtonClick();
}
} }
}); });
return true; return true;
@ -132,7 +152,9 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
vh.primaryButton.setOnClickListener(new View.OnClickListener() { vh.primaryButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
onPrimaryBtnClick(); if (listener != null) {
listener.onPrimaryButtonClick();
}
} }
}); });
return true; return true;
@ -141,20 +163,11 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
return false; return false;
} }
private void onSecondaryBtnClick() { public interface ClickListener {
if (loadingInProgress) {
Toast.makeText(app, "Cancel", Toast.LENGTH_SHORT).show();
} else if (!download) {
Toast.makeText(app, "Later", Toast.LENGTH_SHORT).show();
}
}
private void onPrimaryBtnClick() { void onPrimaryButtonClick();
if (download) {
Toast.makeText(app, "Download", Toast.LENGTH_SHORT).show(); void onSecondaryButtonClick();
} else {
Toast.makeText(app, "Update", Toast.LENGTH_SHORT).show();
}
} }
public static class DownloadUpdateVH extends RecyclerView.ViewHolder { public static class DownloadUpdateVH extends RecyclerView.ViewHolder {