Use dialog fragment to delete answer

pull/157/head
M66B 5 years ago
parent 737eef3cbb
commit ce159b1776

@ -21,7 +21,7 @@ package eu.faircode.email;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spanned;
import android.view.LayoutInflater;
@ -41,6 +41,8 @@ import androidx.fragment.app.DialogFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import static android.app.Activity.RESULT_OK;
public class FragmentAnswer extends FragmentBase {
private ViewGroup view;
private EditText etName;
@ -53,6 +55,8 @@ public class FragmentAnswer extends FragmentBase {
private long id = -1;
private final static int REQUEST_DELETE = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -143,46 +147,13 @@ public class FragmentAnswer extends FragmentBase {
}
private void onActionDelete() {
new DialogBuilderLifecycle(getContext(), getViewLifecycleOwner())
.setMessage(R.string.title_ask_delete_answer)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
Helper.setViewsEnabled(view, false);
}
@Override
protected void onPostExecute(Bundle args) {
Helper.setViewsEnabled(view, true);
}
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB.getInstance(context).answer().deleteAnswer(id);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(FragmentAnswer.this, args, "answer:delete");
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
Bundle args = new Bundle();
args.putString("question", getString(R.string.title_ask_delete_answer));
FragmentDialogAsk fragment = new FragmentDialogAsk();
fragment.setArguments(args);
fragment.setTargetFragment(FragmentAnswer.this, REQUEST_DELETE);
fragment.show(getFragmentManager(), "answer:delete");
}
private void onActionSave() {
@ -240,6 +211,52 @@ public class FragmentAnswer extends FragmentBase {
}.execute(this, args, "answer:save");
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_DELETE:
if (resultCode == RESULT_OK)
onDelete();
break;
}
}
private void onDelete() {
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected void onPreExecute(Bundle args) {
Helper.setViewsEnabled(view, false);
}
@Override
protected void onPostExecute(Bundle args) {
Helper.setViewsEnabled(view, true);
}
@Override
protected Void onExecute(Context context, Bundle args) {
long id = args.getLong("id");
DB.getInstance(context).answer().deleteAnswer(id);
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
finish();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.unexpectedError(getFragmentManager(), ex);
}
}.execute(FragmentAnswer.this, args, "answer:delete");
}
public static class FragmentInfo extends DialogFragment {
@NonNull
@Override

Loading…
Cancel
Save