Skip to content

Commit

Permalink
Release 0.7.0
Browse files Browse the repository at this point in the history
Merge branch 'develop'
  • Loading branch information
ifoche committed Dec 14, 2017
2 parents 718e4a7 + a059f52 commit a60e8f2
Show file tree
Hide file tree
Showing 52 changed files with 1,351 additions and 257 deletions.
2 changes: 1 addition & 1 deletion api/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application android:allowBackup="true" />
<application android:allowBackup="false" />

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.ResourceType;
import org.hisp.dhis.android.dashboard.api.utils.DbUtils;
import org.hisp.dhis.android.dashboard.api.utils.SyncStrategy;
import org.joda.time.DateTime;

import java.util.ArrayList;
Expand Down Expand Up @@ -122,22 +123,27 @@ private static List<DashboardElement> queryDashboardElements(DashboardItem item)
.queryList();
}

public void syncDashboards() throws APIException {
public void syncDashboards(SyncStrategy syncStrategy) throws APIException {
/* first we need to fetch all changes from server
and apply them to local database */
getDashboardDataFromServer();
getDashboardDataFromServer(syncStrategy);

/* now we can try to send changes made locally to server */
sendLocalChanges();
}

private void getDashboardDataFromServer() throws APIException {
private void getDashboardDataFromServer(SyncStrategy syncStrategy) throws APIException {
DateTime lastUpdated = DateTimeManager.getInstance()
.getLastUpdated(ResourceType.DASHBOARDS);
DateTime serverDateTime = mDhisApi.getSystemInfo()
.getServerDate();

List<Dashboard> dashboards = updateDashboards(lastUpdated);
List<Dashboard> dashboards;
if (syncStrategy == SyncStrategy.DOWNLOAD_ONLY_NEW) {
dashboards = updateDashboards(lastUpdated);
} else {
dashboards = updateDashboards(null);
}
List<DashboardItem> dashboardItems = updateDashboardItems(dashboards, lastUpdated);

Queue<DbOperation> operations = new LinkedList<>();
Expand Down Expand Up @@ -615,6 +621,9 @@ public void syncDashboardContent() throws APIException {
operations.addAll(DbUtils.createOperations(new Select()
.from(DashboardItemContent.class).queryList(), dashboardItemContent));
DbUtils.applyBatch(operations);



DateTimeManager.getInstance()
.setLastUpdated(ResourceType.DASHBOARDS_CONTENT, serverDateTime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@
import org.hisp.dhis.android.dashboard.api.network.RepoManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.LastUpdatedManager;

import org.hisp.dhis.android.dashboard.api.persistence.preferences.SettingsManager;
import org.hisp.dhis.android.dashboard.api.utils.SyncStrategy;
import org.hisp.dhis.android.dashboard.api.utils.NetworkUtils;

import java.io.IOException;

public class DhisController {
private static DhisController mDhisController;
Expand Down Expand Up @@ -80,19 +83,35 @@ public static DhisController getInstance() {
}

public static String buildImageUrl(String resource, String id, Context context) {
String widthUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_WIDTH), SettingsManager.MINIMUM_WIDTH);
String heightUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_HEIGHT), SettingsManager.MINIMUM_HEIGHT);
return getInstance().getServerUrl().newBuilder()
.addPathSegment("api").addPathSegment(resource).addPathSegment(id).addPathSegment(
"data.png")
.addQueryParameter("width", widthUserPreference).addQueryParameter("height", heightUserPreference)
.toString();
if (resource.contains(PullImageController.MAPS_ENDPOINT)) {
return getInstance().getServerUrl().newBuilder()
.addPathSegment("api").addPathSegment(resource).addPathSegment(
id).addPathSegment(
"data.png")
.addQueryParameter("width", "500").addQueryParameter("height", "391")
.toString();
} else {
String widthUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_WIDTH), SettingsManager.MINIMUM_WIDTH);
String heightUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_HEIGHT), SettingsManager.MINIMUM_HEIGHT);
return getInstance().getServerUrl().newBuilder()
.addPathSegment("api").addPathSegment(resource).addPathSegment(
id).addPathSegment(
"data.png")
.addQueryParameter("width", widthUserPreference).addQueryParameter("height",
heightUserPreference)
.toString();
}
}

