Change priority or due date with drag and drop

pull/998/head
Alex Baker 4 years ago
parent 5dcc6a1e56
commit dcd6217f6c

@ -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?) {

@ -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
}

@ -231,4 +231,8 @@ public class TaskContainer {
public long getCaldavSortOrder() {
return indent == 0 ? primarySort : secondarySort;
}
public int getPriority() {
return task.getPriority();
}
}

@ -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)
}

@ -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<TaskContainer>) {

@ -108,4 +108,10 @@ class SectionedDataSource constructor(tasks: List<TaskContainer>, 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)
}
}
Loading…
Cancel
Save