[WIP] add DeleteTask.askReasonAndExecute

This commit is contained in:
Yusuke Matsubara 2018-05-19 22:24:19 +02:00 committed by maskara
parent 8b5a4e4af4
commit 6d49a854ee
2 changed files with 53 additions and 0 deletions

View file

@ -1,11 +1,17 @@
package fr.free.nrw.commons.delete;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
@ -14,6 +20,7 @@ import java.util.Locale;
import javax.inject.Inject;
import butterknife.OnClick;
import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.Media;
import fr.free.nrw.commons.R;
@ -174,4 +181,49 @@ public class DeleteTask extends AsyncTask<Void, Integer, Boolean> {
.setPriority(PRIORITY_HIGH);
notificationManager.notify(NOTIFICATION_DELETE, notificationBuilder.build());
}
// TODO: refactor; see MediaDetailsFragment.onDeleteButtonClicked
// ReviewActivity will use this
public static void askReasonAndExecute(Media media, Context context, String question, String defaultValue) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setMessage(question);
final EditText input = new EditText(context);
input.setText(defaultValue);
alert.setView(input);
input.requestFocus();
alert.setPositiveButton(R.string.ok, (dialog, whichButton) -> {
String reason = input.getText().toString();
DeleteTask deleteTask = new DeleteTask(context, media, reason);
deleteTask.execute();
});
alert.setNegativeButton(R.string.cancel, (dialog, whichButton) -> {
});
AlertDialog d = alert.create();
input.addTextChangedListener(new TextWatcher() {
private void handleText() {
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE);
if (input.getText().length() == 0) {
okButton.setEnabled(false);
} else {
okButton.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable arg0) {
handleText();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
d.show();
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(defaultValue.length() > 0);
}
}

View file

@ -22,6 +22,7 @@ import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
// Example code:
// CheckCategoryTask deleteTask = new CheckCategoryTask(getActivity(), media);
// TODO: refactor; see DeleteTask and SendThankTask
public class CheckCategoryTask extends AsyncTask<Void, Integer, Boolean> {
@Inject