Replace some setter usage with constructors

pull/2672/head
Alex Baker 7 months ago
parent 8058414137
commit 6c031925ba

@ -12,9 +12,10 @@ open class NewSyncTestCase : InjectingTestCase() {
@Inject lateinit var tagDataDao: TagDataDao @Inject lateinit var tagDataDao: TagDataDao
suspend fun createTask(): Task { suspend fun createTask(): Task {
val task = Task() val task = Task(
task.title = SYNC_TASK_TITLE title = SYNC_TASK_TITLE,
task.priority = SYNC_TASK_IMPORTANCE priority = SYNC_TASK_IMPORTANCE,
)
taskDao.createNew(task) taskDao.createNew(task)
return task return task
} }

@ -210,9 +210,7 @@ open class TaskAdapter(
SORT_IMPORTANCE -> { SORT_IMPORTANCE -> {
val newPriority = dataSource.nearestHeader(if (pos == 0) 1 else pos).toInt() val newPriority = dataSource.nearestHeader(if (pos == 0) 1 else pos).toInt()
if (newPriority != task.priority) { if (newPriority != task.priority) {
val t = task.task taskDao.save(task.task.copy(priority = newPriority))
t.priority = newPriority
taskDao.save(t)
} }
} }
SORT_LIST -> taskMover.move(task.id, dataSource.nearestHeader(if (pos == 0) 1 else pos)) SORT_LIST -> taskMover.move(task.id, dataSource.nearestHeader(if (pos == 0) 1 else pos))

@ -135,14 +135,13 @@ class TaskCreator @Inject constructor(
* base task model. * base task model.
*/ */
internal suspend fun create(values: Map<String, Any>?, title: String?): Task { internal suspend fun create(values: Map<String, Any>?, title: String?): Task {
val task = Task() val task = Task(
task.creationDate = DateUtilities.now() title = title?.trim { it <= ' ' },
task.modificationDate = DateUtilities.now() creationDate = DateUtilities.now(),
if (title != null) { modificationDate = DateUtilities.now(),
task.title = title.trim { it <= ' ' } remoteId = UUIDHelper.newUUID(),
} priority = preferences.defaultPriority,
task.uuid = UUIDHelper.newUUID() )
task.priority = preferences.defaultPriority
preferences.getStringValue(R.string.p_default_recurrence) preferences.getStringValue(R.string.p_default_recurrence)
?.takeIf { it.isNotBlank() } ?.takeIf { it.isNotBlank() }
?.let { ?.let {

@ -3,6 +3,7 @@ package com.todoroo.astrid.service
import com.todoroo.andlib.utility.DateUtilities import com.todoroo.andlib.utility.DateUtilities
import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.dao.TaskDao
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import com.todoroo.astrid.data.Task.Companion.NO_ID
import com.todoroo.astrid.gcal.GCalHelper import com.todoroo.astrid.gcal.GCalHelper
import org.tasks.LocalBroadcastManager import org.tasks.LocalBroadcastManager
import org.tasks.data.Alarm import org.tasks.data.Alarm
@ -47,25 +48,25 @@ class TaskDuplicator @Inject constructor(
.also { localBroadcastManager.broadcastRefresh() } .also { localBroadcastManager.broadcastRefresh() }
} }
private suspend fun clone(clone: Task, parentId: Long): Task { private suspend fun clone(task: Task, parentId: Long): Task {
val originalId = clone.id val clone = task.copy(
with(clone) { id = NO_ID,
creationDate = DateUtilities.now() creationDate = DateUtilities.now(),
modificationDate = DateUtilities.now() modificationDate = DateUtilities.now(),
reminderLast = 0 reminderLast = 0,
completionDate = 0L completionDate = 0L,
calendarURI = "" calendarURI = "",
parent = parentId parent = parentId,
uuid = Task.NO_UUID remoteId = Task.NO_UUID,
suppressSync() )
suppressRefresh() clone.suppressSync()
} clone.suppressRefresh()
val newId = taskDao.createNew(clone) val newId = taskDao.createNew(clone)
val tags = tagDataDao.getTagDataForTask(originalId) val tags = tagDataDao.getTagDataForTask(task.id)
if (tags.isNotEmpty()) { if (tags.isNotEmpty()) {
tagDao.insert(tags.map { Tag(clone, it) }) tagDao.insert(tags.map { Tag(clone, it) })
} }
val googleTask = googleTaskDao.getByTaskId(originalId) val googleTask = googleTaskDao.getByTaskId(task.id)
val addToTop = preferences.addTasksToTop() val addToTop = preferences.addTasksToTop()
if (googleTask != null) { if (googleTask != null) {
googleTaskDao.insertAndShift( googleTaskDao.insertAndShift(
@ -78,7 +79,7 @@ class TaskDuplicator @Inject constructor(
addToTop addToTop
) )
} }
val caldavTask = caldavDao.getTask(originalId) val caldavTask = caldavDao.getTask(task.id)
if (caldavTask != null) { if (caldavTask != null) {
val newDavTask = CaldavTask( val newDavTask = CaldavTask(
task = clone.id, task = clone.id,
@ -90,18 +91,18 @@ class TaskDuplicator @Inject constructor(
} }
caldavDao.insert(clone, newDavTask, addToTop) caldavDao.insert(clone, newDavTask, addToTop)
} }
for (g in locationDao.getGeofencesForTask(originalId)) { for (g in locationDao.getGeofencesForTask(task.id)) {
locationDao.insert( locationDao.insert(
Geofence(clone.id, g.place, g.isArrival, g.isDeparture)) Geofence(clone.id, g.place, g.isArrival, g.isDeparture))
} }
val alarms = alarmDao.getAlarms(originalId) val alarms = alarmDao.getAlarms(task.id)
if (alarms.isNotEmpty()) { if (alarms.isNotEmpty()) {
alarmDao.insert(alarms.map { Alarm(clone.id, it.time, it.type) }) alarmDao.insert(alarms.map { Alarm(clone.id, it.time, it.type) })
} }
gcalHelper.createTaskEventIfEnabled(clone) gcalHelper.createTaskEventIfEnabled(clone)
taskDao.save(clone, null) // TODO: delete me taskDao.save(clone, null) // TODO: delete me
taskAttachmentDao taskAttachmentDao
.getAttachmentsForTask(originalId) .getAttachmentsForTask(task.id)
.map { .map {
Attachment( Attachment(
task = clone.id, task = clone.id,
@ -110,7 +111,7 @@ class TaskDuplicator @Inject constructor(
) )
} }
.let { taskAttachmentDao.insert(it) } .let { taskAttachmentDao.insert(it) }
getDirectChildren(originalId).forEach { subtask -> getDirectChildren(task.id).forEach { subtask ->
clone(subtask, newId) clone(subtask, newId)
} }
return clone return clone

@ -69,8 +69,7 @@ class PriorityPicker : DialogFragment() {
taskDao taskDao
.fetch(taskIds.toList()) .fetch(taskIds.toList())
.forEach { .forEach {
it.priority = priorityPickerViewModel.priority.value taskDao.save(it.copy(priority = priorityPickerViewModel.priority.value))
taskDao.save(it)
} }
} }
dismiss() dismiss()

@ -145,18 +145,20 @@ class AlarmCalculatorTest {
@Test @Test
fun scheduleRelativeAfterDue() { fun scheduleRelativeAfterDue() {
val alarm = alarmCalculator.toAlarmEntry( freezeAt(DateTime(2023, 11, 3, 17, 13)) {
newTask(with(DUE_DATE, now)), val alarm = alarmCalculator.toAlarmEntry(
Alarm(0L, DAYS.toMillis(1), TYPE_REL_END) newTask(with(DUE_DATE, newDateTime())),
) Alarm(0L, DAYS.toMillis(1), TYPE_REL_END)
)
assertEquals( assertEquals(
newAlarmEntry( newAlarmEntry(
with(TIME, now.plusDays(1).startOfDay().withHourOfDay(13)), with(TIME, DateTime(2023, 11, 4, 13, 0, 0)),
with(TYPE, TYPE_REL_END) with(TYPE, TYPE_REL_END)
), ),
alarm alarm
) )
}
} }
@Test @Test
@ -177,18 +179,20 @@ class AlarmCalculatorTest {
@Test @Test
fun scheduleRelativeAfterStart() = runBlocking { fun scheduleRelativeAfterStart() = runBlocking {
val alarm = alarmCalculator.toAlarmEntry( freezeAt(DateTime(2023, 11, 3, 17, 13)) {
newTask(with(DUE_DATE, now), with(HIDE_TYPE, HIDE_UNTIL_DUE)), val alarm = alarmCalculator.toAlarmEntry(
Alarm(0, DAYS.toMillis(1), TYPE_REL_START) newTask(with(DUE_DATE, newDateTime()), with(HIDE_TYPE, HIDE_UNTIL_DUE)),
) Alarm(0, DAYS.toMillis(1), TYPE_REL_START)
)
assertEquals( assertEquals(
newAlarmEntry( newAlarmEntry(
with(TIME, now.plusDays(1).startOfDay().withHourOfDay(13)), with(TIME, DateTime(2023, 11, 4, 13, 0)),
with(TYPE, TYPE_REL_START) with(TYPE, TYPE_REL_START)
), ),
alarm alarm
) )
}
} }
@Test @Test

@ -8,7 +8,6 @@ import com.natpryce.makeiteasy.PropertyValue
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import com.todoroo.astrid.data.Task.Companion.HIDE_UNTIL_SPECIFIC_DAY import com.todoroo.astrid.data.Task.Companion.HIDE_UNTIL_SPECIFIC_DAY
import com.todoroo.astrid.data.Task.Companion.NO_UUID import com.todoroo.astrid.data.Task.Companion.NO_UUID
import org.tasks.Strings
import org.tasks.date.DateTimeUtils import org.tasks.date.DateTimeUtils
import org.tasks.makers.Maker.make import org.tasks.makers.Maker.make
import org.tasks.repeats.RecurrenceUtils.newRecur import org.tasks.repeats.RecurrenceUtils.newRecur
@ -21,7 +20,6 @@ object TaskMaker {
val START_DATE: Property<Task, DateTime?> = newProperty() val START_DATE: Property<Task, DateTime?> = newProperty()
val REMINDER_LAST: Property<Task, DateTime?> = newProperty() val REMINDER_LAST: Property<Task, DateTime?> = newProperty()
val HIDE_TYPE: Property<Task, Int> = newProperty() val HIDE_TYPE: Property<Task, Int> = newProperty()
val REMINDERS: Property<Task, Int> = newProperty()
val MODIFICATION_TIME: Property<Task, DateTime> = newProperty() val MODIFICATION_TIME: Property<Task, DateTime> = newProperty()
val CREATION_TIME: Property<Task, DateTime> = newProperty() val CREATION_TIME: Property<Task, DateTime> = newProperty()
val COMPLETION_TIME: Property<Task, DateTime> = newProperty() val COMPLETION_TIME: Property<Task, DateTime> = newProperty()
@ -37,35 +35,32 @@ object TaskMaker {
val ORDER: Property<Task, Long> = newProperty() val ORDER: Property<Task, Long> = newProperty()
private val instantiator = Instantiator { lookup: PropertyLookup<Task> -> private val instantiator = Instantiator { lookup: PropertyLookup<Task> ->
val task = Task() val creationTime = lookup.valueOf(CREATION_TIME, DateTimeUtils.newDateTime())
val title = lookup.valueOf(TITLE, null as String?) val task = Task(
if (!Strings.isNullOrEmpty(title)) { id = lookup.valueOf(ID, Task.NO_ID),
task.title = title!! title = lookup.valueOf(TITLE, null as String?),
} priority = lookup.valueOf(PRIORITY, Task.Priority.NONE),
val id = lookup.valueOf(ID, Task.NO_ID) dueDate = lookup.valueOf(DUE_DATE, null as DateTime?)
if (id != Task.NO_ID) { ?.let { Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, it.millis) }
task.id = id ?: lookup.valueOf(DUE_TIME, null as DateTime?)
} ?.let { Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, it.millis) }
val priority = lookup.valueOf(PRIORITY, -1) ?: 0L,
if (priority >= 0) { completionDate = lookup.valueOf(COMPLETION_TIME, null as DateTime?)?.millis ?: 0L,
task.priority = priority deletionDate = lookup.valueOf(DELETION_TIME, null as DateTime?)?.millis ?: 0L,
} reminderLast = lookup.valueOf(REMINDER_LAST, null as DateTime?)?.millis ?: 0L,
val dueDate = lookup.valueOf(DUE_DATE, null as DateTime?) recurrence = lookup.valueOf(RECUR, null as String?)?.let { newRecur(it).toString() },
if (dueDate != null) { repeatFrom = if (lookup.valueOf(AFTER_COMPLETE, false))
task.dueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY, dueDate.millis) Task.RepeatFrom.COMPLETION_DATE
} else
val dueTime = lookup.valueOf(DUE_TIME, null as DateTime?) Task.RepeatFrom.DUE_DATE,
if (dueTime != null) { notes = lookup.valueOf(DESCRIPTION, null as String?),
task.dueDate = Task.createDueDate(Task.URGENCY_SPECIFIC_DAY_TIME, dueTime.millis) isCollapsed = lookup.valueOf(COLLAPSED, false),
} remoteId = lookup.valueOf(UUID, NO_UUID),
val completionTime = lookup.valueOf(COMPLETION_TIME, null as DateTime?) parent = lookup.valueOf(PARENT, 0L),
if (completionTime != null) { order = lookup.valueOf(ORDER, null as Long?),
task.completionDate = completionTime.millis creationDate = creationTime.millis,
} modificationDate = lookup.valueOf(MODIFICATION_TIME, creationTime).millis,
val deletedTime = lookup.valueOf(DELETION_TIME, null as DateTime?) )
if (deletedTime != null) {
task.deletionDate = deletedTime.millis
}
lookup.valueOf(START_DATE, null as DateTime?)?.let { lookup.valueOf(START_DATE, null as DateTime?)?.let {
task.hideUntil = task.createHideUntil(HIDE_UNTIL_SPECIFIC_DAY, it.millis) task.hideUntil = task.createHideUntil(HIDE_UNTIL_SPECIFIC_DAY, it.millis)
} }
@ -73,30 +68,6 @@ object TaskMaker {
if (hideType >= 0) { if (hideType >= 0) {
task.hideUntil = task.createHideUntil(hideType, 0) task.hideUntil = task.createHideUntil(hideType, 0)
} }
val reminderFlags = lookup.valueOf(REMINDERS, -1)
if (reminderFlags >= 0) {
task.ringFlags = reminderFlags
}
val reminderLast = lookup.valueOf(REMINDER_LAST, null as DateTime?)
if (reminderLast != null) {
task.reminderLast = reminderLast.millis
}
lookup.valueOf(RECUR, null as String?)?.let {
task.setRecurrence(newRecur(it))
}
task.repeatFrom = if (lookup.valueOf(AFTER_COMPLETE, false)) {
Task.RepeatFrom.COMPLETION_DATE
} else {
Task.RepeatFrom.DUE_DATE
}
task.notes = lookup.valueOf(DESCRIPTION, null as String?)
task.isCollapsed = lookup.valueOf(COLLAPSED, false)
task.uuid = lookup.valueOf(UUID, NO_UUID)
val creationTime = lookup.valueOf(CREATION_TIME, DateTimeUtils.newDateTime())
task.creationDate = creationTime.millis
task.modificationDate = lookup.valueOf(MODIFICATION_TIME, creationTime).millis
task.parent = lookup.valueOf(PARENT, 0L)
task.order = lookup.valueOf(ORDER, null as Long?)
task task
} }

Loading…
Cancel
Save