Search in sources :

Example 6 with OneTimeReminder

use of ve.com.abicelis.remindy.model.reminder.OneTimeReminder in project Remindy by abicelis.

the class RemindyDAO method getNextTaskToTrigger.

/**
     * Returns the next PROGRAMMED task(With ONE-TIME or REPEATING reminder) to occur
     * @param alreadyTriggeredTaskList an optional task list to not include in the search
     * @return A single TaskTriggerViewModel or null of there are no tasks
     */
public TaskTriggerViewModel getNextTaskToTrigger(@NonNull List<Integer> alreadyTriggeredTaskList) throws CouldNotGetDataException {
    SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
    Task nextTaskToTrigger = null;
    Calendar triggerDate = null;
    Time triggerTime = null;
    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
            alreadyTriggeredTaskList.contains(current.getId()))
                continue;
            try {
                current.setReminder(getReminderOfTask(current.getId(), current.getReminderType()));
            } catch (CouldNotGetDataException | SQLiteConstraintException e) {
                throw new CouldNotGetDataException("Error fetching reminder for task ID" + current.getId(), e);
            }
            //TODO: this filter could be made on db query.
            if (current.getReminderType().equals(ReminderType.ONE_TIME) || current.getReminderType().equals(ReminderType.REPEATING)) {
                if (//Skip overdue reminders
                TaskUtil.checkIfOverdue(current.getReminder()))
                    continue;
                if (nextTaskToTrigger == null) {
                    nextTaskToTrigger = current;
                    triggerDate = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getDate() : TaskUtil.getRepeatingReminderNextCalendar((RepeatingReminder) current.getReminder()));
                    triggerTime = (current.getReminderType().equals(ReminderType.ONE_TIME) ? ((OneTimeReminder) current.getReminder()).getTime() : ((RepeatingReminder) current.getReminder()).getTime());
                    continue;
                }
                if (current.getReminderType().equals(ReminderType.ONE_TIME)) {
                    OneTimeReminder otr = (OneTimeReminder) current.getReminder();
                    Calendar currentDate = CalendarUtil.getCalendarFromDateAndTime(otr.getDate(), otr.getTime());
                    if (currentDate.compareTo(triggerDate) < 0) {
                        nextTaskToTrigger = current;
                        triggerDate = currentDate;
                        triggerTime = otr.getTime();
                        continue;
                    }
                }
                if (current.getReminderType().equals(ReminderType.REPEATING)) {
                    RepeatingReminder rr = (RepeatingReminder) current.getReminder();
                    Calendar currentDate = TaskUtil.getRepeatingReminderNextCalendar(rr);
                    //Overdue
                    if (currentDate == null)
                        continue;
                    if (currentDate.compareTo(triggerDate) < 0) {
                        nextTaskToTrigger = current;
                        triggerDate = currentDate;
                        triggerTime = rr.getTime();
                        continue;
                    }
                }
            }
        }
    } finally {
        cursor.close();
    }
    if (nextTaskToTrigger == null)
        return null;
    return new TaskTriggerViewModel(nextTaskToTrigger, triggerDate, triggerTime);
}
Also used : Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Calendar(java.util.Calendar) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Time(ve.com.abicelis.remindy.model.Time) TaskTriggerViewModel(ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel) Cursor(android.database.Cursor)

Example 7 with OneTimeReminder

use of ve.com.abicelis.remindy.model.reminder.OneTimeReminder in project Remindy by abicelis.

the class ReminderFragment method handleReminderTypeSelection.

private void handleReminderTypeSelection(int position) {
    Bundle bundle = new Bundle();
    mReminderType = ReminderType.values()[position];
    switch(mReminderType) {
        case ONE_TIME:
            mReminder = (useReminderFlag ? mTask.getReminder() : new OneTimeReminder());
            mFragment = new EditOneTimeReminderFragment();
            bundle.putSerializable(EditRepeatingReminderFragment.REMINDER_ARGUMENT, mReminder);
            mFragment.setArguments(bundle);
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_reminder_reminder_placeholder, mFragment).commit();
            break;
        case REPEATING:
            mReminder = (useReminderFlag ? mTask.getReminder() : new RepeatingReminder());
            mFragment = new EditRepeatingReminderFragment();
            bundle.putSerializable(EditRepeatingReminderFragment.REMINDER_ARGUMENT, mReminder);
            mFragment.setArguments(bundle);
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_reminder_reminder_placeholder, mFragment).commit();
            break;
        case LOCATION_BASED:
            if (atLeastOnePlaceExists()) {
                handleLocationBasedTaskReminderSelected();
            } else
                handleNoPlacesExist();
            break;
        case NONE:
            if (mFragment != null)
                getActivity().getSupportFragmentManager().beginTransaction().remove(mFragment).commit();
            mFragment = null;
            mReminder = null;
            break;
    }
    //Reset flag if used
    useReminderFlag = false;
}
Also used : OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) Bundle(android.os.Bundle) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder)

Example 8 with OneTimeReminder

use of ve.com.abicelis.remindy.model.reminder.OneTimeReminder in project Remindy by abicelis.

the class RemindyDAO method insertReminderOfTask.

/**
     * Inserts a new Reminder into the database.
     * @param taskId The id of the Task associated to the Reminder
     * @param reminder The Reminder to insert
     */
public long insertReminderOfTask(int taskId, Reminder reminder) throws CouldNotInsertDataException {
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
    ContentValues values;
    String tableName;
    reminder.setTaskId(taskId);
    switch(reminder.getType()) {
        case ONE_TIME:
            values = getValuesFromOneTimeReminder((OneTimeReminder) reminder);
            tableName = RemindyContract.OneTimeReminderTable.TABLE_NAME;
            break;
        case REPEATING:
            values = getValuesFromRepeatingReminder((RepeatingReminder) reminder);
            tableName = RemindyContract.RepeatingReminderTable.TABLE_NAME;
            break;
        case LOCATION_BASED:
            values = getValuesFromLocationBasedReminder((LocationBasedReminder) reminder);
            tableName = RemindyContract.LocationBasedReminderTable.TABLE_NAME;
            break;
        default:
            throw new CouldNotInsertDataException("ReminderType is invalid. Type=" + reminder.getType());
    }
    long newRowId;
    newRowId = db.insert(tableName, null, values);
    if (newRowId == -1)
        throw new CouldNotInsertDataException("There was a problem inserting the Reminder: " + reminder.toString());
    return newRowId;
}
Also used : ContentValues(android.content.ContentValues) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) LocationBasedReminder(ve.com.abicelis.remindy.model.reminder.LocationBasedReminder) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) CouldNotInsertDataException(ve.com.abicelis.remindy.exception.CouldNotInsertDataException)

Aggregations

OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)8 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)6 Calendar (java.util.Calendar)3 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)3 Task (ve.com.abicelis.remindy.model.Task)3 Time (ve.com.abicelis.remindy.model.Time)3 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 RemindyDAO (ve.com.abicelis.remindy.database.RemindyDAO)2 NotificationManager (android.app.NotificationManager)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 Bundle (android.os.Bundle)1 DateFormat (ve.com.abicelis.remindy.enums.DateFormat)1 CouldNotInsertDataException (ve.com.abicelis.remindy.exception.CouldNotInsertDataException)1 CouldNotUpdateDataException (ve.com.abicelis.remindy.exception.CouldNotUpdateDataException)1 LocationBasedReminder (ve.com.abicelis.remindy.model.reminder.LocationBasedReminder)1 TaskTriggerViewModel (ve.com.abicelis.remindy.viewmodel.TaskTriggerViewModel)1