Add TaskAdapterDataSource interface

pull/996/head
Alex Baker 5 years ago
parent fe9dbe4585
commit cbd9af20ea

@ -246,7 +246,7 @@ class TaskListFragment : InjectingFragment(), OnRefreshListener, Toolbar.OnMenuI
private fun setAdapter(adapter: TaskListRecyclerAdapter) { private fun setAdapter(adapter: TaskListRecyclerAdapter) {
recyclerAdapter = adapter recyclerAdapter = adapter
recyclerView.adapter = adapter recyclerView.adapter = adapter
taskAdapter.setHelper(adapter) taskAdapter.setDataSource(adapter)
} }
private fun setupMenu() { private fun setupMenu() {

@ -8,7 +8,6 @@ import com.todoroo.astrid.subtasks.SubtasksFilterUpdater
import org.tasks.Strings.isNullOrEmpty import org.tasks.Strings.isNullOrEmpty
import org.tasks.data.TaskContainer import org.tasks.data.TaskContainer
import org.tasks.data.TaskListMetadata import org.tasks.data.TaskListMetadata
import org.tasks.tasklist.ViewHolder
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
import kotlin.math.abs import kotlin.math.abs
@ -23,7 +22,7 @@ class AstridTaskAdapter internal constructor(
override fun getIndent(task: TaskContainer) = updater.getIndentForTask(task.uuid) 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 { override fun maxIndent(previousPosition: Int, task: TaskContainer): Int {
val previous = getTask(previousPosition) val previous = getTask(previousPosition)

@ -3,14 +3,9 @@ package com.todoroo.astrid.adapter
import com.todoroo.astrid.dao.TaskDao import com.todoroo.astrid.dao.TaskDao
import org.tasks.data.CaldavDao import org.tasks.data.CaldavDao
import org.tasks.data.TaskContainer import org.tasks.data.TaskContainer
import org.tasks.tasklist.ViewHolder
class CaldavTaskAdapter internal constructor(private val taskDao: TaskDao, private val caldavDao: CaldavDao) : TaskAdapter() { class CaldavTaskAdapter internal constructor(private val taskDao: TaskDao, private val caldavDao: CaldavDao) : TaskAdapter() {
override fun canMove(sourceVh: ViewHolder, targetVh: ViewHolder): Boolean { override fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int) = !taskIsChild(source, to)
val source = sourceVh.task
val to = targetVh.adapterPosition
return !taskIsChild(source, to)
}
override fun maxIndent(previousPosition: Int, task: TaskContainer): Int { override fun maxIndent(previousPosition: Int, task: TaskContainer): Int {
val previous = getTask(previousPosition) val previous = getTask(previousPosition)

@ -4,18 +4,14 @@ import com.todoroo.astrid.dao.TaskDao
import org.tasks.BuildConfig import org.tasks.BuildConfig
import org.tasks.data.GoogleTaskDao import org.tasks.data.GoogleTaskDao
import org.tasks.data.TaskContainer import org.tasks.data.TaskContainer
import org.tasks.tasklist.ViewHolder
open class GoogleTaskManualSortAdapter internal constructor(val taskDao: TaskDao, val googleTaskDao: GoogleTaskDao) : TaskAdapter() { open class GoogleTaskManualSortAdapter internal constructor(val taskDao: TaskDao, val googleTaskDao: GoogleTaskDao) : TaskAdapter() {
override fun canMove(sourceVh: ViewHolder, targetVh: ViewHolder): Boolean { override fun canMove(source: TaskContainer, from: Int, target: TaskContainer, to: Int): Boolean {
val source = sourceVh.task
val to = targetVh.adapterPosition
if (!source.hasChildren() || to <= 0 || to >= count - 1) { if (!source.hasChildren() || to <= 0 || to >= count - 1) {
return true return true
} }
val target = targetVh.task return if (from < to) {
return if (sourceVh.adapterPosition < to) {
if (target.hasChildren()) { if (target.hasChildren()) {
return false return false
} }

@ -7,19 +7,17 @@ package com.todoroo.astrid.adapter
import com.todoroo.astrid.data.Task import com.todoroo.astrid.data.Task
import org.tasks.data.TaskContainer import org.tasks.data.TaskContainer
import org.tasks.tasklist.TaskListRecyclerAdapter
import org.tasks.tasklist.ViewHolder
import java.util.* import java.util.*
open class TaskAdapter { open class TaskAdapter {
private val selected = HashSet<Long>() private val selected = HashSet<Long>()
private lateinit var helper: TaskListRecyclerAdapter private lateinit var dataSource: TaskAdapterDataSource
val count: Int val count: Int
get() = helper.itemCount get() = dataSource.itemCount
fun setHelper(helper: TaskListRecyclerAdapter) { fun setDataSource(dataSource: TaskAdapterDataSource) {
this.helper = helper this.dataSource = dataSource
} }
val numSelected: Int val numSelected: Int
@ -38,7 +36,7 @@ open class TaskAdapter {
open fun getIndent(task: TaskContainer): Int = task.getIndent() 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 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) {} 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 fun getItemUuid(position: Int): String = getTask(position).uuid

@ -0,0 +1,9 @@
package com.todoroo.astrid.adapter
import org.tasks.data.TaskContainer
interface TaskAdapterDataSource {
fun getItem(position: Int): TaskContainer
val itemCount: Int
}

@ -159,7 +159,7 @@ public class DragAndDropRecyclerAdapter extends TaskListRecyclerAdapter {
int fromPosition = src.getAdapterPosition(); int fromPosition = src.getAdapterPosition();
int toPosition = target.getAdapterPosition(); int toPosition = target.getAdapterPosition();
ViewHolder source = (ViewHolder) src; ViewHolder source = (ViewHolder) src;
if (!adapter.canMove(source, (ViewHolder) target)) { if (!adapter.canMove(source.task, fromPosition, ((ViewHolder) target).task, toPosition)) {
return false; return false;
} }
if (from == -1) { if (from == -1) {

@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView;
import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.TaskAdapter; import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.adapter.TaskAdapterDataSource;
import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
@ -20,7 +21,7 @@ import org.tasks.tasklist.ViewHolder.ViewHolderCallbacks;
import java.util.List; import java.util.List;
public abstract class TaskListRecyclerAdapter extends RecyclerView.Adapter<ViewHolder> public abstract class TaskListRecyclerAdapter extends RecyclerView.Adapter<ViewHolder>
implements ViewHolderCallbacks, ListUpdateCallback { implements ViewHolderCallbacks, ListUpdateCallback, TaskAdapterDataSource {
private final TaskAdapter adapter; private final TaskAdapter adapter;
private final TaskListFragment taskList; private final TaskListFragment taskList;

Loading…
Cancel
Save