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

View file

@ -9,7 +9,6 @@ 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;
@ -24,11 +23,29 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
private boolean download;
private boolean loadingInProgress;
private ClickListener listener;
@Nullable
private IndexItem indexItem;
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) {
this.indexItem = indexItem;
}
@ -53,6 +70,7 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
holder.fileIcon.setImageDrawable(getFileIcon());
holder.fileTitle.setText(getFileTitle());
holder.fileDescription.setText(getFileDescription());
holder.progressBar.setVisibility(loadingInProgress ? View.VISIBLE : View.GONE);
}
boolean primaryBtnVisible = updatePrimaryButton(holder);
boolean secondaryBtnVisible = updateSecondaryButton(holder);
@ -113,7 +131,9 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
vh.secondaryBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSecondaryBtnClick();
if (listener != null) {
listener.onSecondaryButtonClick();
}
}
});
return true;
@ -132,7 +152,9 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
vh.primaryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onPrimaryBtnClick();
if (listener != null) {
listener.onPrimaryButtonClick();
}
}
});
return true;
@ -141,20 +163,11 @@ public class TravelDownloadUpdateCard extends BaseTravelCard {
return false;
}
private void onSecondaryBtnClick() {
if (loadingInProgress) {
Toast.makeText(app, "Cancel", Toast.LENGTH_SHORT).show();
} else if (!download) {
Toast.makeText(app, "Later", Toast.LENGTH_SHORT).show();
}
}
public interface ClickListener {
private void onPrimaryBtnClick() {
if (download) {
Toast.makeText(app, "Download", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(app, "Update", Toast.LENGTH_SHORT).show();
}
void onPrimaryButtonClick();
void onSecondaryButtonClick();
}
public static class DownloadUpdateVH extends RecyclerView.ViewHolder {