Skip to content

Commit

Permalink
add formQueue offline syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
Hurshal Patel committed Jun 19, 2014
1 parent 5e4cf97 commit 446e3b6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
add post method to clear db
implement all form element types
handle files
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void submitData(FormData data, FormQueue queue) {

private boolean isOnline() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo.isConnected();
return networkInfo != null && networkInfo.isConnected();
}

private void clearForm() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public void add(FormData data) {
write();
}

//TODO use hashed FormData objects instead of timestamp
public void remove(FormData data) {
FormData[] oldSubmissions = queue.submissions;
queue.submissions = new FormData[oldSubmissions.length - 1];

int j = 0;
for (int i = 0; i < oldSubmissions.length; i++) {
if (oldSubmissions[i].timestamp != data.timestamp) {
queue.submissions[j] = oldSubmissions[i];
j++;
}
}

write();
}

public boolean flush() {
if (queue.submissions.length == 0)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,26 @@ protected Boolean doInBackground(Void... params) {
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d(TAG, "Recieved response: " + response.toString());
return true;
if (response.getStatusLine().getStatusCode() == 200) {
Log.d(TAG, "Recieved response: " + response.toString());
doTaskSuccess();
return true;
} else {
doTaskFail();
return false;
}
} catch (Exception e) {
Log.e(TAG, e.toString());
formQueue.add(formData);
doTaskFail();
return false;
}
}

private void doTaskSuccess() {
formQueue.remove(formData);
}

private void doTaskFail() {
formQueue.add(formData);
}
}

0 comments on commit 446e3b6

Please sign in to comment.