Cleanup and refactor in TasksDialogFragment in preparation for abstraction

This commit is contained in:
2017-03-20 19:37:46 -04:00
parent 63843043f9
commit 428de8e2b1
2 changed files with 21 additions and 32 deletions

Binary file not shown.

View File

@@ -19,7 +19,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox; import android.widget.CheckBox;
import android.widget.DatePicker; import android.widget.DatePicker;
import android.widget.EditText; import android.widget.EditText;
@@ -67,11 +66,6 @@ public class TaskDialogFragment extends DialogFragment {
return fragment; return fragment;
} }
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@@ -99,25 +93,30 @@ public class TaskDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
// Inflate and set the layout for the dialog // Inflate and set the layout for the dialog
LayoutInflater inflater = getActivity().getLayoutInflater(); LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.fragment_task_form, null); final View view = inflater.inflate(R.layout.fragment_task_form, null);
setToolbarTitle(view); setToolbarTitle(view);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Pass null as the parent view because its going in the dialog layout // Pass null as the parent view because its going in the dialog layout
builder.setView(view) builder.setView(view)
// Add action buttons // Add action buttons
.setPositiveButton(R.string.new_task_save, null) .setPositiveButton(R.string.new_task_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
onPositiveButtonClick(view);
}
})
.setNegativeButton(R.string.new_task_cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.new_task_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity // Send the negative button event back to the host activity
// Canceled creation, nothing to do // Canceled creation, nothing to do
TaskDialogFragment.this.getDialog().cancel(); //dialog.cancel();
onNegativeButtonClick();
} }
}); });
if (task != null) { if (task != null) {
builder.setNeutralButton(R.string.new_task_delete, new DialogInterface.OnClickListener() { builder.setNeutralButton(R.string.new_task_delete, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
mListener.onNewTaskDialogNeutralClick(TaskDialogFragment.this); onNeutralButtonClick();
} }
}); });
} }
@@ -125,24 +124,6 @@ public class TaskDialogFragment extends DialogFragment {
return builder.create(); return builder.create();
} }
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if(dialog != null && dialog instanceof AlertDialog)
{
AlertDialog d = (AlertDialog) dialog;
Button positiveButton = d.getButton(Dialog.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onSaveClickListener(v.getRootView());
}
});
}
}
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//super.onCreateOptionsMenu(menu, inflater); //super.onCreateOptionsMenu(menu, inflater);
@@ -171,17 +152,17 @@ public class TaskDialogFragment extends DialogFragment {
} }
if (id == R.id.menu_new_task_save) { if (id == R.id.menu_new_task_save) {
// handle save button click here // handle save button click here
onSaveClickListener(view); onPositiveButtonClick(view);
return true; return true;
} }
else if (id == R.id.menu_new_task_delete) { else if (id == R.id.menu_new_task_delete) {
// handle delete button click here // handle delete button click here
mListener.onNewTaskDialogNeutralClick(TaskDialogFragment.this); onNeutralButtonClick();
return true; return true;
} }
else if (id == android.R.id.home) { else if (id == android.R.id.home) {
// handle close button click here // handle close button click here
dismiss(); onNegativeButtonClick();
return true; return true;
} }
@@ -251,7 +232,7 @@ public class TaskDialogFragment extends DialogFragment {
return toolbar; return toolbar;
} }
private void onSaveClickListener(View view) { protected void onPositiveButtonClick(View view) {
if (view == null) return; if (view == null) return;
EditText titleText = (EditText) view.findViewById(R.id.new_task_name); EditText titleText = (EditText) view.findViewById(R.id.new_task_name);
// handle confirmation button click hereEditText titleText = (EditText) d.findViewById(R.id.new_task_name); // handle confirmation button click hereEditText titleText = (EditText) d.findViewById(R.id.new_task_name);
@@ -263,4 +244,12 @@ public class TaskDialogFragment extends DialogFragment {
dismiss(); dismiss();
} }
} }
protected /*abstract*/ void onNeutralButtonClick() {
mListener.onNewTaskDialogNeutralClick(TaskDialogFragment.this);
}
protected /*abstract*/ void onNegativeButtonClick() {
dismiss();
}
} }