Experimental scheme for passing callback functions with sync messages, to be run at the end of a loop

pull/14/head
Sam Bosley 12 years ago
parent 29d10a6003
commit 89709ab71f

@ -22,6 +22,7 @@ import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
@ -332,7 +333,14 @@ public class TagViewFragment extends TaskListFragment {
if (actFmPreferenceService.isLoggedIn()) {
((TextView)taskListView.findViewById(android.R.id.empty)).setText(R.string.DLG_loading);
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<TagData>(TagData.class, tagData.getValue(TagData.UUID), tagData.getValue(TagData.PUSHED_AT)));
Runnable callback = new Runnable() {
@Override
public void run() {
Log.e("TagViewFragment", "Refresh data callback");
}
};
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<TagData>(TagData.class, tagData.getValue(TagData.UUID), tagData.getValue(TagData.PUSHED_AT)), callback);
// TODO: Refresh and reload tagData
// syncService.synchronizeList(tagData, manual, new ProgressBarSyncResultCallback(getActivity(), this,
@ -629,29 +637,33 @@ public class TagViewFragment extends TaskListFragment {
AndroidUtilities.tryUnregisterReceiver(getActivity(), notifyReceiver);
}
protected void reloadTagData() {
tagData = tagDataService.fetchById(tagData.getId(), TagData.PROPERTIES); // refetch
if (tagData == null) {
// This can happen if a tag has been deleted as part of a sync
return;
} else if (tagData.isDeleted()) {
justDeleted = true;
return;
}
filter = TagFilterExposer.filterFromTagData(getActivity(), tagData);
getActivity().getIntent().putExtra(TOKEN_FILTER, filter);
extras.putParcelable(TOKEN_FILTER, filter);
Activity activity = getActivity();
if (activity instanceof TaskListActivity) {
((TaskListActivity) activity).setListsTitle(filter.title);
FilterListFragment flf = ((TaskListActivity) activity).getFilterListFragment();
if (flf != null)
flf.clear();
}
taskAdapter = null;
Flags.set(Flags.REFRESH);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_SETTINGS && resultCode == Activity.RESULT_OK) {
tagData = tagDataService.fetchById(tagData.getId(), TagData.PROPERTIES); // refetch
if (tagData == null) {
// This can happen if a tag has been deleted as part of a sync
return;
} else if (tagData.isDeleted()) {
justDeleted = true;
return;
}
filter = TagFilterExposer.filterFromTagData(getActivity(), tagData);
getActivity().getIntent().putExtra(TOKEN_FILTER, filter);
extras.putParcelable(TOKEN_FILTER, filter);
Activity activity = getActivity();
if (activity instanceof TaskListActivity) {
((TaskListActivity) activity).setListsTitle(filter.title);
FilterListFragment flf = ((TaskListActivity) activity).getFilterListFragment();
if (flf != null)
flf.clear();
}
taskAdapter = null;
Flags.set(Flags.REFRESH);
reloadTagData();
} else {
super.onActivityResult(requestCode, resultCode, data);
}

