From dcd6217f6c3de7c58df4c4b31c3d761f35d18459 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 1 Jun 2020 14:25:48 -0500 Subject: [PATCH] Change priority or due date with drag and drop --- .../com/todoroo/astrid/adapter/TaskAdapter.kt | 47 +++++++++++++++++-- .../astrid/adapter/TaskAdapterDataSource.kt | 4 ++ .../java/org/tasks/data/TaskContainer.java | 4 ++ .../main/java/org/tasks/date/DateTimeUtils.kt | 2 + .../tasklist/DragAndDropRecyclerAdapter.kt | 5 ++ .../org/tasks/tasklist/SectionedDataSource.kt | 6 +++ 6 files changed, 63 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt index 88dcfa0ec..0930d3e23 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt @@ -5,12 +5,15 @@ */ package com.todoroo.astrid.adapter +import com.todoroo.astrid.core.SortHelper.SORT_DUE +import com.todoroo.astrid.core.SortHelper.SORT_IMPORTANCE import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.data.Task import org.tasks.BuildConfig import org.tasks.LocalBroadcastManager import org.tasks.data.* import org.tasks.date.DateTimeUtils.toAppleEpoch +import org.tasks.date.DateTimeUtils.toDateTime import java.util.* import kotlin.collections.HashSet @@ -127,6 +130,9 @@ open class TaskAdapter( val task = getTask(from) val newParent = findParent(indent, to) if (newParent?.id ?: 0 == task.parent) { + if (indent == 0) { + changeSortGroup(task, if (from < to) to - 1 else to) + } return } else if (newParent != null) { when { @@ -141,7 +147,10 @@ open class TaskAdapter( } } when { - newParent == null -> moveToTopLevel(task) + newParent == null -> { + moveToTopLevel(task) + changeSortGroup(task, if (from < to) to - 1 else to) + } newParent.isGoogleTask -> changeGoogleTaskParent(task, newParent) newParent.isCaldavTask -> changeCaldavParent(task, newParent) else -> changeLocalParent(task, newParent) @@ -189,10 +198,38 @@ open class TaskAdapter( return null } - private fun moveToTopLevel(task: TaskContainer) = when { - task.isGoogleTask -> changeGoogleTaskParent(task, null) - task.isCaldavTask -> changeCaldavParent(task, null) - else -> changeLocalParent(task, null) + private fun changeSortGroup(task: TaskContainer, pos: Int) { + when(dataSource.sortMode) { + SORT_IMPORTANCE -> { + val newPriority = dataSource.nearestHeader(if (pos == 0) 1 else pos).toInt() + if (newPriority != task.priority) { + val t = task.getTask() + t.priority = newPriority + taskDao.save(t) + } + } + SORT_DUE -> applyDate(task.task, dataSource.nearestHeader(if (pos == 0) 1 else pos)) + } + } + + private fun applyDate(task: Task, date: Long) { + val original = task.dueDate + task.dueDate = if (date == 0L) { + 0L + } else { + date.toDateTime().withMillisOfDay(task.dueDate.toDateTime().millisOfDay).millis + } + if (original != task.dueDate) { + taskDao.save(task) + } + } + + private fun moveToTopLevel(task: TaskContainer) { + when { + task.isGoogleTask -> changeGoogleTaskParent(task, null) + task.isCaldavTask -> changeCaldavParent(task, null) + else -> changeLocalParent(task, null) + } } private fun changeLocalParent(task: TaskContainer, newParent: TaskContainer?) { diff --git a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt index 2c0c5c634..45d622285 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt @@ -8,4 +8,8 @@ interface TaskAdapterDataSource { fun getTaskCount(): Int fun isHeader(position: Int): Boolean = false + + fun nearestHeader(position: Int): Long = -1 + + val sortMode: Int get() = -1 } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/data/TaskContainer.java b/app/src/main/java/org/tasks/data/TaskContainer.java index 194c34987..4970f1af2 100644 --- a/app/src/main/java/org/tasks/data/TaskContainer.java +++ b/app/src/main/java/org/tasks/data/TaskContainer.java @@ -231,4 +231,8 @@ public class TaskContainer { public long getCaldavSortOrder() { return indent == 0 ? primarySort : secondarySort; } + + public int getPriority() { + return task.getPriority(); + } } diff --git a/app/src/main/java/org/tasks/date/DateTimeUtils.kt b/app/src/main/java/org/tasks/date/DateTimeUtils.kt index 761794bff..477abb5ff 100644 --- a/app/src/main/java/org/tasks/date/DateTimeUtils.kt +++ b/app/src/main/java/org/tasks/date/DateTimeUtils.kt @@ -23,4 +23,6 @@ object DateTimeUtils { fun newDateTime(timestamp: Long): DateTime = DateTime(timestamp) fun Long.toAppleEpoch(): Long = DateTime(this).toAppleEpoch() + + fun Long.toDateTime(): DateTime = DateTime(this) } \ No newline at end of file diff --git a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt index 140d26c9a..3c58ea3dc 100644 --- a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt @@ -51,6 +51,9 @@ class DragAndDropRecyclerAdapter( } } + override val sortMode: Int + get() = list.sortMode + override fun getItemViewType(position: Int) = if (list.isHeader(position)) 1 else 0 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = if (viewType == 1) { @@ -68,6 +71,8 @@ class DragAndDropRecyclerAdapter( override fun isHeader(position: Int): Boolean = list.isHeader(position) + override fun nearestHeader(position: Int) = list.getNearestHeader(position) + override fun getItem(position: Int) = list.getItem(position) override fun submitList(list: List) { diff --git a/app/src/main/java/org/tasks/tasklist/SectionedDataSource.kt b/app/src/main/java/org/tasks/tasklist/SectionedDataSource.kt index 99de58f6d..c62295789 100644 --- a/app/src/main/java/org/tasks/tasklist/SectionedDataSource.kt +++ b/app/src/main/java/org/tasks/tasklist/SectionedDataSource.kt @@ -108,4 +108,10 @@ class SectionedDataSource constructor(tasks: List, disableHeaders val new = AdapterSection(newFirstPosition, old.value, newSectionedPosition, old.collapsed) sections.append(new.sectionedPosition, new) } + + tailrec fun getNearestHeader(sectionedPosition: Int): Long = if (isHeader(sectionedPosition)) { + getHeaderValue(sectionedPosition) + } else { + getNearestHeader(sectionedPosition - 1) + } } \ No newline at end of file