Search in sources :

Example 1 with Task

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();
}
Also used : AlertDialog(android.app.AlertDialog) Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) DialogInterface(android.content.DialogInterface) StringRes(android.support.annotation.StringRes) BaseTransientBottomBar(android.support.design.widget.BaseTransientBottomBar) ArrayList(java.util.ArrayList) CouldNotDeleteDataException(ve.com.abicelis.remindy.exception.CouldNotDeleteDataException) RemindyDAO(ve.com.abicelis.remindy.database.RemindyDAO) Snackbar(android.support.design.widget.Snackbar)

Example 2 with Task

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);
}
Also used : Task(ve.com.abicelis.remindy.model.Task) TaskCategory(ve.com.abicelis.remindy.enums.TaskCategory) Calendar(java.util.Calendar) TaskStatus(ve.com.abicelis.remindy.enums.TaskStatus) ReminderType(ve.com.abicelis.remindy.enums.ReminderType)

Example 3 with Task

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;
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) CouldNotDeleteDataException(ve.com.abicelis.remindy.exception.CouldNotDeleteDataException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 4 with Task

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;
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) TaskViewModel(ve.com.abicelis.remindy.viewmodel.TaskViewModel) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) TaskSortingUtil(ve.com.abicelis.remindy.util.sorting.TaskSortingUtil) Cursor(android.database.Cursor)

Example 5 with Task

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;
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) Task(ve.com.abicelis.remindy.model.Task) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor)

Aggregations

Task (ve.com.abicelis.remindy.model.Task)21 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)12 ArrayList (java.util.ArrayList)10 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)9 TaskViewModel (ve.com.abicelis.remindy.viewmodel.TaskViewModel)8 Cursor (android.database.Cursor)7 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)4 CouldNotDeleteDataException (ve.com.abicelis.remindy.exception.CouldNotDeleteDataException)3 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)3 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)3 TaskSortingUtil (ve.com.abicelis.remindy.util.sorting.TaskSortingUtil)3 NotificationManager (android.app.NotificationManager)2 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)2 Calendar (java.util.Calendar)2 CouldNotUpdateDataException (ve.com.abicelis.remindy.exception.CouldNotUpdateDataException)2 Time (ve.com.abicelis.remindy.model.Time)2 AlertDialog (android.app.AlertDialog)1 PendingIntent (android.app.PendingIntent)1 DialogInterface (android.content.DialogInterface)1 Intent (android.content.Intent)1