Adjust hide until when due date modified by gtasks

pull/384/head
Alex Baker 9 years ago
parent 7e40bef31f
commit 087280fddd

@ -5,13 +5,16 @@ import android.test.AndroidTestCase;
import com.todoroo.astrid.data.Task;
import org.tasks.preferences.Preferences;
import org.tasks.scheduling.AlarmManager;
import org.tasks.time.DateTime;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static com.todoroo.astrid.data.Task.NOTIFY_AT_DEADLINE;
import static com.todoroo.astrid.reminders.ReminderService.NO_ALARM;
import static org.mockito.Mockito.mock;
import static org.tasks.Freeze.freezeAt;
import static org.tasks.Freeze.thaw;
import static org.tasks.makers.TaskMaker.DUE_DATE;
import static org.tasks.makers.TaskMaker.DUE_TIME;
import static org.tasks.makers.TaskMaker.REMINDERS;
import static org.tasks.makers.TaskMaker.REMINDER_LAST;
import static org.tasks.makers.TaskMaker.newTask;
public class NotifyAtDeadlineTest extends AndroidTestCase {
@ -20,55 +23,38 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
@Override
public void setUp() {
Preferences preferences = new Preferences(getContext(), null);
reminderService = new ReminderService(getContext(), preferences, mock(AlarmManager.class));
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
}
@Override
public void tearDown() {
thaw();
reminderService = new ReminderService(getContext(), preferences, null);
}
public void testNoReminderWhenNoDueDate() {
Task task = new Task() {{
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
Task task = newTask(with(REMINDERS, NOTIFY_AT_DEADLINE));
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task));
}
public void testNoReminderWhenNotifyAtDeadlineFlagNotSet() {
Task task = new Task() {{
setDueDate(URGENCY_SPECIFIC_DAY_TIME, new DateTime(2014, 1, 24, 19, 23).getMillis());
}};
Task task = newTask(with(DUE_TIME, new DateTime(2014, 1, 24, 19, 23)));
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task));
}
public void testScheduleReminderAtDueTime() {
final DateTime dueDate = new DateTime(2014, 1, 24, 19, 23);
Task task = new Task() {{
setDueDate(URGENCY_SPECIFIC_DAY_TIME, dueDate.getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
Task task = newTask(with(DUE_TIME, dueDate), with(REMINDERS, NOTIFY_AT_DEADLINE));
assertEquals(dueDate.plusSeconds(1).getMillis(), reminderService.calculateNextDueDateReminder(task));
}
public void testScheduleReminderAtDefaultDueTime() {
final DateTime dueDate = new DateTime(2015, 12, 29, 12, 0);
Task task = new Task() {{
setDueDate(URGENCY_SPECIFIC_DAY, dueDate.getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
Task task = newTask(with(DUE_DATE, dueDate), with(REMINDERS, NOTIFY_AT_DEADLINE));
assertEquals(dueDate.withHourOfDay(18).getMillis(),
reminderService.calculateNextDueDateReminder(task));
}
public void testNoReminderIfAlreadyRemindedPastDueDate() {
final DateTime dueDate = new DateTime(2015, 12, 29, 19, 23);
Task task = new Task() {{
setDueDate(URGENCY_SPECIFIC_DAY_TIME, dueDate.getMillis());
setReminderLast(dueDate.plusSeconds(1).getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
Task task = newTask(
with(DUE_TIME, dueDate),
with(REMINDER_LAST, dueDate.plusSeconds(1)),
with(REMINDERS, NOTIFY_AT_DEADLINE));
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task));
}
}

@ -0,0 +1,60 @@
package org.tasks.makers;
import com.natpryce.makeiteasy.Instantiator;
import com.natpryce.makeiteasy.Property;
import com.natpryce.makeiteasy.PropertyLookup;
import com.natpryce.makeiteasy.PropertyValue;
import com.todoroo.astrid.data.Task;
import org.tasks.time.DateTime;
import static com.natpryce.makeiteasy.Property.newProperty;
import static org.tasks.makers.Maker.make;
public class TaskMaker {
public static Property<Task, DateTime> DUE_DATE = newProperty();
public static Property<Task, DateTime> DUE_TIME = newProperty();
public static Property<Task, DateTime> REMINDER_LAST = newProperty();
public static Property<Task, Integer> HIDE_TYPE = newProperty();
public static Property<Task, Integer> REMINDERS = newProperty();
@SafeVarargs
public static Task newTask(PropertyValue<? super Task, ?>... properties) {
return make(instantiator, properties);
}
private static final Instantiator<Task> instantiator = new Instantiator<Task>() {
@Override
public Task instantiate(PropertyLookup<Task> lookup) {
Task task = new Task();
DateTime dueDate = lookup.valueOf(DUE_DATE, (DateTime) null);
if (dueDate != null) {
task.setDueDate(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate.getMillis()));
}
DateTime dueTime = lookup.valueOf(DUE_TIME, (DateTime) null);
if (dueTime != null) {
task.setDueDate(Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, dueTime.getMillis()));
}
int hideType = lookup.valueOf(HIDE_TYPE, -1);
if (hideType >= 0) {
task.setHideUntil(task.createHideUntil(hideType, 0));
}
int reminderFlags = lookup.valueOf(REMINDERS, -1);
if (reminderFlags >= 0) {
task.setReminderFlags(reminderFlags);
}
DateTime reminderLast = lookup.valueOf(REMINDER_LAST, (DateTime) null);
if (reminderLast != null) {
task.setReminderLast(reminderLast.getMillis());
}
return task;
}
};
}

@ -0,0 +1,96 @@
package com.todoroo.astrid.gtasks.sync;
import android.test.AndroidTestCase;
import com.todoroo.astrid.data.Task;
import org.tasks.time.DateTime;
import static com.natpryce.makeiteasy.MakeItEasy.with;
import static com.todoroo.astrid.data.Task.HIDE_UNTIL_DUE;
import static com.todoroo.astrid.data.Task.HIDE_UNTIL_DUE_TIME;
import static com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider.mergeDates;
import static org.tasks.makers.TaskMaker.DUE_DATE;
import static org.tasks.makers.TaskMaker.DUE_TIME;
import static org.tasks.makers.TaskMaker.HIDE_TYPE;
import static org.tasks.makers.TaskMaker.newTask;
public class GtasksSyncV2ProviderTest extends AndroidTestCase {
public void testMergeDate() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 11)));
Task local = newTask(with(DUE_DATE, new DateTime(2016, 3, 12)));
mergeDates(remote, local);
assertEquals(new DateTime(2016, 3, 11, 12, 0).getMillis(), remote.getDueDate().longValue());
}
public void testMergeTime() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 11)));
Task local = newTask(with(DUE_TIME, new DateTime(2016, 3, 11, 13, 30)));
mergeDates(remote, local);
assertEquals(
new DateTime(2016, 3, 11, 13, 30, 1).getMillis(),
remote.getDueDate().longValue());
}
public void testDueDateAdjustHideBackwards() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 11)));
Task local = newTask(with(DUE_DATE, new DateTime(2016, 3, 12)), with(HIDE_TYPE, HIDE_UNTIL_DUE));
mergeDates(remote, local);
assertEquals(new DateTime(2016, 3, 11).getMillis(), remote.getHideUntil().longValue());
}
public void testDueDateAdjustHideForwards() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 14)));
Task local = newTask(with(DUE_DATE, new DateTime(2016, 3, 12)), with(HIDE_TYPE, HIDE_UNTIL_DUE));
mergeDates(remote, local);
assertEquals(new DateTime(2016, 3, 14).getMillis(), remote.getHideUntil().longValue());
}
public void testDueTimeAdjustHideBackwards() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 11)));
Task local = newTask(with(DUE_TIME, new DateTime(2016, 3, 12, 13, 30)),
with(HIDE_TYPE, HIDE_UNTIL_DUE_TIME));
mergeDates(remote, local);
assertEquals(new DateTime(2016, 3, 11, 13, 30, 1).getMillis(), remote.getHideUntil().longValue());
}
public void testDueTimeAdjustTimeForwards() {
Task remote = newTask(with(DUE_DATE, new DateTime(2016, 3, 14)));
Task local = newTask(with(DUE_TIME, new DateTime(2016, 3, 12, 13, 30)),
with(HIDE_TYPE, HIDE_UNTIL_DUE_TIME));
mergeDates(remote, local);
assertEquals(new DateTime(2016, 3, 14, 13, 30, 1).getMillis(), remote.getHideUntil().longValue());
}
public void testDueDateClearHide() {
Task remote = newTask();
Task local = newTask(with(DUE_DATE, new DateTime(2016, 3, 12)), with(HIDE_TYPE, HIDE_UNTIL_DUE));
mergeDates(remote, local);
assertEquals(0, remote.getHideUntil().longValue());
}
public void testDueTimeClearHide() {
Task remote = newTask();
Task local = newTask(with(DUE_TIME, new DateTime(2016, 3, 12, 13, 30)),
with(HIDE_TYPE, HIDE_UNTIL_DUE_TIME));
mergeDates(remote, local);
assertEquals(0, remote.getHideUntil().longValue());
}
}

