Skip to content

Commit

Permalink
[Fix #24486411] roughly sync TimeEntries updated on the web.
Browse files Browse the repository at this point in the history
  • Loading branch information
mootoh committed Feb 27, 2012
1 parent d6a8335 commit 52e4e8e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/net/mootoh/toggltouch/Tag.java
Expand Up @@ -196,7 +196,7 @@ public void onTouched(final Activity activity) {
api.startTimeEntry(touchedTask, new ApiResponseDelegate<Integer>() {
public void onSucceeded(Integer result) {
touchedTask.setId(result.intValue());
touchedTask.updateStartedAt();
touchedTask.updateStartedAtToNow();
try {
touchedTask.update(activity);
} catch (SQLException e) {
Expand Down Expand Up @@ -246,7 +246,7 @@ public void onSucceeded(Integer result) {
api.startTimeEntry(touchedTask, new ApiResponseDelegate<Integer>() {
public void onSucceeded(Integer result) {
touchedTask.setId(result.intValue());
touchedTask.updateStartedAt();
touchedTask.updateStartedAtToNow();
try {
touchedTask.update(activity);
} catch (SQLException e) {
Expand Down
71 changes: 59 additions & 12 deletions src/net/mootoh/toggltouch/Task.java
Expand Up @@ -67,6 +67,14 @@ public void setId(int id) {
this.id = id;
}

public void setStartedAt(Date date) {
this.startedAt = date;
}

public void updateStartedAtToNow() {
startedAt = new Date();
}

public String toStartJsonString() throws JSONException {
JSONObject data = new JSONObject();
Calendar calendar = new GregorianCalendar();
Expand Down Expand Up @@ -101,10 +109,6 @@ public String toStopJsonString() throws JSONException {
return json.toString();
}

public void updateStartedAt() {
startedAt = new Date();
}

// ----------------------------------------------------------------------------------
// DB
//
Expand Down Expand Up @@ -162,6 +166,31 @@ public static Task getTask(String taskId, Context context) {
return task;
}

public static Task getTaskForDescription(String description, Context context) {
String[] selectionArgs = {description};

DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, null, COLUMN_NAME_DESCRIPTION + " is ?", selectionArgs, null, null, null);
Task task = null;
if (cursor.moveToFirst()) {
String dateString = cursor.getString(cursor.getColumnIndex(COLUMN_NAME_STARTED));
try {
Date started = formatter.parse(dateString);
task = new Task(
cursor.getInt(cursor.getColumnIndex(COLUMN_NAME_ID)),
cursor.getString(cursor.getColumnIndex(COLUMN_NAME_DESCRIPTION)),
started
);
} catch (ParseException e) {
e.printStackTrace();
}
}
cursor.close();
db.close();
return task;
}

public static Task[] getAll(Context context) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Expand All @@ -187,23 +216,41 @@ public static Task[] getAll(Context context) {
tasks.toArray(ret);
return ret;
}

public static void sync(final Context context, final TaskSyncDelegate delegate) {
TogglApi api = new TogglApi(context);
api.getTimeEntries(new ApiResponseDelegate<Task[]>() {
public void onSucceeded(Task[] result) {
// TODO: re-link relation between Tag-Task
for (Task task: result) {
try {
task.save(context);
} catch (SQLException e) {
delegate.onFailed(e);
return;
Task existingTask = getTaskForDescription(task.description, context);
if (existingTask == null) {
try {
task.save(context);
} catch (SQLException e) {
delegate.onFailed(e);
return;
}
} else {
if (existingTask.getId().equals(task.getId()))
continue;

// re-link relation between Tag-Task
Tag tag = Tag.getForTaskId(existingTask.getId(), context);

existingTask.setId(task.id);
existingTask.setStartedAt(task.getStartedAt());
try {
existingTask.update(context);
} catch (SQLException e) {
e.printStackTrace();
}
if (tag != null)
tag.assignTask(existingTask, context);
}
}
delegate.onSucceeded(result);
}

public void onFailed(Exception e) {
delegate.onFailed(e);
}
Expand Down
1 change: 1 addition & 0 deletions src/net/mootoh/toggltouch/TogglApi.java
Expand Up @@ -98,6 +98,7 @@ public void onHttpResponse(JSONObject response) {
HashMap<String, Task> taskMap = new HashMap<String, Task>();

JSONArray data = response.getJSONArray("data");
Log.d(getClass().getSimpleName(), "TimeEntries size:" + data.length());
for (int i=0; i<data.length(); i++) {
JSONObject obj = (JSONObject)data.get(i);
String description = obj.getString("description");
Expand Down

0 comments on commit 52e4e8e

Please sign in to comment.