Search in sources :

Example 6 with Time

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

the class TaskActionsIntentService method onHandleIntent.

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    if (intent == null || intent.getAction() == null)
        return;
    if (intent.getAction().equals(ACTION_SET_TASK_DONE)) {
        int taskId = intent.getIntExtra(PARAM_TASK_ID, -1);
        if (taskId != -1) {
            RemindyDAO dao = new RemindyDAO(getApplicationContext());
            try {
                Task task = dao.getTask(taskId);
                task.setDoneDate(CalendarUtil.getNewInstanceZeroedCalendar());
                task.setStatus(TaskStatus.DONE);
                dao.updateTask(task);
            } catch (CouldNotGetDataException | CouldNotUpdateDataException e) {
                Toast.makeText(this, getResources().getString(R.string.task_actions_service_could_not_set_done), Toast.LENGTH_SHORT).show();
            }
            //Dismiss the notification
            NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.cancel(taskId);
        }
    } else if (intent.getAction().equals(ACTION_POSTPONE_TASK)) {
        int taskId = intent.getIntExtra(PARAM_TASK_ID, -1);
        if (taskId != -1) {
            RemindyDAO dao = new RemindyDAO(getApplicationContext());
            try {
                Task task = dao.getTask(taskId);
                Time time;
                switch(task.getReminderType()) {
                    case REPEATING:
                        time = ((RepeatingReminder) task.getReminder()).getTime();
                        break;
                    case ONE_TIME:
                        time = ((OneTimeReminder) task.getReminder()).getTime();
                        break;
                    default:
                        //TODO: Show some kind of error?
                        return;
                }
                int timeInMinutes = time.getTimeInMinutes();
                timeInMinutes += POSTPONE_MINUTES;
                //Set max per day to 24:59:00
                timeInMinutes = (timeInMinutes >= 1440 ? 1339 : timeInMinutes);
                time.setTimeInMinutes(timeInMinutes);
                dao.updateTask(task);
                //Remove task from triggeredTasks list
                SharedPreferenceUtil.removeIdFromTriggeredTasks(getApplicationContext(), task.getId());
                //Update alarms
                AlarmManagerUtil.updateAlarms(getApplicationContext());
            } catch (CouldNotGetDataException | CouldNotUpdateDataException e) {
                Toast.makeText(this, getResources().getString(R.string.task_actions_service_could_not_set_done), Toast.LENGTH_SHORT).show();
            }
            //Dismiss the notification
            NotificationManager mNotifyMgr = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
            mNotifyMgr.cancel(taskId);
        }
    }
}
Also used : CouldNotUpdateDataException(ve.com.abicelis.remindy.exception.CouldNotUpdateDataException) Task(ve.com.abicelis.remindy.model.Task) CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) NotificationManager(android.app.NotificationManager) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) Time(ve.com.abicelis.remindy.model.Time) RemindyDAO(ve.com.abicelis.remindy.database.RemindyDAO)

Example 7 with Time

use of ve.com.abicelis.remindy.model.Time 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)

Aggregations

Time (ve.com.abicelis.remindy.model.Time)7 Calendar (java.util.Calendar)6 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)3 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)3 View (android.view.View)2 CalendarDatePickerDialogFragment (com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment)2 RadialTimePickerDialogFragment (com.codetroopers.betterpickers.radialtimepicker.RadialTimePickerDialogFragment)2 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)2 Task (ve.com.abicelis.remindy.model.Task)2 NotificationManager (android.app.NotificationManager)1 DialogInterface (android.content.DialogInterface)1 Cursor (android.database.Cursor)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 AdapterView (android.widget.AdapterView)1 TextView (android.widget.TextView)1 OnDialogDismissListener (com.codetroopers.betterpickers.OnDialogDismissListener)1 MonthAdapter (com.codetroopers.betterpickers.calendardatepicker.MonthAdapter)1 NumberPickerBuilder (com.codetroopers.betterpickers.numberpicker.NumberPickerBuilder)1 BigDecimal (java.math.BigDecimal)1