Move task adapter subclasses to separate files

pull/795/head
Alex Baker 6 years ago
parent 30943b9e64
commit ed434a3e83

@ -0,0 +1,71 @@
package com.todoroo.astrid.adapter;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.subtasks.SubtasksFilterUpdater;
import org.tasks.data.TaskListMetadata;
import timber.log.Timber;
public final class AstridTaskAdapter extends TaskAdapter {
private final TaskListMetadata list;
private final Filter filter;
private final SubtasksFilterUpdater updater;
public AstridTaskAdapter(TaskListMetadata list, Filter filter, SubtasksFilterUpdater updater) {
this.list = list;
this.filter = filter;
this.updater = updater;
}
@Override
public int getIndent(Task task) {
return updater.getIndentForTask(task.getUuid());
}
@Override
public boolean canIndent(int position, Task task) {
String parentUuid = getItemUuid(position - 1);
int parentIndent = updater.getIndentForTask(parentUuid);
return getIndent(task) <= parentIndent;
}
@Override
public boolean isManuallySorted() {
return true;
}
@Override
public void moved(int from, int to) {
String targetTaskId = getItemUuid(from);
if (!Task.isValidUuid(targetTaskId)) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
if (to >= getCount()) {
updater.moveTo(list, filter, targetTaskId, "-1"); // $NON-NLS-1$
} else {
String destinationTaskId = getItemUuid(to);
updater.moveTo(list, filter, targetTaskId, destinationTaskId);
}
} catch (Exception e) {
Timber.e(e);
}
}
@Override
public void indented(int which, int delta) {
String targetTaskId = getItemUuid(which);
if (!Task.isValidUuid(targetTaskId)) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
updater.indent(list, filter, targetTaskId, delta);
} catch (Exception e) {
Timber.e(e);
}
}
}

@ -0,0 +1,67 @@
package com.todoroo.astrid.adapter;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
import org.tasks.data.GoogleTaskList;
import timber.log.Timber;
public final class GoogleTaskAdapter extends TaskAdapter {
private final GoogleTaskList list;
private final GtasksTaskListUpdater updater;
public GoogleTaskAdapter(GoogleTaskList list, GtasksTaskListUpdater updater) {
this.list = list;
this.updater = updater;
}
@Override
public int getIndent(Task task) {
return task.getIndent();
}
@Override
public boolean canIndent(int position, Task task) {
Task parent = getTask(position - 1);
return parent != null && getIndent(task) == 0;
}
@Override
public boolean isManuallySorted() {
return true;
}
@Override
public void moved(int from, int to) {
long targetTaskId = getTaskId(from);
if (targetTaskId <= 0) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
if (to >= getCount()) {
updater.moveTo(list, targetTaskId, -1);
} else {
long destinationTaskId = getTaskId(to);
updater.moveTo(list, targetTaskId, destinationTaskId);
}
} catch (Exception e) {
Timber.e(e);
}
}
@Override
public void indented(int which, int delta) {
long targetTaskId = getTaskId(which);
if (targetTaskId <= 0) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
updater.indent(list, targetTaskId, delta);
} catch (Exception e) {
Timber.e(e);
}
}
}

