From cbd9af20ea431265876f6b706ac11a2644de8391 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 14 May 2020 11:48:25 -0500 Subject: [PATCH] Add TaskAdapterDataSource interface --- .../todoroo/astrid/activity/TaskListFragment.kt | 2 +- .../todoroo/astrid/adapter/AstridTaskAdapter.kt | 3 +-- .../todoroo/astrid/adapter/CaldavTaskAdapter.kt | 7 +------ .../astrid/adapter/GoogleTaskManualSortAdapter.kt | 8 ++------ .../java/com/todoroo/astrid/adapter/TaskAdapter.kt | 14 ++++++-------- .../astrid/adapter/TaskAdapterDataSource.kt | 9 +++++++++ .../tasks/tasklist/DragAndDropRecyclerAdapter.java | 2 +- .../tasks/tasklist/TaskListRecyclerAdapter.java | 3 ++- 8 files changed, 23 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt diff --git a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt index 811a06943..4b0c94789 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -246,7 +246,7 @@ class TaskListFragment : InjectingFragment(), OnRefreshListener, Toolbar.OnMenuI private fun setAdapter(adapter: TaskListRecyclerAdapter) { recyclerAdapter = adapter recyclerView.adapter = adapter - taskAdapter.setHelper(adapter) + taskAdapter.setDataSource(adapter) } private fun setupMenu() { diff --git a/app/src/main/java/com/todoroo/astrid/adapter/AstridTaskAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/AstridTaskAdapter.kt index 56337d94a..97e9685e0 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/AstridTaskAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/AstridTaskAdapter.kt @@ -8,7 +8,6 @@ import com.todoroo.astrid.subtasks.SubtasksFilterUpdater import org.tasks.Strings.isNullOrEmpty import org.tasks.data.TaskContainer import org.tasks.data.TaskListMetadata -import org.tasks.tasklist.ViewHolder import timber.log.Timber import java.util.* import kotlin.math.abs @@ -23,7 +22,7 @@ class AstridTaskAdapter internal constructor( override fun getIndent(task: TaskContainer) = updater.getIndentForTask(task.uuid) - override fun canMove(source: ViewHolder, target: ViewHolder) = !updater.isDescendantOf(target.task.uuid, source.task.uuid) + override fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int) = !updater.isDescendantOf(target.uuid, source.uuid) override fun maxIndent(previousPosition: Int, task: TaskContainer): Int { val previous = getTask(previousPosition) diff --git a/app/src/main/java/com/todoroo/astrid/adapter/CaldavTaskAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/CaldavTaskAdapter.kt index 976f43ec6..288ba5e76 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/CaldavTaskAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/CaldavTaskAdapter.kt @@ -3,14 +3,9 @@ package com.todoroo.astrid.adapter import com.todoroo.astrid.dao.TaskDao import org.tasks.data.CaldavDao import org.tasks.data.TaskContainer -import org.tasks.tasklist.ViewHolder class CaldavTaskAdapter internal constructor(private val taskDao: TaskDao, private val caldavDao: CaldavDao) : TaskAdapter() { - override fun canMove(sourceVh: ViewHolder, targetVh: ViewHolder): Boolean { - val source = sourceVh.task - val to = targetVh.adapterPosition - return !taskIsChild(source, to) - } + override fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int) = !taskIsChild(source, to) override fun maxIndent(previousPosition: Int, task: TaskContainer): Int { val previous = getTask(previousPosition) diff --git a/app/src/main/java/com/todoroo/astrid/adapter/GoogleTaskManualSortAdapter.kt b/app/src/main/java/com/todoroo/astrid/adapter/GoogleTaskManualSortAdapter.kt index 78fe4ec15..77b809e70 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/GoogleTaskManualSortAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/GoogleTaskManualSortAdapter.kt @@ -4,18 +4,14 @@ import com.todoroo.astrid.dao.TaskDao import org.tasks.BuildConfig import org.tasks.data.GoogleTaskDao import org.tasks.data.TaskContainer -import org.tasks.tasklist.ViewHolder open class GoogleTaskManualSortAdapter internal constructor(val taskDao: TaskDao, val googleTaskDao: GoogleTaskDao) : TaskAdapter() { - override fun canMove(sourceVh: ViewHolder, targetVh: ViewHolder): Boolean { - val source = sourceVh.task - val to = targetVh.adapterPosition + override fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int): Boolean { if (!source.hasChildren() || to <= 0 || to >= count - 1) { return true } - val target = targetVh.task - return if (sourceVh.adapterPosition < to) { + return if (from < to) { if (target.hasChildren()) { return false } 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 c69a56afd..320b66048 100644 --- a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt +++ b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapter.kt @@ -7,19 +7,17 @@ package com.todoroo.astrid.adapter import com.todoroo.astrid.data.Task import org.tasks.data.TaskContainer -import org.tasks.tasklist.TaskListRecyclerAdapter -import org.tasks.tasklist.ViewHolder import java.util.* open class TaskAdapter { private val selected = HashSet() - private lateinit var helper: TaskListRecyclerAdapter + private lateinit var dataSource: TaskAdapterDataSource val count: Int - get() = helper.itemCount + get() = dataSource.itemCount - fun setHelper(helper: TaskListRecyclerAdapter) { - this.helper = helper + fun setDataSource(dataSource: TaskAdapterDataSource) { + this.dataSource = dataSource } val numSelected: Int @@ -38,7 +36,7 @@ open class TaskAdapter { open fun getIndent(task: TaskContainer): Int = task.getIndent() - open fun canMove(source: ViewHolder, target: ViewHolder): Boolean = false + open fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int): Boolean = false open fun maxIndent(previousPosition: Int, task: TaskContainer): Int = 0 @@ -61,7 +59,7 @@ open class TaskAdapter { open fun moved(from: Int, to: Int, indent: Int) {} - fun getTask(position: Int): TaskContainer = helper.getItem(position) + fun getTask(position: Int): TaskContainer = dataSource.getItem(position) fun getItemUuid(position: Int): String = getTask(position).uuid diff --git a/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt new file mode 100644 index 000000000..09da1807d --- /dev/null +++ b/app/src/main/java/com/todoroo/astrid/adapter/TaskAdapterDataSource.kt @@ -0,0 +1,9 @@ +package com.todoroo.astrid.adapter + +import org.tasks.data.TaskContainer + +interface TaskAdapterDataSource { + fun getItem(position: Int): TaskContainer + + val itemCount: Int +} \ No newline at end of file diff --git a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.java b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.java index 085fde59f..24dba1373 100644 --- a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.java +++ b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.java @@ -159,7 +159,7 @@ public class DragAndDropRecyclerAdapter extends TaskListRecyclerAdapter { int fromPosition = src.getAdapterPosition(); int toPosition = target.getAdapterPosition(); ViewHolder source = (ViewHolder) src; - if (!adapter.canMove(source, (ViewHolder) target)) { + if (!adapter.canMove(source.task, fromPosition, ((ViewHolder) target).task, toPosition)) { return false; } if (from == -1) { diff --git a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.java b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.java index 6328f4ad1..78499a27e 100644 --- a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.java +++ b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.java @@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.adapter.TaskAdapter; +import com.todoroo.astrid.adapter.TaskAdapterDataSource; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.dao.TaskDao; @@ -20,7 +21,7 @@ import org.tasks.tasklist.ViewHolder.ViewHolderCallbacks; import java.util.List; public abstract class TaskListRecyclerAdapter extends RecyclerView.Adapter - implements ViewHolderCallbacks, ListUpdateCallback { + implements ViewHolderCallbacks, ListUpdateCallback, TaskAdapterDataSource { private final TaskAdapter adapter; private final TaskListFragment taskList;