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 com.todoroo.astrid.data.Task;
import org.tasks.preferences.Preferences; import org.tasks.preferences.Preferences;
import org.tasks.scheduling.AlarmManager;
import org.tasks.time.DateTime; 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 com.todoroo.astrid.reminders.ReminderService.NO_ALARM;
import static org.mockito.Mockito.mock; import static org.tasks.makers.TaskMaker.DUE_DATE;
import static org.tasks.Freeze.freezeAt; import static org.tasks.makers.TaskMaker.DUE_TIME;
import static org.tasks.Freeze.thaw; 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 { public class NotifyAtDeadlineTest extends AndroidTestCase {
@ -20,55 +23,38 @@ public class NotifyAtDeadlineTest extends AndroidTestCase {
@Override @Override
public void setUp() { public void setUp() {
Preferences preferences = new Preferences(getContext(), null); Preferences preferences = new Preferences(getContext(), null);
reminderService = new ReminderService(getContext(), preferences, mock(AlarmManager.class)); reminderService = new ReminderService(getContext(), preferences, null);
freezeAt(new DateTime(2014, 1, 24, 17, 23, 37));
}
@Override
public void tearDown() {
thaw();
} }
public void testNoReminderWhenNoDueDate() { public void testNoReminderWhenNoDueDate() {
Task task = new Task() {{ Task task = newTask(with(REMINDERS, NOTIFY_AT_DEADLINE));
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task)); assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task));
} }
public void testNoReminderWhenNotifyAtDeadlineFlagNotSet() { public void testNoReminderWhenNotifyAtDeadlineFlagNotSet() {
Task task = new Task() {{ Task task = newTask(with(DUE_TIME, new DateTime(2014, 1, 24, 19, 23)));
setDueDate(URGENCY_SPECIFIC_DAY_TIME, new DateTime(2014, 1, 24, 19, 23).getMillis());
}};
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task)); assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task));
} }
public void testScheduleReminderAtDueTime() { public void testScheduleReminderAtDueTime() {
final DateTime dueDate = new DateTime(2014, 1, 24, 19, 23); final DateTime dueDate = new DateTime(2014, 1, 24, 19, 23);
Task task = new Task() {{ Task task = newTask(with(DUE_TIME, dueDate), with(REMINDERS, NOTIFY_AT_DEADLINE));
setDueDate(URGENCY_SPECIFIC_DAY_TIME, dueDate.getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(dueDate.plusSeconds(1).getMillis(), reminderService.calculateNextDueDateReminder(task)); assertEquals(dueDate.plusSeconds(1).getMillis(), reminderService.calculateNextDueDateReminder(task));
} }
public void testScheduleReminderAtDefaultDueTime() { public void testScheduleReminderAtDefaultDueTime() {
final DateTime dueDate = new DateTime(2015, 12, 29, 12, 0); final DateTime dueDate = new DateTime(2015, 12, 29, 12, 0);
Task task = new Task() {{ Task task = newTask(with(DUE_DATE, dueDate), with(REMINDERS, NOTIFY_AT_DEADLINE));
setDueDate(URGENCY_SPECIFIC_DAY, dueDate.getMillis());
setReminderFlags(Task.NOTIFY_AT_DEADLINE);
}};
assertEquals(dueDate.withHourOfDay(18).getMillis(), assertEquals(dueDate.withHourOfDay(18).getMillis(),
reminderService.calculateNextDueDateReminder(task)); reminderService.calculateNextDueDateReminder(task));
} }
public void testNoReminderIfAlreadyRemindedPastDueDate() { public void testNoReminderIfAlreadyRemindedPastDueDate() {
final DateTime dueDate = new DateTime(2015, 12, 29, 19, 23); final DateTime dueDate = new DateTime(2015, 12, 29, 19, 23);
Task task = new Task() {{ Task task = newTask(
setDueDate(URGENCY_SPECIFIC_DAY_TIME, dueDate.getMillis()); with(DUE_TIME, dueDate),
setReminderLast(dueDate.plusSeconds(1).getMillis()); with(REMINDER_LAST, dueDate.plusSeconds(1)),
setReminderFlags(Task.NOTIFY_AT_DEADLINE); with(REMINDERS, NOTIFY_AT_DEADLINE));
}};
assertEquals(NO_ALARM, reminderService.calculateNextDueDateReminder(task)); 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 // merge astrid dates with google dates
if(task.task.isSaved()) { 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) { if (local == null) {
task.task.clearValue(Task.ID); task.task.clearValue(Task.ID);
task.task.clearValue(Task.UUID); task.task.clearValue(Task.UUID);
@ -313,15 +313,20 @@ public class GtasksSyncV2Provider {
} }
} }
private void mergeDates(Task remote, Task local) { static void mergeDates(Task remote, Task local) {
if(remote.hasDueDate() && local.hasDueTime()) { if (remote.hasDueDate() && local.hasDueTime()) {
DateTime oldDate = newDateTime(local.getDueDate()); DateTime oldDate = newDateTime(local.getDueDate());
DateTime newDate = newDateTime(remote.getDueDate()) DateTime newDate = newDateTime(remote.getDueDate())
.withHourOfDay(oldDate.getHourOfDay()) .withHourOfDay(oldDate.getHourOfDay())
.withMinuteOfHour(oldDate.getMinuteOfHour()) .withMinuteOfHour(oldDate.getMinuteOfHour())
.withSecondOfMinute(oldDate.getSecondOfMinute()); .withSecondOfMinute(oldDate.getSecondOfMinute());
long setDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, newDate.getMillis()); local.setDueDateAdjustingHideUntil(
remote.setDueDate(setDate); 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); return getValue(DUE_DATE);
} }
public void setDueDate(int setting, long time) {
setDueDate(createDueDate(setting, time));
}
public void setDueDate(Long dueDate) { public void setDueDate(Long dueDate) {
setValue(DUE_DATE, 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() { public String getRecurrence() {
return getValue(RECURRENCE); return getValue(RECURRENCE);
} }

@ -102,15 +102,9 @@ public class RepeatTaskCompleteListener extends InjectingBroadcastReceiver {
} }
public static void rescheduleTask(GCalHelper gcalHelper, TaskService taskService, Task task, long newDueDate) { 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.setReminderSnooze(0L);
task.setCompletionDate(0L); task.setCompletionDate(0L);
task.setDueDate(newDueDate); task.setDueDateAdjustingHideUntil(newDueDate);
task.setHideUntil(hideUntil);
task.putTransitory(TaskService.TRANS_REPEAT_COMPLETE, true); task.putTransitory(TaskService.TRANS_REPEAT_COMPLETE, true);
gcalHelper.rescheduleRepeatingTask(task); gcalHelper.rescheduleRepeatingTask(task);

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

Loading…
Cancel
Save