Skip to content

Commit

Permalink
[#166]Only fetch countries if we never did that, 50 per fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
stellanl committed Apr 17, 2015
1 parent fed5850 commit d1ab0aa
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
4 changes: 2 additions & 2 deletions android/AkvoRSR/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.akvo.rsr.up"
android:versionCode="23"
android:versionName="1.4.0Beta"
android:versionCode="24"
android:versionName="1.4.0"
>

<uses-sdk
Expand Down
24 changes: 21 additions & 3 deletions android/AkvoRSR/src/org/akvo/rsr/up/dao/RsrDbAdapter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2014 Stichting Akvo (Akvo Foundation)
* Copyright (C) 2012-2015 Stichting Akvo (Akvo Foundation)
*
* This file is part of Akvo RSR.
*
Expand Down Expand Up @@ -772,8 +772,9 @@ public int[] countAllUpdatesFor(String _id) {
draftCount++;
} else if (cursor.getInt(unsentCol) > 0) {
unsentCount++;
} else
} else {
otherCount++;
}
cursor.moveToNext();
}
}
Expand Down Expand Up @@ -1097,7 +1098,7 @@ public Organisation findOrganisation(String _id) {
/**
* creates or updates a user in the db
*
* @param org
* @param org the organisation data to be updated
* @return
*/
public void saveOrganisation(Organisation org) {
Expand Down Expand Up @@ -1156,6 +1157,11 @@ public void clearAllData() {
executeSql("delete from " + COUNTRY_TABLE);
}

/**
* lists all Countries
*
* @return a Cursor containing all countries
*/
public Cursor listAllCountries() {
Cursor cursor = database.query(COUNTRY_TABLE,
null,
Expand All @@ -1166,6 +1172,18 @@ public Cursor listAllCountries() {
null);
return cursor;
}


public int getCountryCount() {
Cursor cursor = listAllCountries();
int c = 0;
if (cursor != null) {
c = cursor.getCount();
cursor.close();
}
return c;
}


/**
* saves or updates a Country in the db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ protected void onHandleIntent(Intent intent) {
String.format(ConstantUtil.FETCH_PROJ_URL_PATTERN, id)));
broadcastProgress(0, ++i, projects);
}
if (mFetchCountries) {
// TODO: rarely changes, so only fetch countries if we never did that
if (mFetchCountries && ad.getCountryCount() == 0) { // rarely changes, so only fetch countries if we never did that
dl.fetchCountryListRestApiPaged(this, new URL(SettingsUtil.host(this) +
String.format(ConstantUtil.FETCH_COUNTRIES_URL)));
}
Expand Down
5 changes: 2 additions & 3 deletions android/AkvoRSR/src/org/akvo/rsr/up/util/ConstantUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public class ConstantUtil {
public static final String POST_UPDATE_URL = "/rest/v1/project_update/?format=xml";
public static final String FETCH_UPDATE_URL_PATTERN = "/rest/v1/project_update/?format=xml&project=%s";//use default limit
public static final String VERIFY_UPDATE_PATTERN = "/rest/v1/project_update/?format=xml&uuid=%s&limit=2";
public static final String FETCH_PROJ_URL_PATTERN = "/rest/v1/project_up/%s/?format=xml&image_thumb_name=up&image_thumb_up_width=100"; //ask for thumbnail size
// public static final String FETCH_COUNTRIES_URL = "/api/v1/country/?format=xml&limit=0";
public static final String FETCH_COUNTRIES_URL = "/rest/v1/country/?format=xml";
public static final String FETCH_PROJ_URL_PATTERN = "/rest/v1/project_up/%s/?format=xml&image_thumb_name=up&image_thumb_up_width=100"; //now asks for thumbnail size
public static final String FETCH_COUNTRIES_URL = "/rest/v1/country/?format=xml&limit=50"; //very small objects - get many at a time
public static final String FETCH_USER_URL_PATTERN = "/api/v1/user/%s/?format=xml&depth=1";
public static final String FETCH_ORG_URL_PATTERN = "/api/v1/organisation/%s/?format=xml&depth=0";
public static final int MAX_IMAGE_UPLOAD_SIZE = 2000000; //Nginx POST limit is 3MB, B64 encoding expands 33% and there may be long text
Expand Down
4 changes: 2 additions & 2 deletions android/AkvoRSR/src/org/akvo/rsr/up/util/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ public Date fetchUpdateListRestApiPaged(Context ctx, URL url) throws ParserConfi
url = null;//we are done
} else {
//Vanilla case is 403 forbidden on an auth failure
Log.e(TAG, "Fetch update list HTTP error code:" + code);
Log.e(TAG, "Fetch update list HTTP error code:" + code + ' ' + h.message());
Log.e(TAG, h.body());
throw new FailedFetchException("Unexpected server response " + code);
throw new FailedFetchException("Unexpected server response " + code + ' ' + h.message());
}
}
Log.i(TAG, "Grand total of " + total + " updates");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void startElement(String namespaceURI, String localName,
buffer = "";
if (depth == 1 && localName.equals("next")) {
this.in_next = true;
} else if (depth == 1 && localName.equals("list-item")) {
} else if (depth == 2 && localName.equals("list-item")) {
this.in_country = true;
currentCountry = new Country();
} else if (in_country)
Expand Down

0 comments on commit d1ab0aa

Please sign in to comment.