in Android/app/src/main/java/com/harvard/studyappmodule/custom/question/CustomDateQuestionBody.java [198:493]
private void showDialog(final TextView tv, final LayoutInflater inflater) {
// need to find a material date picker, since it's not in the support library
final ContextThemeWrapper contextWrapper =
new ContextThemeWrapper(tv.getContext(), R.style.Theme_Backbone);
if (format.getStyle() == AnswerFormatCustom.DateAnswerStyle.Date) {
final DatePickerDialog datePickerDialog = new DatePickerDialog(contextWrapper, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
}
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setButton(
DialogInterface.BUTTON_NEGATIVE,
inflater.getContext().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_cancel));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
}
}
});
datePickerDialog.setButton(
DialogInterface.BUTTON_POSITIVE,
inflater.getContext().getString(R.string.ok_2),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_ok));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_POSITIVE) {
dialog.dismiss();
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(calendar.getTime());
calendar1.set(
datePickerDialog.getDatePicker().getYear(),
datePickerDialog.getDatePicker().getMonth(),
datePickerDialog.getDatePicker().getDayOfMonth());
if (format.validateAnswer(calendar1.getTime()).isValid()) {
hasChosenDate = true;
calendar.set(
datePickerDialog.getDatePicker().getYear(),
datePickerDialog.getDatePicker().getMonth(),
datePickerDialog.getDatePicker().getDayOfMonth());
// Set result to our edit text
String formattedResult = CustomDateQuestionBody.this.createFormattedResult();
tv.setText(formattedResult);
} else {
Toast.makeText(
inflater.getContext(),
format
.validateAnswer(calendar1.getTime())
.getString(inflater.getContext()),
Toast.LENGTH_LONG)
.show();
}
}
}
});
datePickerDialog.setButton(
DialogInterface.BUTTON_NEUTRAL,
inflater.getContext().getString(R.string.clear),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_clear));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEUTRAL) {
dialog.dismiss();
tv.setText("");
hasChosenDate = false;
}
}
});
datePickerDialog.show();
} else if (format.getStyle() == AnswerFormatCustom.DateAnswerStyle.TimeOfDay) {
TimePickerDialog timePickerDialog =
new TimePickerDialog(contextWrapper, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(calendar.getTime());
calendar1.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar1.set(Calendar.MINUTE, minute);
if (format.validateAnswer(calendar1.getTime()).isValid()) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
hasChosenDate = true;
// Set result to our edit text
String formattedResult = CustomDateQuestionBody.this.createFormattedResult();
tv.setText(formattedResult);
} else {
Toast.makeText(
inflater.getContext(),
format
.validateAnswer(calendar1.getTime())
.getString(inflater.getContext()),
Toast.LENGTH_LONG)
.show();
}
}
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
true);
timePickerDialog.setButton(
DialogInterface.BUTTON_NEGATIVE,
inflater.getContext().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_cancel));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
}
}
});
timePickerDialog.setButton(
DialogInterface.BUTTON_NEUTRAL,
inflater.getContext().getString(R.string.clear),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_clear));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEUTRAL) {
dialog.dismiss();
tv.setText("");
hasChosenDate = false;
}
}
});
timePickerDialog.show();
} else if (format.getStyle() == AnswerFormatCustom.DateAnswerStyle.DateAndTime) {
final DatePickerDialog datePickerDialog = new DatePickerDialog(contextWrapper, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(
DatePicker dview, int year, int monthOfYear, int dayOfMonth) {
}
},
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setButton(
DialogInterface.BUTTON_NEGATIVE,
inflater.getContext().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_cancel));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
}
}
});
datePickerDialog.setButton(
DialogInterface.BUTTON_NEUTRAL,
inflater.getContext().getString(R.string.clear),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_clear));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEUTRAL) {
dialog.dismiss();
tv.setText("");
hasChosenDate = false;
}
}
});
datePickerDialog.setButton(
DialogInterface.BUTTON_POSITIVE,
inflater.getContext().getString(R.string.ok_2),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_ok));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_POSITIVE) {
dialog.dismiss();
calendar.set(
datePickerDialog.getDatePicker().getYear(),
datePickerDialog.getDatePicker().getMonth(),
datePickerDialog.getDatePicker().getDayOfMonth());
TimePickerDialog timePickerDialog =
new TimePickerDialog(
contextWrapper,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker tview, int hourOfDay, int minute) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(calendar.getTime());
calendar1.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar1.set(Calendar.MINUTE, minute);
if (format.validateAnswer(calendar1.getTime()).isValid()) {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
hasChosenDate = true;
// Set result to our edit text
String formattedResult =
CustomDateQuestionBody.this.createFormattedResult();
tv.setText(formattedResult);
} else {
Toast.makeText(
inflater.getContext(),
format
.validateAnswer(calendar1.getTime())
.getString(inflater.getContext()),
Toast.LENGTH_LONG)
.show();
}
}
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
true);
timePickerDialog.setButton(
DialogInterface.BUTTON_NEGATIVE,
inflater.getContext().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_cancel));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEGATIVE) {
dialog.dismiss();
}
}
});
timePickerDialog.setButton(
DialogInterface.BUTTON_NEUTRAL,
inflater.getContext().getString(R.string.clear),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Bundle eventProperties = new Bundle();
eventProperties.putString(
CustomFirebaseAnalytics.Param.BUTTON_CLICK_REASON,
context.getString(R.string.custom_data_question_clear));
analyticsInstance.logEvent(
CustomFirebaseAnalytics.Event.ADD_BUTTON_CLICK, eventProperties);
if (which == DialogInterface.BUTTON_NEUTRAL) {
dialog.dismiss();
tv.setText("");
hasChosenDate = false;
}
}
});
timePickerDialog.show();
}
}
});
datePickerDialog.show();
} else {
throw new RuntimeException("DateAnswerStyle " + format.getStyle() + " is not recognised");
}
}