From 3fb98995824e24fdc4d3ff1785a157098bc08bc4 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Tue, 8 May 2012 18:36:29 -0700 Subject: [PATCH] PersonViewFragment syncs tasks for that user --- .../astrid/actfm/sync/ActFmSyncService.java | 4 + .../actfm/sync/ActFmSyncV2Provider.java | 27 +++++++ .../astrid/people/PersonViewFragment.java | 74 ++++++++++++++++++- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java index 79c4f3ccc..2f894de98 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncService.java @@ -740,6 +740,10 @@ public final class ActFmSyncService { }, done, "tasks:" + tagData.getId(), "tag_id", tagData.getValue(TagData.REMOTE_ID)); } + public void fetchTasksForUser(final User user, final boolean manual, Runnable done) { + invokeFetchList("task", manual, null, new TaskListItemProcessor(false), + done, "user_" + user.getId(), "user_id", user.getValue(User.REMOTE_ID)); + } /** diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java index 9a4921d9a..1a40cf818 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncV2Provider.java @@ -25,6 +25,7 @@ import com.todoroo.astrid.data.Metadata; import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.TagData; import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.data.User; import com.todoroo.astrid.service.AstridDependencyInjector; import com.todoroo.astrid.service.TagDataService; import com.todoroo.astrid.service.TaskService; @@ -273,6 +274,11 @@ public class ActFmSyncV2Provider extends SyncV2Provider { public void synchronizeList(Object list, final boolean manual, final SyncResultCallback callback) { + if (list instanceof User) { + synchronizeUser((User) list, manual, callback); + return; + } + if(!(list instanceof TagData)) return; @@ -299,6 +305,27 @@ public class ActFmSyncV2Provider extends SyncV2Provider { }).start(); } + private void synchronizeUser(final User user, final boolean manual, final SyncResultCallback callback) { + if (user.getValue(User.REMOTE_ID) == 0) + return; + + new Thread(new Runnable() { + @Override + public void run() { + callback.started(); + callback.incrementMax(100); + + actFmSyncService.waitUntilEmpty(); + actFmSyncService.fetchTasksForUser(user, manual, new Runnable() { + public void run() { + callback.finished(); + } + }); + callback.incrementProgress(50); + } + }).start(); + } + private void fetchTagData(final TagData tagData, final boolean noRemoteId, final boolean manual, final SyncResultCallback callback, final AtomicInteger finisher) { diff --git a/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java b/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java index 8be951002..d260cc43e 100644 --- a/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java +++ b/astrid/plugin-src/com/todoroo/astrid/people/PersonViewFragment.java @@ -1,27 +1,48 @@ package com.todoroo.astrid.people; +import android.content.Intent; +import android.support.v4.view.Menu; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; import com.timsu.astrid.R; import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.ContextManager; +import com.todoroo.andlib.utility.DateUtilities; +import com.todoroo.andlib.utility.Preferences; +import com.todoroo.astrid.actfm.sync.ActFmPreferenceService; import com.todoroo.astrid.activity.TaskListFragment; +import com.todoroo.astrid.api.AstridApiConstants; import com.todoroo.astrid.dao.UserDao; import com.todoroo.astrid.data.User; +import com.todoroo.astrid.helper.ProgressBarSyncResultCallback; +import com.todoroo.astrid.service.SyncV2Service; +import com.todoroo.astrid.service.ThemeService; public class PersonViewFragment extends TaskListFragment { public static final String EXTRA_USER_ID_LOCAL = "user_local_id"; //$NON-NLS-1$ + private static final String LAST_FETCH_KEY = "actfm_last_user_"; //$NON-NLS-1$ + + protected static final int MENU_REFRESH_ID = MENU_SUPPORT_ID + 1; + @Autowired UserDao userDao; + @Autowired SyncV2Service syncService; + + @Autowired ActFmPreferenceService actFmPreferenceService; + + protected View taskListView; + private User user; @Override protected View getListBody(ViewGroup root) { ViewGroup parent = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.task_list_body_person, root, false); - View taskListView = super.getListBody(parent); + taskListView = super.getListBody(parent); parent.addView(taskListView); return parent; @@ -38,7 +59,56 @@ public class PersonViewFragment extends TaskListFragment { protected void setupQuickAddBar() { super.setupQuickAddBar(); quickAddBar.setUsePeopleControl(false); - quickAddBar.getQuickAddBox().setHint(getString(R.string.TLA_quick_add_hint_assign, user.getDisplayName())); + if (user != null) + quickAddBar.getQuickAddBox().setHint(getString(R.string.TLA_quick_add_hint_assign, user.getDisplayName())); + + } + + @Override + protected void addSyncRefreshMenuItem(Menu menu, int themeFlags) { + if(actFmPreferenceService.isLoggedIn()) { + addMenuItem(menu, R.string.actfm_TVA_menu_refresh, + ThemeService.getDrawable(R.drawable.icn_menu_refresh, themeFlags), MENU_REFRESH_ID, true); + } else { + super.addSyncRefreshMenuItem(menu, themeFlags); + } + } + + @Override + public boolean handleOptionsMenuItemSelected(int id, Intent intent) { + switch (id) { + case MENU_REFRESH_ID: + refreshData(true); + return true; + } + return super.handleOptionsMenuItemSelected(id, intent); + } + + @Override + protected void initiateAutomaticSyncImpl() { + if (!isCurrentTaskListFragment()) + return; + if (user != null) { + long lastAutoSync = Preferences.getLong(LAST_FETCH_KEY + user.getId(), 0); + if (DateUtilities.now() - lastAutoSync > DateUtilities.ONE_HOUR) + refreshData(false); + } + } + + private void refreshData(final boolean manual) { + ((TextView)taskListView.findViewById(android.R.id.empty)).setText(R.string.DLG_loading); + + syncService.synchronizeList(user, manual, new ProgressBarSyncResultCallback(getActivity(), this, + R.id.progressBar, new Runnable() { + @Override + public void run() { + if (manual) + ContextManager.getContext().sendBroadcast(new Intent(AstridApiConstants.BROADCAST_EVENT_REFRESH)); + else + refresh(); + ((TextView)taskListView.findViewById(android.R.id.empty)).setText(R.string.TLA_no_items); + } + })); } }