Now we automatically feed our task to the decorations so no extra sql queries in the background. Also automatic updates when action button is pressed

pull/14/head
Tim Su 14 years ago
parent c0e9ae6ff3
commit 888183c1bb

@ -11,6 +11,7 @@ import android.content.Intent;
import android.graphics.Color;
import android.os.SystemClock;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.RemoteViews;
import com.timsu.astrid.R;
@ -33,8 +34,12 @@ public class TimerDecorationExposer extends BroadcastReceiver {
private static HashMap<Long, TaskDecoration> decorations =
new HashMap<Long, TaskDecoration>();
public static void removeFromCache(long taskId) {
private static HashMap<Long, Boolean> tasksNeedingUpdate =
new HashMap<Long, Boolean>();
public static void updateTimer(long taskId) {
decorations.remove(taskId);
tasksNeedingUpdate.put(taskId, true);
}
@Override
@ -45,11 +50,15 @@ public class TimerDecorationExposer extends BroadcastReceiver {
Task task;
try {
task = PluginServices.getTaskService().fetchById(taskId, Task.ELAPSED_SECONDS, Task.TIMER_START);
if(tasksNeedingUpdate.containsKey(taskId))
task = PluginServices.getTaskService().fetchById(taskId, Task.ID, Task.ELAPSED_SECONDS, Task.TIMER_START);
else
task = intent.getParcelableExtra("model"); //$NON-NLS-1$
} catch (IllegalStateException e) {
return;
}
if(task == null || (task.getValue(Task.ELAPSED_SECONDS) == 0 &&
if(task == null || task.getId() != taskId || (task.getValue(Task.ELAPSED_SECONDS) == 0 &&
task.getValue(Task.TIMER_START) == 0))
return;
@ -70,12 +79,16 @@ public class TimerDecorationExposer extends BroadcastReceiver {
elapsed += DateUtilities.now() - task.getValue(Task.TIMER_START);
decoration.decoration.setChronometer(R.id.timer, SystemClock.elapsedRealtime() -
elapsed, null, true);
decoration.decoration.setViewVisibility(R.id.timer, View.VISIBLE);
decoration.decoration.setViewVisibility(R.id.label, View.GONE);
} else {
// if timer is not started, make the chronometer just a text label,
// since we don't want the time to be displayed relative to elapsed
String format = buildFormat(elapsed);
decoration.decoration.setChronometer(R.id.timer, SystemClock.elapsedRealtime() -
elapsed, format, false);
decoration.color = 0;
decoration.decoration.setTextViewText(R.id.label, format);
decoration.decoration.setViewVisibility(R.id.timer, View.GONE);
decoration.decoration.setViewVisibility(R.id.label, View.VISIBLE);
}

@ -53,7 +53,7 @@ public class TimerPlugin extends BroadcastReceiver {
}
}
PluginServices.getTaskService().save(task);
TimerDecorationExposer.removeFromCache(task.getId());
TimerDecorationExposer.updateTimer(task.getId());
// transmit new intents
Intent intent = new Intent(AstridApiConstants.BROADCAST_REQUEST_ACTIONS);

@ -72,6 +72,18 @@
android:visibility="gone"
android:paddingTop="4dip"
android:paddingLeft="4dip"
android:orientation="horizontal" />
android:orientation="horizontal">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

@ -15,6 +15,14 @@
android:src="@drawable/timers_decoration" />
<Chronometer android:id="@+id/timer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="top"
android:visibility="gone"
android:textSize="10sp" />
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"

@ -68,6 +68,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
public static final String DETAIL_SEPARATOR = " | "; //$NON-NLS-1$
public static final String BROADCAST_EXTRA_TASK = "model"; //$NON-NLS-1$
// --- other constants
/** Properties that need to be read from the action item */
@ -80,6 +82,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
Task.HIDE_UNTIL,
Task.DELETION_DATE,
Task.DETAILS,
Task.ELAPSED_SECONDS,
Task.TIMER_START,
};
private static int[] IMPORTANCE_COLORS = null;
@ -265,8 +269,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
if(hiddenUntil > DateUtilities.now())
nameValue = r.getString(R.string.TAd_hiddenFormat, nameValue);
nameView.setText(nameValue);
viewHolder.nameView.setFocusable(false);
viewHolder.nameView.setClickable(false);
nameView.setMovementMethod(null);
Linkify.addLinks(nameView, Linkify.ALL);
}
@ -467,10 +471,21 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
extendedDetailManager.clearCache();
decorationManager.clearCache();
taskActionManager.clearCache();
taskDetailLoader.clear();
detailLoader = new DetailLoaderThread();
detailLoader.start();
}
/**
* Called to tell the cache to be cleared
*/
public void flushSpecific(long taskId) {
extendedDetailManager.clearCache(taskId);
decorationManager.clearCache(taskId);
taskActionManager.clearCache(taskId);
taskDetailLoader.remove(taskId);
}
/**
* AddOnManager for Details
* @author Tim Su <tim@todoroo.com>
@ -483,9 +498,9 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
}
@Override
Intent createBroadcastIntent(long taskId) {
Intent createBroadcastIntent(Task task) {
Intent broadcastIntent = new Intent(AstridApiConstants.BROADCAST_REQUEST_DETAILS);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
broadcastIntent.putExtra(AstridApiConstants.EXTRAS_EXTENDED, true);
return broadcastIntent;
}
@ -536,12 +551,18 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
*/
public class DecorationManager extends AddOnManager<TaskDecoration> {
@Override
Intent createBroadcastIntent(long taskId) {
Intent createBroadcastIntent(Task task) {
Intent intent = new Intent(AstridApiConstants.BROADCAST_REQUEST_DECORATIONS);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
intent.putExtra(BROADCAST_EXTRA_TASK, task);
return intent;
}
@Override
public void addNew(long taskId, String addOn, TaskDecoration item) {
super.addNew(taskId, addOn, item);
}
@Override
void draw(ViewHolder viewHolder, long taskId, Collection<TaskDecoration> decorations) {
if(decorations == null || viewHolder.task.getId() != taskId)
@ -551,6 +572,7 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
if(decorations.size() == 0)
return;
int i = 0;
boolean colorSet = false;
viewHolder.decorations = new View[decorations.size()];
@ -600,9 +622,9 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
LayoutParams.FILL_PARENT, 1f);
@Override
Intent createBroadcastIntent(long taskId) {
Intent createBroadcastIntent(Task task) {
Intent intent = new Intent(AstridApiConstants.BROADCAST_REQUEST_ACTIONS);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, taskId);
intent.putExtra(AstridApiConstants.EXTRAS_TASK_ID, task.getId());
return intent;
}
@ -620,14 +642,15 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
editButton.setLayoutParams(params);
viewHolder.actions.addView(editButton);
}
if(actions.size() + 1 < viewHolder.actions.getChildCount())
viewHolder.actions.removeViews(0, viewHolder.actions.getChildCount() -
actions.size() - 1);
for(int i = actions.size() + 1; i < viewHolder.actions.getChildCount(); i++) {
viewHolder.actions.getChildAt(i).setVisibility(View.GONE);
}
int i = 0;
Button button = (Button) viewHolder.actions.getChildAt(i++);
button.setText(R.string.TAd_actionEditTask);
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
@ -640,7 +663,8 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
for(TaskAction action : actions) {
button = (Button) viewHolder.actions.getChildAt(i++);
button.setText(action.text);
button.setOnClickListener(new ActionClickListener(action));
button.setVisibility(View.VISIBLE);
button.setOnClickListener(new ActionClickListener(action, viewHolder));
}
reset(viewHolder, taskId);
@ -680,19 +704,25 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
};
private final class ActionClickListener implements View.OnClickListener {
TaskAction action;
private final TaskAction action;
private final ViewHolder viewHolder;
public ActionClickListener(TaskAction action) {
public ActionClickListener(TaskAction action, ViewHolder viewHolder) {
this.action = action;
this.viewHolder = viewHolder;
}
public void onClick(View v) {
flushSpecific(viewHolder.task.getId());
try {
action.intent.send();
} catch (Exception e) {
exceptionService.displayAndReportError(activity,
"Error launching action", e); //$NON-NLS-1$
}
decorationManager.request(viewHolder);
extendedDetailManager.request(viewHolder);
taskActionManager.request(viewHolder);
}
};
@ -808,13 +838,13 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
// request details
draw(viewHolder, taskId, get(taskId));
Intent broadcastIntent = createBroadcastIntent(taskId);
Intent broadcastIntent = createBroadcastIntent(viewHolder.task);
activity.sendOrderedBroadcast(broadcastIntent, AstridApiConstants.PERMISSION_READ);
return true;
}
/** creates a broadcast intent for requesting */
abstract Intent createBroadcastIntent(long taskId);
abstract Intent createBroadcastIntent(Task task);
/** updates the given view */
abstract void draw(ViewHolder viewHolder, long taskId, Collection<TYPE> list);
@ -849,6 +879,13 @@ public class TaskAdapter extends CursorAdapter implements Filterable {
cache.clear();
}
/**
* Clears single item from cache
*/
public void clearCache(long taskId) {
cache.remove(taskId);
}
// --- internal goodies
/**

@ -215,7 +215,7 @@ public final class Task extends AbstractModel {
// --- parcelable helpers
private static final Creator<Task> CREATOR = new ModelCreator<Task>(Task.class);
public static final Creator<Task> CREATOR = new ModelCreator<Task>(Task.class);
@Override
protected Creator<? extends AbstractModel> getCreator() {

Loading…
Cancel
Save