From 087280fddd156732bbcd2655485e8caf9a249998 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Fri, 11 Mar 2016 09:27:23 -0600 Subject: [PATCH] Adjust hide until when due date modified by gtasks --- .../reminders/NotifyAtDeadlineTest.java | 46 ++++----- .../java/org/tasks/makers/TaskMaker.java | 60 ++++++++++++ .../gtasks/sync/GtasksSyncV2ProviderTest.java | 96 +++++++++++++++++++ .../gtasks/sync/GtasksSyncV2Provider.java | 15 ++- .../java/com/todoroo/astrid/data/Task.java | 15 ++- .../repeats/RepeatTaskCompleteListener.java | 8 +- .../receivers/RepeatConfirmationReceiver.java | 6 +- 7 files changed, 195 insertions(+), 51 deletions(-) create mode 100644 src/androidTest/java/org/tasks/makers/TaskMaker.java create mode 100644 src/androidTestGoogleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2ProviderTest.java diff --git a/src/androidTest/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java b/src/androidTest/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java index e28e6bb39..1fe210a8f 100644 --- a/src/androidTest/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java +++ b/src/androidTest/java/com/todoroo/astrid/reminders/NotifyAtDeadlineTest.java @@ -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)); } } diff --git a/src/androidTest/java/org/tasks/makers/TaskMaker.java b/src/androidTest/java/org/tasks/makers/TaskMaker.java new file mode 100644 index 000000000..aa559bdf3 --- /dev/null +++ b/src/androidTest/java/org/tasks/makers/TaskMaker.java @@ -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 DUE_DATE = newProperty(); + public static Property DUE_TIME = newProperty(); + public static Property REMINDER_LAST = newProperty(); + public static Property HIDE_TYPE = newProperty(); + public static Property REMINDERS = newProperty(); + + @SafeVarargs + public static Task newTask(PropertyValue... properties) { + return make(instantiator, properties); + } + + private static final Instantiator instantiator = new Instantiator() { + @Override + public Task instantiate(PropertyLookup 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; + } + }; +} diff --git a/src/androidTestGoogleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2ProviderTest.java b/src/androidTestGoogleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2ProviderTest.java new file mode 100644 index 000000000..effad641d --- /dev/null +++ b/src/androidTestGoogleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2ProviderTest.java @@ -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()); + } +} diff --git a/src/googleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java b/src/googleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java index ce4e12db7..279043813 100644 --- a/src/googleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java +++ b/src/googleplay/java/com/todoroo/astrid/gtasks/sync/GtasksSyncV2Provider.java @@ -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()); } } diff --git a/src/main/java/com/todoroo/astrid/data/Task.java b/src/main/java/com/todoroo/astrid/data/Task.java index 4c21c66bf..226075c37 100644 --- a/src/main/java/com/todoroo/astrid/data/Task.java +++ b/src/main/java/com/todoroo/astrid/data/Task.java @@ -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); } diff --git a/src/main/java/com/todoroo/astrid/repeats/RepeatTaskCompleteListener.java b/src/main/java/com/todoroo/astrid/repeats/RepeatTaskCompleteListener.java index ad6c25abf..964c420e6 100644 --- a/src/main/java/com/todoroo/astrid/repeats/RepeatTaskCompleteListener.java +++ b/src/main/java/com/todoroo/astrid/repeats/RepeatTaskCompleteListener.java @@ -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); diff --git a/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java b/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java index 2125b6320..55f37a909 100644 --- a/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java +++ b/src/main/java/org/tasks/receivers/RepeatConfirmationReceiver.java @@ -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); } })