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 16 years ago
parent c0e9ae6ff3
commit 888183c1bb

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

@ -72,6 +72,18 @@
android:visibility="gone" android:visibility="gone"
android:paddingTop="4dip" android:paddingTop="4dip"
android:paddingLeft="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> </LinearLayout>

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

@ -215,7 +215,7 @@ public final class Task extends AbstractModel {
// --- parcelable helpers // --- 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 @Override
protected Creator<? extends AbstractModel> getCreator() { protected Creator<? extends AbstractModel> getCreator() {

Loading…
Cancel
Save