Partial implementation of updating user status

pull/14/head
Sam Bosley 12 years ago
parent d18d2b9397
commit 81b3e15912

@ -58,10 +58,15 @@ public final class User extends RemoteModel {
public static final LongProperty REMOTE_ID = new LongProperty(
TABLE, REMOTE_ID_PROPERTY_NAME);
/** Friendship status */
/** Friendship status. One of the STATUS constants below */
public static final StringProperty STATUS = new StringProperty(
TABLE, "status");
/** Friendship tatus that needs to be reported to the server.
* One of the PENDING constants below */
public static final StringProperty PENDING_STATUS = new StringProperty(
TABLE, "pendingStatus");
/** List of all properties for this model */
public static final Property<?>[] PROPERTIES = generateProperties(User.class);
@ -88,6 +93,11 @@ public final class User extends RemoteModel {
public static final String STATUS_IGNORED = "ignored";
public static final String STATUS_BLOCKED = "blocked";
public static final String PENDING_REQUEST = "request";
public static final String PENDING_APPROVE = "approve";
public static final String PENDING_IGNORE = "ignore";
public static final String PENDING_UNFRIEND = "unfriend";
// --- data access boilerplate
public User() {

@ -890,6 +890,27 @@ public final class ActFmSyncService {
return result.optInt("time", 0);
}
public void pushUser(User model) {
if (TextUtils.isEmpty(model.getValue(User.PENDING_STATUS)))
return;
if (model.getValue(User.REMOTE_ID) == 0)
return;
if (!checkForToken())
return;
try {
ArrayList<Object> params = new ArrayList<Object>();
params.add("token"); params.add(token);
params.add("id"); params.add(model.getValue(User.REMOTE_ID));
params.add("status"); params.add(model.getValue(User.PENDING_STATUS));
JSONObject result = actFmInvoker.invoke("user_set_status", params);
System.err.println("RESULT: " + result);
} catch (IOException e) {
handleException("user-status", e);
}
}
/**
* Fetch active tasks asynchronously

@ -29,6 +29,7 @@ import com.todoroo.astrid.billing.BillingConstants;
import com.todoroo.astrid.dao.Database;
import com.todoroo.astrid.dao.MetadataDao.MetadataCriteria;
import com.todoroo.astrid.dao.TaskDao.TaskCriteria;
import com.todoroo.astrid.dao.UserDao;
import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.data.Task;
@ -60,6 +61,8 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
@Autowired MetadataService metadataService;
@Autowired UserDao userDao;
@Autowired Database database;
private final PushQueuedArgs<Task> taskPusher = new PushQueuedArgs<Task>() {
@ -92,6 +95,19 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
}
};
private final PushQueuedArgs<User> userPusher = new PushQueuedArgs<User>() {
@Override
public User getRemoteModelInstance(TodorooCursor<User> cursor) {
return new User(cursor);
}
@Override
public void pushRemoteModel(User model) {
actFmSyncService.pushUser(model);
}
};
private final PushQueuedArgs<Metadata> filesPusher = new PushQueuedArgs<Metadata>() {
@Override
@ -160,7 +176,7 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
actFmPreferenceService.recordSyncStart();
updateUserStatus();
startUsersFetcher(callback, finisher);
startUsersSync(callback, finisher);
startTagFetcher(callback, finisher);
@ -207,13 +223,14 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
}
/** fetch changes to users/friends */
private void startUsersFetcher(final SyncResultCallback callback,
private void startUsersSync(final SyncResultCallback callback,
final AtomicInteger finisher) {
new Thread(new Runnable() {
@Override
public void run() {
int time = Preferences.getInt(LAST_USERS_FETCH_TIME, 0);
try {
pushQueuedUsers(callback, finisher);
time = actFmSyncService.fetchUsers();
Preferences.setInt(LAST_USERS_FETCH_TIME, time);
} catch (JSONException e) {
@ -397,6 +414,17 @@ public class ActFmSyncV2Provider extends SyncV2Provider {
}
private void pushQueuedUsers(final SyncResultCallback callback,
final AtomicInteger finisher) {
TodorooCursor<User> users = userDao.query(Query.select(User.PROPERTIES).where(
Criterion.and(User.PENDING_STATUS.isNotNull(), Functions.length(User.PENDING_STATUS).gt(0))));
try {
pushQueued(callback, finisher, users, true, userPusher);
} finally {
users.close();
}
}
// --- synchronize list
@Override

@ -126,8 +126,17 @@ public class PersonViewFragment extends TaskListFragment {
}
public void handleStatusButtonClicked() {
// TODO: Handle this for real
System.err.println("Status for " + user.getDisplayName() + " is " + user.getValue(User.STATUS)); //$NON-NLS-1$ //$NON-NLS-2$
String status = user.getValue(User.STATUS);
if (TextUtils.isEmpty(status)) { // Add friend case
user.setValue(User.PENDING_STATUS, User.PENDING_REQUEST);
} else if (User.STATUS_OTHER_PENDING.equals(status)) { // Accept friend case
user.setValue(User.PENDING_STATUS, User.PENDING_APPROVE);
}
if (user.getSetValues().containsKey(User.PENDING_STATUS.name)) {
userDao.saveExisting(user);
// Push
}
}
@Override

@ -40,7 +40,7 @@ public class Database extends AbstractDatabase {
* Database version number. This variable must be updated when database
* tables are updated, as it determines whether a database needs updating.
*/
public static final int VERSION = 25;
public static final int VERSION = 26;
/**
* Database name (must be unique)
@ -333,6 +333,9 @@ public class Database extends AbstractDatabase {
case 25: try {
database.execSQL("ALTER TABLE " + User.TABLE.name + " ADD " +
User.STATUS.accept(visitor, null));
database.execSQL("ALTER TABLE " + User.TABLE.name + " ADD " +
User.PENDING_STATUS.accept(visitor, null));
} catch (SQLiteException e) {
Log.e("astrid", "db-upgrade-" + oldVersion + "-" + newVersion, e);
}

Loading…
Cancel
Save