@ -295,7 +295,7 @@ public class GtasksSyncV2Provider {
// merge astrid dates with google dates
if(task.task.isSaved()) {
Task local = taskService.fetchById(task.task.getId(), Task.DUE_DATE, Task.COMPLETION_DATE);
Task local = taskService.fetchById(task.task.getId(), Task.PROPERTIES);
if (local == null) {
task.task.clearValue(Task.ID);
task.task.clearValue(Task.UUID);
@ -313,15 +313,20 @@ public class GtasksSyncV2Provider {
}
}
private void mergeDates(Task remote, Task local) {
if(remote.hasDueDate() && local.hasDueTime()) {
static void mergeDates(Task remote, Task local) {
if (remote.hasDueDate() && local.hasDueTime()) {
DateTime oldDate = newDateTime(local.getDueDate());
DateTime newDate = newDateTime(remote.getDueDate())
.withHourOfDay(oldDate.getHourOfDay())
.withMinuteOfHour(oldDate.getMinuteOfHour())
.withSecondOfMinute(oldDate.getSecondOfMinute());
long setDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, newDate.getMillis());
remote.setDueDate(setDate);
local.setDueDateAdjustingHideUntil(
Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, newDate.getMillis()));
} else {
local.setDueDateAdjustingHideUntil(remote.getDueDate());
}
remote.setHideUntil(local.getHideUntil());
remote.setDueDate(local.getDueDate());
}
}

@ -395,14 +395,21 @@ public class Task extends RemoteModel {
return getValue(DUE_DATE);
}
public void setDueDate(int setting, long time) {
setDueDate(createDueDate(setting, time));
}
public void setDueDate(Long dueDate) {
setValue(DUE_DATE, dueDate);
}
public void setDueDateAdjustingHideUntil(Long dueDate) {
long oldDueDate = getValue(DUE_DATE);
if (oldDueDate > 0) {
long hideUntil = getValue(HIDE_UNTIL);
if (hideUntil > 0) {
setHideUntil(dueDate > 0 ? hideUntil + dueDate - oldDueDate : 0);
}
}
setDueDate(dueDate);
}
public String getRecurrence() {
return getValue(RECURRENCE);
}

@ -102,15 +102,9 @@ public class RepeatTaskCompleteListener extends InjectingBroadcastReceiver {
}
public static void rescheduleTask(GCalHelper gcalHelper, TaskService taskService, Task task, long newDueDate) {
long hideUntil = task.getHideUntil();
if(hideUntil > 0 && task.getDueDate() > 0) {
hideUntil += newDueDate - task.getDueDate();
}
task.setReminderSnooze(0L);
task.setCompletionDate(0L);
task.setDueDate(newDueDate);
task.setHideUntil(hideUntil);
task.setDueDateAdjustingHideUntil(newDueDate);
task.putTransitory(TaskService.TRANS_REPEAT_COMPLETE, true);
gcalHelper.rescheduleRepeatingTask(task);

@ -78,12 +78,8 @@ public class RepeatConfirmationReceiver extends BroadcastReceiver {
.setAction(R.string.DLG_undo, new View.OnClickListener() {
@Override
public void onClick(View v) {
task.setDueDate(oldDueDate);
task.setDueDateAdjustingHideUntil(oldDueDate);
task.setCompletionDate(0L);
long hideUntil = task.getHideUntil();
if (hideUntil > 0) {
task.setHideUntil(hideUntil - (newDueDate - oldDueDate));
}
taskService.save(task);
}
})

Loading…
Cancel
Save