Moved task actions loading into its own thread

pull/14/head
Sam Bosley 14 years ago
parent 414ea4f24a
commit baf2da5bda

Binary file not shown.

Before

Width:  |  Height:  |  Size: 726 B

After

Width:  |  Height:  |  Size: 7.7 KiB

@ -162,6 +162,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
private final int resource; private final int resource;
private final LayoutInflater inflater; private final LayoutInflater inflater;
private DetailLoaderThread detailLoader; private DetailLoaderThread detailLoader;
private ActionsLoaderThread actionsLoader;
private int fontSize; private int fontSize;
protected boolean applyListenersToRowBody = false; protected boolean applyListenersToRowBody = false;
private long mostRecentlyMade = -1; private long mostRecentlyMade = -1;
@ -214,6 +215,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
this.minRowHeight = (int) (57 * displayMetrics.density); this.minRowHeight = (int) (57 * displayMetrics.density);
startDetailThread(); startDetailThread();
startTaskActionsThread();
decorationManager = new DecorationManager(); decorationManager = new DecorationManager();
@ -230,6 +232,11 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
} }
} }
private void startTaskActionsThread() {
actionsLoader = new ActionsLoaderThread();
actionsLoader.start();
}
/* ====================================================================== /* ======================================================================
* =========================================================== filterable * =========================================================== filterable
* ====================================================================== */ * ====================================================================== */
@ -634,7 +641,6 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
// a large number of tasks to load, since it all goes into memory. // a large number of tasks to load, since it all goes into memory.
// it's best to do this, though, in order to append details to each other // it's best to do this, though, in order to append details to each other
private final Map<Long, StringBuilder> taskDetailLoader = Collections.synchronizedMap(new HashMap<Long, StringBuilder>(0)); private final Map<Long, StringBuilder> taskDetailLoader = Collections.synchronizedMap(new HashMap<Long, StringBuilder>(0));
private final Map<Long, TaskAction> taskActionLoader = Collections.synchronizedMap(new HashMap<Long, TaskAction>());
public class DetailLoaderThread extends Thread { public class DetailLoaderThread extends Thread {
@Override @Override
@ -648,7 +654,6 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
Random random = new Random(); Random random = new Random();
Task task = new Task(); Task task = new Task();
LinkActionExposer linkActionExposer = new LinkActionExposer();
for(fetchCursor.moveToFirst(); !fetchCursor.isAfterLast(); fetchCursor.moveToNext()) { for(fetchCursor.moveToFirst(); !fetchCursor.isAfterLast(); fetchCursor.moveToNext()) {
task.clear(); task.clear();
@ -656,10 +661,6 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
if(task.isCompleted()) if(task.isCompleted())
continue; continue;
List<TaskAction> actions = linkActionExposer.getActionsForTask(ContextManager.getContext(), task.getId());
if (actions.size() > 0)
taskActionLoader.put(task.getId(), actions.get(0));
if(detailsAreRecentAndUpToDate(task)) { if(detailsAreRecentAndUpToDate(task)) {
// even if we are up to date, randomly load a fraction // even if we are up to date, randomly load a fraction
if(random.nextFloat() < 0.1) { if(random.nextFloat() < 0.1) {
@ -683,7 +684,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
requestNewDetails(task); requestNewDetails(task);
} }
if(taskDetailLoader.size() > 0 || taskActionLoader.size() > 0) { if(taskDetailLoader.size() > 0) {
Activity activity = fragment.getActivity(); Activity activity = fragment.getActivity();
if (activity != null) { if (activity != null) {
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@ -720,6 +721,46 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
} }
} }
private final Map<Long, TaskAction> taskActionLoader = Collections.synchronizedMap(new HashMap<Long, TaskAction>());
public class ActionsLoaderThread extends Thread {
@Override
public void run() {
AndroidUtilities.sleepDeep(500L);
TodorooCursor<Task> fetchCursor = taskService.fetchFiltered(
query.get(), null, Task.ID, Task.TITLE, Task.DETAILS, Task.DETAILS_DATE,
Task.MODIFICATION_DATE, Task.COMPLETION_DATE);
try {
Task task = new Task();
LinkActionExposer linkActionExposer = new LinkActionExposer();
for(fetchCursor.moveToFirst(); !fetchCursor.isAfterLast(); fetchCursor.moveToNext()) {
task.clear();
task.readFromCursor(fetchCursor);
if(task.isCompleted())
continue;
List<TaskAction> actions = linkActionExposer.getActionsForTask(ContextManager.getContext(), task.getId());
if (actions.size() > 0)
taskActionLoader.put(task.getId(), actions.get(0));
}
if(taskActionLoader.size() > 0) {
Activity activity = fragment.getActivity();
if (activity != null) {
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
}
} finally {
fetchCursor.close();
}
}
}
/** /**
* Add detail to a task * Add detail to a task
* *
@ -797,6 +838,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
taskDetailLoader.clear(); taskDetailLoader.clear();
taskActionLoader.clear(); taskActionLoader.clear();
startDetailThread(); startDetailThread();
startTaskActionsThread();
} }
/** /**

Loading…
Cancel
Save