From 55c8be04d47982715d2016547cad9e32644f02e5 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Mon, 29 Jun 2020 16:44:52 -0500 Subject: [PATCH] Move viewholder callbacks to task list fragment --- .../astrid/activity/TaskListFragment.kt | 52 ++++++++++++++++--- .../tasklist/DragAndDropRecyclerAdapter.kt | 4 +- .../tasklist/PagedListRecyclerAdapter.kt | 4 +- .../tasks/tasklist/TaskListRecyclerAdapter.kt | 51 ++---------------- 4 files changed, 51 insertions(+), 60 deletions(-) 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 487ad5859..e5e6bfa99 100644 --- a/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt +++ b/app/src/main/java/com/todoroo/astrid/activity/TaskListFragment.kt @@ -71,10 +71,7 @@ import org.tasks.preferences.Device import org.tasks.preferences.Preferences import org.tasks.sync.SyncAdapters import org.tasks.tags.TagPickerActivity -import org.tasks.tasklist.DragAndDropRecyclerAdapter -import org.tasks.tasklist.PagedListRecyclerAdapter -import org.tasks.tasklist.TaskListRecyclerAdapter -import org.tasks.tasklist.ViewHolderFactory +import org.tasks.tasklist.* import org.tasks.themes.ColorProvider import org.tasks.themes.ThemeColor import org.tasks.ui.TaskListViewModel @@ -85,7 +82,9 @@ import javax.inject.Inject import kotlin.math.max @AndroidEntryPoint -class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickListener, MenuItem.OnActionExpandListener, SearchView.OnQueryTextListener, ActionMode.Callback { +class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickListener, + MenuItem.OnActionExpandListener, SearchView.OnQueryTextListener, ActionMode.Callback, + TaskViewHolder.ViewHolderCallbacks { private val refreshReceiver = RefreshReceiver() private var disposables: CompositeDisposable? = null @@ -230,13 +229,13 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL if (recyclerAdapter !is PagedListRecyclerAdapter) { setAdapter( PagedListRecyclerAdapter( - taskAdapter, recyclerView, viewHolderFactory, this, tasks, taskDao, preferences)) + taskAdapter, recyclerView, viewHolderFactory, this, tasks, preferences)) return } } else if (recyclerAdapter !is DragAndDropRecyclerAdapter) { setAdapter( DragAndDropRecyclerAdapter( - taskAdapter, recyclerView, viewHolderFactory, this, tasks, taskDao, preferences)) + taskAdapter, recyclerView, viewHolderFactory, this, tasks, preferences)) return } recyclerAdapter!!.submitList(tasks) @@ -736,6 +735,45 @@ class TaskListFragment : Fragment(), OnRefreshListener, Toolbar.OnMenuItemClickL fun clearCollapsed() = taskAdapter.clearCollapsed() + override fun onCompletedTask(task: TaskContainer, newState: Boolean) { + taskAdapter.onCompletedTask(task, newState) + loadTaskListContent() + } + + override fun onClick(taskViewHolder: TaskViewHolder) { + if (isActionModeActive) { + recyclerAdapter?.toggle(taskViewHolder) + } else { + onTaskListItemClicked(taskViewHolder.task.getTask()) + } + } + + override fun onClick(filter: Filter) { + if (!isActionModeActive) { + val context = activity + context?.startActivity(TaskIntents.getTaskListIntent(context, filter)) + } + } + + override fun onLongPress(taskViewHolder: TaskViewHolder): Boolean { + if (recyclerAdapter?.dragAndDropEnabled() != true) { + startActionMode() + } + if (isActionModeActive && !taskViewHolder.moving) { + recyclerAdapter?.toggle(taskViewHolder) + } + return true + } + + override fun onChangeDueDate(task: TaskContainer) { + showDateTimePicker(task) + } + + override fun toggleSubtasks(task: TaskContainer, collapsed: Boolean) { + taskDao.setCollapsed(task.id, collapsed) + broadcastRefresh() + } + companion object { const val TAGS_METADATA_JOIN = "for_tags" // $NON-NLS-1$ const val GTASK_METADATA_JOIN = "googletask" // $NON-NLS-1$ diff --git a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt index 6118f2f4f..e512a2f51 100644 --- a/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/DragAndDropRecyclerAdapter.kt @@ -9,7 +9,6 @@ import androidx.recyclerview.widget.ItemTouchHelper.Callback.makeMovementFlags import androidx.recyclerview.widget.RecyclerView import com.todoroo.astrid.activity.TaskListFragment import com.todoroo.astrid.adapter.TaskAdapter -import com.todoroo.astrid.dao.TaskDaoBlocking import com.todoroo.astrid.utility.Flags import io.reactivex.disposables.CompositeDisposable import io.reactivex.subjects.PublishSubject @@ -26,8 +25,7 @@ class DragAndDropRecyclerAdapter( viewHolderFactory: ViewHolderFactory, private val taskList: TaskListFragment, tasks: List, - taskDao: TaskDaoBlocking, - preferences: Preferences) : TaskListRecyclerAdapter(adapter, viewHolderFactory, taskList, taskDao, preferences), DragAndDropDiffer { + preferences: Preferences) : TaskListRecyclerAdapter(adapter, viewHolderFactory, taskList, preferences), DragAndDropDiffer { private val disableHeaders = taskList.getFilter().let { !it.supportsSorting() || !preferences.showGroupHeaders() diff --git a/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt index e6863cd2d..245b0980a 100644 --- a/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/PagedListRecyclerAdapter.kt @@ -6,7 +6,6 @@ import androidx.recyclerview.widget.AsyncDifferConfig import androidx.recyclerview.widget.RecyclerView import com.todoroo.astrid.activity.TaskListFragment import com.todoroo.astrid.adapter.TaskAdapter -import com.todoroo.astrid.dao.TaskDaoBlocking import org.tasks.data.TaskContainer import org.tasks.preferences.Preferences @@ -16,8 +15,7 @@ class PagedListRecyclerAdapter( viewHolderFactory: ViewHolderFactory, taskList: TaskListFragment, list: List, - taskDao: TaskDaoBlocking, - preferences: Preferences) : TaskListRecyclerAdapter(adapter, viewHolderFactory, taskList, taskDao, preferences) { + preferences: Preferences) : TaskListRecyclerAdapter(adapter, viewHolderFactory, taskList, preferences) { private val differ: AsyncPagedListDiffer = AsyncPagedListDiffer(this, AsyncDifferConfig.Builder(ItemCallback()).build()) diff --git a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt index 0f38a03cb..ead2d64eb 100644 --- a/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt +++ b/app/src/main/java/org/tasks/tasklist/TaskListRecyclerAdapter.kt @@ -6,22 +6,18 @@ 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.TaskDaoBlocking import org.tasks.data.TaskContainer -import org.tasks.intents.TaskIntents import org.tasks.preferences.Preferences -import org.tasks.tasklist.TaskViewHolder.ViewHolderCallbacks abstract class TaskListRecyclerAdapter internal constructor( private val adapter: TaskAdapter, internal val viewHolderFactory: ViewHolderFactory, private val taskList: TaskListFragment, - private val taskDao: TaskDaoBlocking, internal val preferences: Preferences) - : RecyclerView.Adapter(), ViewHolderCallbacks, ListUpdateCallback, TaskAdapterDataSource { + : RecyclerView.Adapter(), ListUpdateCallback, TaskAdapterDataSource { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = viewHolderFactory.newViewHolder(parent, this) + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder + = viewHolderFactory.newViewHolder(parent, taskList) override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { val sortMode = preferences.sortMode @@ -36,45 +32,6 @@ abstract class TaskListRecyclerAdapter internal constructor( } } - override fun onCompletedTask(task: TaskContainer, newState: Boolean) { - adapter.onCompletedTask(task, newState) - taskList.loadTaskListContent() - } - - override fun onClick(taskViewHolder: TaskViewHolder) { - if (taskList.isActionModeActive) { - toggle(taskViewHolder) - } else { - taskList.onTaskListItemClicked(taskViewHolder.task.getTask()) - } - } - - override fun onClick(filter: Filter) { - if (!taskList.isActionModeActive) { - val context = taskList.activity - context?.startActivity(TaskIntents.getTaskListIntent(context, filter)) - } - } - - override fun onLongPress(taskViewHolder: TaskViewHolder): Boolean { - if (!dragAndDropEnabled()) { - taskList.startActionMode() - } - if (taskList.isActionModeActive && !taskViewHolder.moving) { - toggle(taskViewHolder) - } - return true - } - - override fun onChangeDueDate(task: TaskContainer) { - taskList.showDateTimePicker(task) - } - - override fun toggleSubtasks(task: TaskContainer, collapsed: Boolean) { - taskDao.setCollapsed(task.id, collapsed) - taskList.broadcastRefresh() - } - fun toggle(taskViewHolder: TaskViewHolder) { adapter.toggleSelection(taskViewHolder.task) notifyItemChanged(taskViewHolder.adapterPosition) @@ -85,7 +42,7 @@ abstract class TaskListRecyclerAdapter internal constructor( } } - protected abstract fun dragAndDropEnabled(): Boolean + abstract fun dragAndDropEnabled(): Boolean abstract fun submitList(list: List)