@ -107,7 +107,7 @@ public class TaskAdapter {
public void indented(int position, int delta) {} public void indented(int position, int delta) {}
public long getTaskId(int position) { long getTaskId(int position) {
return getTask(position).getId(); return getTask(position).getId();
} }

@ -213,7 +213,7 @@ public class GtasksTaskListUpdater {
* Move a task and all its children to the position right above taskIdToMoveto. Will change the * Move a task and all its children to the position right above taskIdToMoveto. Will change the
* indent level to match taskIdToMoveTo. * indent level to match taskIdToMoveTo.
*/ */
void moveTo(GoogleTaskList list, final long targetTaskId, final long moveBeforeTaskId) { public void moveTo(GoogleTaskList list, final long targetTaskId, final long moveBeforeTaskId) {
if (list == null) { if (list == null) {
return; return;
} }

@ -9,6 +9,7 @@ package com.todoroo.astrid.gtasks;
import android.text.TextUtils; import android.text.TextUtils;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.GoogleTaskAdapter;
import com.todoroo.astrid.adapter.TaskAdapter; import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
@ -21,7 +22,6 @@ import javax.inject.Inject;
import org.tasks.data.GoogleTask; import org.tasks.data.GoogleTask;
import org.tasks.data.GoogleTaskDao; import org.tasks.data.GoogleTaskDao;
import org.tasks.data.GoogleTaskList; import org.tasks.data.GoogleTaskList;
import timber.log.Timber;
class OrderedMetadataListFragmentHelper { class OrderedMetadataListFragmentHelper {
@ -31,7 +31,6 @@ class OrderedMetadataListFragmentHelper {
private final TaskDao taskDao; private final TaskDao taskDao;
private final Map<Long, ArrayList<Long>> chainedCompletions = private final Map<Long, ArrayList<Long>> chainedCompletions =
Collections.synchronizedMap(new HashMap<>()); Collections.synchronizedMap(new HashMap<>());
private DraggableTaskAdapter taskAdapter;
private TaskListFragment fragment; private TaskListFragment fragment;
private GoogleTaskList list; private GoogleTaskList list;
@ -52,7 +51,7 @@ class OrderedMetadataListFragmentHelper {
} }
TaskAdapter createTaskAdapter() { TaskAdapter createTaskAdapter() {
taskAdapter = new DraggableTaskAdapter(); GoogleTaskAdapter taskAdapter = new GoogleTaskAdapter(list, updater);
taskAdapter.setOnCompletedTaskListener(this::setCompletedForItemAndSubtasks); taskAdapter.setOnCompletedTaskListener(this::setCompletedForItemAndSubtasks);
@ -109,57 +108,4 @@ class OrderedMetadataListFragmentHelper {
updater.onDeleteTask(list, task.getId()); updater.onDeleteTask(list, task.getId());
fragment.loadTaskListContent(); fragment.loadTaskListContent();
} }
private final class DraggableTaskAdapter extends TaskAdapter {
@Override
public int getIndent(Task task) {
return task.getIndent();
}
@Override
public boolean canIndent(int position, Task task) {
Task parent = taskAdapter.getTask(position - 1);
return parent != null && getIndent(task) == 0;
}
@Override
public boolean isManuallySorted() {
return true;
}
@Override
public void moved(int from, int to) {
long targetTaskId = taskAdapter.getTaskId(from);
if (targetTaskId <= 0) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
if (to >= taskAdapter.getCount()) {
updater.moveTo(list, targetTaskId, -1);
} else {
long destinationTaskId = taskAdapter.getTaskId(to);
updater.moveTo(list, targetTaskId, destinationTaskId);
}
} catch (Exception e) {
Timber.e(e);
}
}
@Override
public void indented(int which, int delta) {
long targetTaskId = taskAdapter.getTaskId(which);
if (targetTaskId <= 0) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
updater.indent(list, targetTaskId, delta);
} catch (Exception e) {
Timber.e(e);
}
}
}
} }

@ -3,6 +3,7 @@ package com.todoroo.astrid.subtasks;
import android.text.TextUtils; import android.text.TextUtils;
import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.activity.TaskListFragment;
import com.todoroo.astrid.adapter.AstridTaskAdapter;
import com.todoroo.astrid.adapter.TaskAdapter; import com.todoroo.astrid.adapter.TaskAdapter;
import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.dao.TaskDao;
@ -14,7 +15,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import javax.inject.Inject; import javax.inject.Inject;
import org.tasks.data.TaskListMetadata; import org.tasks.data.TaskListMetadata;
import timber.log.Timber;
class AstridOrderedListFragmentHelper { class AstridOrderedListFragmentHelper {
@ -22,7 +22,6 @@ class AstridOrderedListFragmentHelper {
private final TaskDao taskDao; private final TaskDao taskDao;
private final Map<String, ArrayList<String>> chainedCompletions = private final Map<String, ArrayList<String>> chainedCompletions =
Collections.synchronizedMap(new HashMap<>()); Collections.synchronizedMap(new HashMap<>());
private DraggableTaskAdapter taskAdapter;
private TaskListFragment fragment; private TaskListFragment fragment;
private TaskListMetadata list; private TaskListMetadata list;
@ -41,7 +40,7 @@ class AstridOrderedListFragmentHelper {
} }
TaskAdapter createTaskAdapter() { TaskAdapter createTaskAdapter() {
taskAdapter = new DraggableTaskAdapter(); AstridTaskAdapter taskAdapter = new AstridTaskAdapter(list, fragment.getFilter(), updater);
taskAdapter.setOnCompletedTaskListener(this::setCompletedForItemAndSubtasks); taskAdapter.setOnCompletedTaskListener(this::setCompletedForItemAndSubtasks);
@ -107,58 +106,4 @@ class AstridOrderedListFragmentHelper {
updater.onDeleteTask(list, fragment.getFilter(), task.getUuid()); updater.onDeleteTask(list, fragment.getFilter(), task.getUuid());
fragment.loadTaskListContent(); fragment.loadTaskListContent();
} }
private final class DraggableTaskAdapter extends TaskAdapter {
@Override
public int getIndent(Task task) {
return updater.getIndentForTask(task.getUuid());
}
@Override
public boolean canIndent(int position, Task task) {
String parentUuid = taskAdapter.getItemUuid(position - 1);
int parentIndent = updater.getIndentForTask(parentUuid);
return getIndent(task) <= parentIndent;
}
@Override
public boolean isManuallySorted() {
return true;
}
@Override
public void moved(int from, int to) {
String targetTaskId = taskAdapter.getItemUuid(from);
if (!Task.isValidUuid(targetTaskId)) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
if (to >= taskAdapter.getCount()) {
updater.moveTo(list, fragment.getFilter(), targetTaskId, "-1"); // $NON-NLS-1$
} else {
String destinationTaskId = taskAdapter.getItemUuid(to);
updater.moveTo(list, fragment.getFilter(), targetTaskId, destinationTaskId);
}
} catch (Exception e) {
Timber.e(e);
}
}
@Override
public void indented(int which, int delta) {
String targetTaskId = taskAdapter.getItemUuid(which);
if (!Task.isValidUuid(targetTaskId)) {
return; // This can happen with gestures on empty parts of the list (e.g. extra space below
// tasks)
}
try {
updater.indent(list, fragment.getFilter(), targetTaskId, delta);
} catch (Exception e) {
Timber.e(e);
}
}
}
} }

@ -141,7 +141,7 @@ public class SubtasksFilterUpdater {
filter.setFilterQueryOverride(query); filter.setFilterQueryOverride(query);
} }
int getIndentForTask(String targetTaskId) { public int getIndentForTask(String targetTaskId) {
Node n = idToNode.get(targetTaskId); Node n = idToNode.get(targetTaskId);
if (n == null) { if (n == null) {
return 0; return 0;
@ -318,7 +318,7 @@ public class SubtasksFilterUpdater {
} }
} }
void moveTo(TaskListMetadata list, Filter filter, String targetTaskId, String beforeTaskId) { public void moveTo(TaskListMetadata list, Filter filter, String targetTaskId, String beforeTaskId) {
Node target = idToNode.get(targetTaskId); Node target = idToNode.get(targetTaskId);
if (target == null) { if (target == null) {
return; return;

Loading…
Cancel
Save