package se.dat255.grupp12;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import com.google.gson.Gson;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
* Created by David on 10/1/13.
* A modal dialog panel that allows the user to set a deadline for the chosen task
*/
public class DeadlinePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private Task task;
private Calendar c = new GregorianCalendar(Locale.US);
private DetailedViewActivity activity;
private int listid;
/**
* Constructor defining which tasks deadline is to be set
* @param task
*/
public DeadlinePickerFragment(Task task, int listid,DetailedViewActivity activity){
this.task=task;
this.activity = activity;
this.listid = listid;
}
/**
* @see android.app.DatePickerDialog.OnDateSetListener
* @param savedInstanceState
* @return the dialog in which the datepicker resides
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
DatePickerDialog dialog;
if(task.getDate()!=null){
dialog = new DatePickerDialog(getActivity(), this, task.getDate().get(Calendar.YEAR),
task.getDate().get(Calendar.MONTH),
task.getDate().get(Calendar.DAY_OF_MONTH));
} else{
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of TimePickerDialog and return it
dialog = new DatePickerDialog(getActivity(), this, year, month, day);
}
return dialog;
}
/**
* Called when the user has set a date. The chosen data is then passed on in order
* to update the task model.
* @param view The view calling the method
* @param year The year chosen
* @param month The month chosen
* @param day The day chosen
*/
public void onDateSet(DatePicker view, int year, int month, int day){
String from = task.getDate().toString();
c.set(c.YEAR, year);
c.set(c.MONTH, month);
c.set(c.DAY_OF_MONTH, day);
task.setDate(c);
activity.setDeadlineText();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String d = format.format(c.getTime());
Modification.logTaskEdit(task.getId(), listid,
new Modification.Change(Modification.Change.TYPE_DEADLINE,
from, d));
}
}