public UserAccount logInUser(HttpUrl serverUrl, Credentials credentials) throws APIException {
return signInUser(serverUrl, credentials);
if (NetworkUtils.isNetworkAvailable(mContext)) {
return signInUser(serverUrl, credentials);
} else {
throw APIException.networkError(serverUrl.toString(),
new IOException("Network exception"));
}
}

public UserAccount confirmUser(Credentials credentials) throws APIException {
Expand Down Expand Up @@ -158,18 +177,23 @@ public void syncDashboardContent() throws APIException {
(new DashboardController(mDhisApi)).syncDashboardContent();
}

public void syncDashboards() throws APIException {
(new DashboardController(mDhisApi)).syncDashboards();
public void syncDashboards(SyncStrategy syncStrategy) throws APIException {
(new DashboardController(mDhisApi)).syncDashboards(syncStrategy);
}

public void syncInterpretations() throws APIException {
(new InterpretationController(mDhisApi)).syncInterpretations();
public void syncInterpretations(SyncStrategy syncStrategy) throws APIException {
(new InterpretationController(mDhisApi)).syncInterpretations(syncStrategy);
}

public void pullDashboardImages(ImageNetworkPolicy imageNetworkPolicy,Context context) {
(new PullImageController(context)).pullDashboardImages(imageNetworkPolicy);
public void syncDataMaps() {
(new MapController(mDhisApi)).syncDataMaps();
}
public void pullInterpretationImages(ImageNetworkPolicy imageNetworkPolicy,Context context) {
(new PullImageController(context)).pullInterpretationImages(imageNetworkPolicy);

public void pullDashboardImages(ImageNetworkPolicy imageNetworkPolicy, Context context) {
(new PullImageController(mDhisApi,context)).pullDashboardImages(imageNetworkPolicy);
}

public void pullInterpretationImages(ImageNetworkPolicy imageNetworkPolicy, Context context) {
(new PullImageController(mDhisApi,context)).pullInterpretationImages(imageNetworkPolicy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@

package org.hisp.dhis.android.dashboard.api.controllers;

import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.merge;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toMap;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.findLocationHeader;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.handleApiException;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.unwrapResponse;

import android.net.Uri;

import com.raizlabs.android.dbflow.sql.builder.Condition;
Expand All @@ -49,6 +55,7 @@
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.ResourceType;
import org.hisp.dhis.android.dashboard.api.utils.DbUtils;
import org.hisp.dhis.android.dashboard.api.utils.SyncStrategy;
import org.joda.time.DateTime;

import java.util.ArrayList;
Expand All @@ -62,12 +69,6 @@
import retrofit.client.Response;
import retrofit.mime.TypedString;

import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.merge;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toMap;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.findLocationHeader;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.handleApiException;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.unwrapResponse;

/**
* @author Araz Abishov <[email protected]>.
*/
Expand All @@ -78,8 +79,8 @@ public InterpretationController(DhisApi dhisApi) {
mDhisApi = dhisApi;
}

public void syncInterpretations() throws APIException {
getInterpretationDataFromServer();
public void syncInterpretations(SyncStrategy syncStrategy) throws APIException {
getInterpretationDataFromServer(syncStrategy);
sendLocalChanges();
}

Expand Down Expand Up @@ -355,12 +356,17 @@ private void updateInterpretationCommentTimeStamp(InterpretationComment comment)
}
}

private void getInterpretationDataFromServer() throws APIException {
private void getInterpretationDataFromServer(SyncStrategy syncStrategy) throws APIException {
DateTime lastUpdated = DateTimeManager.getInstance()
.getLastUpdated(ResourceType.INTERPRETATIONS);
DateTime serverTime = mDhisApi.getSystemInfo().getServerDate();

List<Interpretation> interpretations = updateInterpretations(lastUpdated);
List<Interpretation> interpretations;
if (syncStrategy == SyncStrategy.DOWNLOAD_ONLY_NEW) {
interpretations = updateInterpretations(lastUpdated);
} else {
interpretations = updateInterpretations(null);
}
List<InterpretationComment> comments = updateInterpretationComments(interpretations);
List<User> users = updateInterpretationUsers(interpretations, comments);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.hisp.dhis.android.dashboard.api.controllers;

import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.merge;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toMap;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.unwrapResponse;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.raizlabs.android.dbflow.sql.language.Select;
import com.squareup.okhttp.HttpUrl;
import com.squareup.picasso.NetworkPolicy;
import com.squareup.picasso.Picasso;

import org.hisp.dhis.android.dashboard.api.models.Dashboard;
import org.hisp.dhis.android.dashboard.api.models.DashboardElement;
import org.hisp.dhis.android.dashboard.api.models.DashboardItem;
import org.hisp.dhis.android.dashboard.api.models.DataMap;
import org.hisp.dhis.android.dashboard.api.models.meta.DbOperation;
import org.hisp.dhis.android.dashboard.api.network.APIException;
import org.hisp.dhis.android.dashboard.api.network.BaseMapLayerDhisTransformation;
import org.hisp.dhis.android.dashboard.api.network.DhisApi;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.ResourceType;
import org.hisp.dhis.android.dashboard.api.utils.DbUtils;
import org.hisp.dhis.android.dashboard.api.utils.PicassoProvider;
import org.joda.time.DateTime;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

public class MapController {
private DhisApi mDhisApi;

private static List<DataMap> mDataMaps;

public MapController(DhisApi dhisApi) {
mDhisApi = dhisApi;
}

public static List<DataMap> queryDataMaps() {
return new Select().from(DataMap.class)
.queryList();
}

public static DataMap getDataMap(String uid) {
DataMap dataMapToReturn = null;

if (mDataMaps == null)
mDataMaps = queryDataMaps();

for (DataMap dataMap:mDataMaps) {
if (dataMap.getUId().equals(uid))
dataMapToReturn = dataMap;
}

return dataMapToReturn;
}

public void syncDataMaps() throws APIException {
getDataMapsFromServer();
}

private void getDataMapsFromServer() throws APIException {
DateTime lastUpdated = DateTimeManager.getInstance()
.getLastUpdated(ResourceType.DASHBOARDS);
DateTime serverDateTime = mDhisApi.getSystemInfo()
.getServerDate();

List<DataMap> dataMaps = updateDataMaps(lastUpdated);

Queue<DbOperation> operations = new LinkedList<>();
operations.addAll(DbUtils.createOperations(queryDataMaps(), dataMaps));

DbUtils.applyBatch(operations);
DateTimeManager.getInstance()
.setLastUpdated(ResourceType.DASHBOARDS, serverDateTime);
}

private List<DataMap> updateDataMaps(DateTime lastUpdated) throws APIException {
final Map<String, String> QUERY_MAP_BASIC = new HashMap<>();
final Map<String, String> QUERY_MAP_FULL = new HashMap<>();
final String BASE = "id,created,lastUpdated,name,displayName,access";

QUERY_MAP_BASIC.put("fields", "id");
QUERY_MAP_FULL.put("fields", BASE + "basemap,latitude,longitude,zoom");

/* if (lastUpdated != null) {
QUERY_MAP_FULL.put("filter",
"lastUpdated:gt:" + lastUpdated.toLocalDateTime().toString());
}*/

// List of dashboards with UUIDs (without content). This list is used
// only to determine what was removed on server.
List<DataMap> actualDataMaps = unwrapResponse(mDhisApi
.getDataMaps(QUERY_MAP_BASIC), "maps");

// List of updated dashboards with content.
List<DataMap> updatedDataMaps = unwrapResponse(mDhisApi
.getDataMaps(QUERY_MAP_FULL), "maps");

// List of persisted dashboards.
List<DataMap> persistedDataMaps = queryDataMaps();

return merge(actualDataMaps, updatedDataMaps, persistedDataMaps);
}

private String getMapUIDs(String request) {
HttpUrl url = HttpUrl.parse(request);

String uid = url.pathSegments().get(3);

return uid;
}

}
Loading

0 comments on commit a60e8f2

Please sign in to comment.