use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.
the class PlaceActivity method handleDeletePlace.
private void handleDeletePlace() {
mDao = new RemindyDAO(getApplicationContext());
final BaseTransientBottomBar.BaseCallback<Snackbar> callback = new BaseTransientBottomBar.BaseCallback<Snackbar>() {
@Override
public void onDismissed(Snackbar transientBottomBar, int event) {
super.onDismissed(transientBottomBar, event);
setResult(RESULT_OK);
finish();
}
};
//Check if Place is associated with at least one location-based reminder
List<Task> locationBasedTasks = new ArrayList<>();
try {
locationBasedTasks = mDao.getLocationBasedTasksAssociatedWithPlace(mPlace.getId(), -1);
} catch (CouldNotGetDataException e) {
SnackbarUtil.showSnackbar(mMapContainer, SnackbarUtil.SnackbarType.ERROR, R.string.activity_place_snackbar_error_deleting, SnackbarUtil.SnackbarDuration.LONG, null);
}
//if there are tasks that use this place, warn user
@StringRes int title = (locationBasedTasks.size() > 0 ? R.string.activity_place_dialog_delete_with_associated_tasks_title : R.string.activity_place_dialog_delete_title);
@StringRes int message = (locationBasedTasks.size() > 0 ? R.string.activity_place_dialog_delete_with_associated_tasks_message : R.string.activity_place_dialog_delete_message);
@StringRes int positive = (locationBasedTasks.size() > 0 ? R.string.activity_place_dialog_delete_with_associated_tasks_positive : R.string.activity_place_dialog_delete_positive);
@StringRes int negative = (locationBasedTasks.size() > 0 ? R.string.activity_place_dialog_delete_negative : R.string.activity_place_dialog_delete_negative);
AlertDialog dialog = new AlertDialog.Builder(this).setTitle(getResources().getString(title)).setMessage(getResources().getString(message)).setPositiveButton(getResources().getString(positive), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
mDao.deletePlace(mPlace.getId());
SnackbarUtil.showSnackbar(mMapContainer, SnackbarUtil.SnackbarType.SUCCESS, R.string.activity_place_snackbar_delete_succesful, SnackbarUtil.SnackbarDuration.SHORT, callback);
dialog.dismiss();
} catch (CouldNotDeleteDataException e) {
SnackbarUtil.showSnackbar(mMapContainer, SnackbarUtil.SnackbarType.ERROR, R.string.activity_place_snackbar_error_deleting, SnackbarUtil.SnackbarDuration.LONG, callback);
}
}
}).setNegativeButton(getResources().getString(negative), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.show();
}
use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.
the class RemindyDAO method getTaskFromCursor.
/* Cursor to Model */
private Task getTaskFromCursor(Cursor cursor) {
int id = cursor.getInt(cursor.getColumnIndex(RemindyContract.TaskTable._ID));
TaskStatus status = TaskStatus.valueOf(cursor.getString(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_STATUS.getName())));
String title = cursor.getString(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_TITLE.getName()));
String description = cursor.getString(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_DESCRIPTION.getName()));
TaskCategory category = TaskCategory.valueOf(cursor.getString(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_CATEGORY.getName())));
ReminderType reminderType;
try {
reminderType = ReminderType.valueOf(cursor.getString(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_REMINDER_TYPE.getName())));
} catch (IllegalArgumentException e) {
//Thrown if task has no reminder
reminderType = null;
}
Calendar doneDate = null;
if (status == TaskStatus.DONE) {
long doneDateLong = cursor.getLong(cursor.getColumnIndex(RemindyContract.TaskTable.COLUMN_NAME_DONE_DATE.getName()));
if (doneDateLong != -1) {
doneDate = Calendar.getInstance();
doneDate.setTimeInMillis(doneDateLong);
}
}
return new Task(id, status, title, description, category, reminderType, null, doneDate);
}
use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.
the class RemindyDAO method deleteTask.
/**
* Deletes a Task and associated Attachments and Reminder
* @param taskId The ID of the task
*/
public boolean deleteTask(int taskId) throws CouldNotDeleteDataException {
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
Task task;
try {
task = getTask(taskId);
} catch (CouldNotGetDataException e) {
throw new CouldNotDeleteDataException("Failed to get task from database. TaskID=" + taskId, e);
}
//Delete task's attachments
deleteAttachmentsOfTask(taskId);
//If task has a reminder, delete it
if (task.getReminderType() != ReminderType.NONE) {
deleteReminderOfTask(taskId);
}
//Finally, delete the task
return db.delete(RemindyContract.TaskTable.TABLE_NAME, RemindyContract.TaskTable._ID + " =?", new String[] { String.valueOf(taskId) }) > 0;
}
use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.
the class RemindyDAO method getProgrammedTasks.
/**
* Returns a List of Tasks (with Reminder and Attachments) which have TaskStatus.PROGRAMMED
* @param sortType TaskSortType enum value with which to sort results. By date or location
* @return A List of TaskViewModel
*/
public List<TaskViewModel> getProgrammedTasks(@NonNull TaskSortType sortType, boolean includeLocationBasedTasks, Resources resources) throws CouldNotGetDataException, InvalidClassException {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
List<TaskViewModel> result;
List<Task> tasks = new ArrayList<>();
Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.TaskTable.COLUMN_NAME_STATUS.getName() + "=?", new String[] { TaskStatus.PROGRAMMED.name() }, null, null, null);
try {
while (cursor.moveToNext()) {
Task current = getTaskFromCursor(cursor);
if (//Skip location-based task
!includeLocationBasedTasks && current.getReminderType() == ReminderType.LOCATION_BASED)
continue;
//Try to get the attachments, if there are any
current.setAttachments(getAttachmentsOfTask(current.getId()));
//If Task ReminderType.NONE, throw an error.
if (current.getReminderType() == ReminderType.NONE)
throw new CouldNotGetDataException("Error, Task with TaskStatus=PROGRAMMED has ReminderType=NONE");
else
current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
tasks.add(current);
}
} finally {
cursor.close();
}
//Generate List<TaskViewModel>
result = new TaskSortingUtil().generateProgrammedTaskHeaderList(tasks, sortType, resources);
return result;
}
use of ve.com.abicelis.remindy.model.Task in project Remindy by abicelis.
the class RemindyDAO method getTask.
/**
* Returns a Task (with Reminder and Attachments) given a taskId
* @param taskId The ID of the Task to get.
*/
public Task getTask(int taskId) throws CouldNotGetDataException, SQLiteConstraintException {
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query(RemindyContract.TaskTable.TABLE_NAME, null, RemindyContract.PlaceTable._ID + "=?", new String[] { String.valueOf(taskId) }, null, null, null);
if (cursor.getCount() == 0)
throw new CouldNotGetDataException("Specified Task not found in the database. Passed id=" + taskId);
if (cursor.getCount() > 1)
throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one record found. Passed value=" + taskId);
cursor.moveToNext();
Task task = getTaskFromCursor(cursor);
task.setAttachments(getAttachmentsOfTask(taskId));
if (task.getReminderType() != ReminderType.NONE)
task.setReminder(getReminderOfTask(taskId, task.getReminderType()));
return task;
}
Aggregations