@ -8,12 +8,10 @@ import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Intent;
import android.util.Log;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.sql.Query;
import com.todoroo.andlib.utility.AndroidUtilities;
@ -23,7 +21,6 @@ import com.todoroo.astrid.actfm.sync.messages.ClientToServerMessage;
import com.todoroo.astrid.actfm.sync.messages.NameMaps;
import com.todoroo.astrid.actfm.sync.messages.ReplayOutstandingEntries;
import com.todoroo.astrid.actfm.sync.messages.ServerToClientMessage;
import com.todoroo.astrid.api.AstridApiConstants;
import com.todoroo.astrid.core.PluginServices;
import com.todoroo.astrid.dao.OutstandingEntryDao;
import com.todoroo.astrid.dao.RemoteModelDao;
@ -48,6 +45,7 @@ public class ActFmSyncThread {
private static final String ERROR_TAG = "actfm-sync-thread"; //$NON-NLS-1$
private final List<ClientToServerMessage<?>> pendingMessages;
private final List<Runnable> pendingCallbacks;
private final Object monitor;
private Thread thread;
@ -101,9 +99,10 @@ public class ActFmSyncThread {
synchronized(ActFmSyncThread.class) {
if (instance == null) {
List<ClientToServerMessage<?>> syncQueue = Collections.synchronizedList(new LinkedList<ClientToServerMessage<?>>());
List<Runnable> callbackQueue = Collections.synchronizedList(new LinkedList<Runnable>());
ActFmSyncMonitor monitor = ActFmSyncMonitor.getInstance();
instance = new ActFmSyncThread(syncQueue, monitor);
instance = new ActFmSyncThread(syncQueue, callbackQueue, monitor);
taskDao.addListener(new SyncDatabaseListener<Task>(instance, ModelType.TYPE_TASK));
tagDataDao.addListener(new SyncDatabaseListener<TagData>(instance, ModelType.TYPE_TAG));
@ -116,9 +115,10 @@ public class ActFmSyncThread {
return instance;
}
private ActFmSyncThread(List<ClientToServerMessage<?>> messageQueue, Object syncMonitor) {
private ActFmSyncThread(List<ClientToServerMessage<?>> messageQueue, List<Runnable> callbackQueue, Object syncMonitor) {
DependencyInjectionService.getInstance().inject(this);
this.pendingMessages = messageQueue;
this.pendingCallbacks = callbackQueue;
this.monitor = syncMonitor;
}
@ -135,9 +135,11 @@ public class ActFmSyncThread {
}
}
public void enqueueMessage(ClientToServerMessage<?> message) {
public void enqueueMessage(ClientToServerMessage<?> message, Runnable callback) {
if (!pendingMessages.contains(message)) {
pendingMessages.add(message);
if (callback != null)
pendingCallbacks.add(callback);
synchronized(monitor) {
monitor.notifyAll();
}
@ -149,6 +151,7 @@ public class ActFmSyncThread {
try {
int batchSize = 1;
List<ClientToServerMessage<?>> messageBatch = new LinkedList<ClientToServerMessage<?>>();
List<Runnable> callbackBatch = new LinkedList<Runnable>();
while(true) {
synchronized(monitor) {
while ((pendingMessages.isEmpty() && !timeForBackgroundSync()) || !actFmPreferenceService.isLoggedIn()) {
@ -168,6 +171,11 @@ public class ActFmSyncThread {
messageBatch.add(message);
}
while (callbackBatch.size() < batchSize && !callbackBatch.isEmpty()) {
Runnable callback = pendingCallbacks.remove(0);
callbackBatch.add(callback);
}
if (messageBatch.isEmpty() && timeForBackgroundSync()) {
Flags.checkAndClear(Flags.BG_SYNC);
messageBatch.add(BriefMe.instantiateBriefMeForClass(Task.class, NameMaps.PUSHED_AT_TASKS));
@ -214,9 +222,14 @@ public class ActFmSyncThread {
Log.e(ERROR_TAG, "IOException", e);
batchSize = Math.max(batchSize / 2, 1);
}
for (Runnable r : callbackBatch) {
r.run();
}
messageBatch = new LinkedList<ClientToServerMessage<?>>();
Intent refresh = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH);
ContextManager.getContext().sendBroadcast(refresh);
callbackBatch = new LinkedList<Runnable>();
// Intent refresh = new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH);
// ContextManager.getContext().sendBroadcast(refresh);
}
}
} catch (Exception e) {

@ -18,7 +18,7 @@ public class SyncDatabaseListener<MTYPE extends AbstractModel> implements ModelU
public void onModelUpdated(MTYPE model, boolean outstandingEntries) {
if (outstandingEntries) {
ChangesHappened<?, ?> ch = ChangesHappened.instantiateChangesHappened(model.getId(), modelType);
actFmSyncThread.enqueueMessage(ch);
actFmSyncThread.enqueueMessage(ch, null);
}
}

@ -82,7 +82,7 @@ public class ReplayOutstandingEntries<T extends RemoteModel, OE extends Outstand
if (count > 0 && !afterErrors && actFmSyncThread != null) {
ChangesHappened<T, OE> ch = new ChangesHappened<T, OE>(id, modelClass, dao, outstandingDao);
actFmSyncThread.enqueueMessage(ch);
actFmSyncThread.enqueueMessage(ch, null);
}
outstanding.moveToPrevious(); // Move back one to undo the last iteration of the for loop

@ -25,6 +25,7 @@ import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
@ -34,7 +35,6 @@ import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
@ -56,6 +56,7 @@ import com.todoroo.astrid.actfm.sync.ActFmPreferenceService;
import com.todoroo.astrid.actfm.sync.ActFmSyncService;
import com.todoroo.astrid.actfm.sync.ActFmSyncThread;
import com.todoroo.astrid.actfm.sync.messages.BriefMe;
import com.todoroo.astrid.activity.AstridActivity;
import com.todoroo.astrid.activity.TaskEditFragment;
import com.todoroo.astrid.adapter.UpdateAdapter;
import com.todoroo.astrid.core.PluginServices;
@ -68,7 +69,6 @@ import com.todoroo.astrid.data.User;
import com.todoroo.astrid.data.UserActivity;
import com.todoroo.astrid.helper.AsyncImageView;
import com.todoroo.astrid.helper.ImageDiskCache;
import com.todoroo.astrid.helper.ProgressBarSyncResultCallback;
import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.service.StartupService;
import com.todoroo.astrid.service.StatisticsConstants;
@ -101,6 +101,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
private ImageButton pictureButton;
private Bitmap pendingCommentPicture = null;
private final Fragment fragment;
private final AstridActivity activity;
private final ImageDiskCache imageCache;
private final int cameraButton;
private final String linkColor;
@ -119,6 +120,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
imageCache = ImageDiskCache.getInstance();
this.fragment = fragment;
this.activity = (AstridActivity) fragment.getActivity();
linkColor = UpdateAdapter.getLinkColor(fragment);
@ -299,7 +301,7 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
update.readFromCursor(updates);
user.readPropertiesFromCursor(updates);
NoteOrUpdate noa = NoteOrUpdate.fromUpdate(update, user, linkColor);
NoteOrUpdate noa = NoteOrUpdate.fromUpdate(activity, update, user, linkColor);
if(noa != null)
items.add(noa);
}
@ -398,24 +400,31 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
return;
}
final SyncResultCallback callback;
if(existingCallback != null)
callback = existingCallback;
else {
callback = new ProgressBarSyncResultCallback(
((Activity)getContext()), (ProgressBar)parentView.findViewById(R.id.progressBar), new Runnable() {
@Override
public void run() {
setUpListAdapter();
loadingText.setText(R.string.ENA_no_comments);
loadingText.setVisibility(items.size() == 0 ? View.VISIBLE : View.GONE);
}
});
callback.started();
callback.incrementMax(100);
}
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<Task>(Task.class, task.getUuid(), task.getValue(Task.PUSHED_AT)));
// final SyncResultCallback callback;
// if(existingCallback != null)
// callback = existingCallback;
// else {
// callback = new ProgressBarSyncResultCallback(
// ((Activity)getContext()), (ProgressBar)parentView.findViewById(R.id.progressBar), new Runnable() {
// @Override
// public void run() {
// setUpListAdapter();
// loadingText.setText(R.string.ENA_no_comments);
// loadingText.setVisibility(items.size() == 0 ? View.VISIBLE : View.GONE);
// }
// });
//
// callback.started();
// callback.incrementMax(100);
// }
Runnable callback = new Runnable() {
@Override
public void run() {
Log.e("EditNoteActivity", "Refresh data callback");
}
};
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<Task>(Task.class, task.getUuid(), task.getValue(Task.PUSHED_AT)), callback);
// actFmSyncService.fetchUpdatesForTask(task, manual, new Runnable() {
@ -425,9 +434,9 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
// callback.finished();
// }
// });
callback.incrementProgress(50);
callback.finished();
callback.incrementProgress(50);
// callback.incrementProgress(50);
// callback.finished();
// callback.incrementProgress(50);
}
private void addComment() {
@ -521,13 +530,12 @@ public class EditNoteActivity extends LinearLayout implements TimerActionListene
m.getValue(Metadata.CREATION_DATE));
}
@SuppressWarnings("nls")
public static NoteOrUpdate fromUpdate(UserActivity u, User user, String linkColor) {
public static NoteOrUpdate fromUpdate(AstridActivity context, UserActivity u, User user, String linkColor) {
// JSONObject user = ActFmPreferenceService.userFromModel(u);
String commentPicture = u.getPictureUrl(UserActivity.PICTURE, RemoteModel.PICTURE_MEDIUM);
Spanned title = UpdateAdapter.getUpdateComment(null, u, user, linkColor, UpdateAdapter.FROM_TASK_VIEW);
Spanned title = UpdateAdapter.getUpdateComment(context, u, user, linkColor, UpdateAdapter.FROM_TASK_VIEW);
return new NoteOrUpdate(user.getPictureUrl(User.PICTURE, RemoteModel.PICTURE_THUMB),
title,
commentPicture,

@ -9,6 +9,7 @@ import android.content.ContentValues;
import android.content.Intent;
import android.support.v4.view.Menu;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
@ -197,15 +198,15 @@ public class PersonViewFragment extends TaskListFragment {
private void refreshData(final boolean manual) {
if (user != null) {
emptyView.setText(R.string.DLG_loading);
new Thread() {
@Override
public void run() {
// TODO: Fix friend status logic for new sync scheme
if (!TextUtils.isEmpty(user.getValue(User.PENDING_STATUS))) {
// emptyView.setText(R.string.DLG_loading);
// new Thread() {
// @Override
// public void run() {
// // TODO: Fix friend status logic for new sync scheme
// if (!TextUtils.isEmpty(user.getValue(User.PENDING_STATUS))) {
// actFmSyncService.pushUser(user);
user = userDao.fetch(user.getId(), User.PROPERTIES);
}
// user = userDao.fetch(user.getId(), User.PROPERTIES);
// }
// SyncResultCallback callback;
// try {
// if (getActivity() == null)
@ -233,10 +234,16 @@ public class PersonViewFragment extends TaskListFragment {
// }
//
// syncService.synchronizeList(user, manual, callback);
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<User>(User.class, user.getValue(User.UUID), user.getValue(User.PUSHED_AT)));
// TODO: Refresh
// // TODO: Refresh
// }
// }.start();
Runnable callback = new Runnable() {
@Override
public void run() {
Log.e("PersonViewFragment", "Refresh data callback");
}
}.start();
};
ActFmSyncThread.getInstance().enqueueMessage(new BriefMe<User>(User.class, user.getValue(User.UUID), user.getValue(User.PUSHED_AT)), callback);
}
}

@ -106,7 +106,7 @@ public class MetadataDao extends DatabaseDao<Metadata> {
to.setValue(OutstandingEntry.VALUE_STRING_PROPERTY, tagUuid);
database.insert(outstandingTable.name, null, to.getSetValues());
ActFmSyncThread.getInstance().enqueueMessage(new ChangesHappened<Task, TaskOutstanding>(taskId, Task.class,
PluginServices.getTaskDao(), PluginServices.getTaskOutstandingDao()));
PluginServices.getTaskDao(), PluginServices.getTaskOutstandingDao()), null);
return true;
}

@ -100,7 +100,7 @@ public class TagMetadataDao extends DatabaseDao<TagMetadata> {
to.setValue(OutstandingEntry.VALUE_STRING_PROPERTY, memberId);
database.insert(outstandingTable.name, null, to.getSetValues());
ActFmSyncThread.getInstance().enqueueMessage(new ChangesHappened<TagData, TagOutstanding>(tagDataId, TagData.class,
PluginServices.getTagDataDao(), PluginServices.getTagOutstandingDao()));
PluginServices.getTagDataDao(), PluginServices.getTagOutstandingDao()), null);
return true;
}

Loading…
Cancel
Save