Skip to content

Commit

Permalink
Merge pull request #830 from OpenSRP/fix-app-crash-hf
Browse files Browse the repository at this point in the history
Fix sync and populating locations on family register form for release app versions.
  • Loading branch information
rkodev authored Sep 18, 2019
2 parents b701adc + 6622a22 commit 18c1148
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.apache.commons.lang3.StringUtils;
import org.smartregister.chw.core.custom_views.NavigationMenu;
import org.smartregister.chw.core.model.CoreFamilyRegisterModel;
import org.smartregister.chw.core.utils.CoreConstants;
import org.smartregister.family.activity.BaseFamilyRegisterActivity;
import org.smartregister.family.model.BaseFamilyRegisterModel;
Expand All @@ -32,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
protected void initializePresenter() {
presenter = new BaseFamilyRegisterPresenter(this, new BaseFamilyRegisterModel());
presenter = new BaseFamilyRegisterPresenter(this, new CoreFamilyRegisterModel());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package org.smartregister.chw.core.model;

import com.vijay.jsonwizard.constants.JsonFormConstants;

import org.json.JSONObject;
import org.smartregister.chw.core.application.CoreChwApplication;
import org.smartregister.chw.core.utils.CoreJsonFormUtils;
import org.smartregister.family.model.BaseFamilyProfileModel;

public abstract class CoreFamilyProfileModel extends BaseFamilyProfileModel {

public CoreFamilyProfileModel(String familyName) {
super(familyName);
}

@Override
public JSONObject getFormAsJson(String formName, String entityId, String currentLocationId) throws Exception {
JSONObject form = super.getFormAsJson(formName, entityId, currentLocationId);
CoreJsonFormUtils.populateLocationsTree(form, JsonFormConstants.STEP1, CoreChwApplication.getInstance().getAllowedLocationLevels(), "nearest_facility");
return form;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.smartregister.chw.core.model;

import com.vijay.jsonwizard.constants.JsonFormConstants;

import org.json.JSONObject;
import org.smartregister.chw.core.application.CoreChwApplication;
import org.smartregister.chw.core.utils.CoreJsonFormUtils;
import org.smartregister.family.model.BaseFamilyRegisterModel;

public class CoreFamilyRegisterModel extends BaseFamilyRegisterModel {

@Override
public JSONObject getFormAsJson(String formName, String entityId, String currentLocationId) throws Exception {
JSONObject form = super.getFormAsJson(formName, entityId, currentLocationId);
CoreJsonFormUtils.populateLocationsTree(form, JsonFormConstants.STEP1, CoreChwApplication.getInstance().getAllowedLocationLevels(), "nearest_facility");
return form;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.util.Pair;

import com.vijay.jsonwizard.constants.JsonFormConstants;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Triple;
import org.json.JSONObject;
Expand Down Expand Up @@ -70,6 +72,7 @@ public FamilyProfileExtendedContract.View getView() {
@Override
public void startFormForEdit(CommonPersonObjectClient client) {
JSONObject form = CoreJsonFormUtils.getAutoPopulatedJsonEditFormString(CoreConstants.JSON_FORM.getFamilyDetailsRegister(), getView().getApplicationContext(), client, Utils.metadata().familyRegister.updateEventType);
CoreJsonFormUtils.populateLocationsTree(form, JsonFormConstants.STEP1, CoreChwApplication.getInstance().getAllowedLocationLevels(), "nearest_facility");
try {
getView().startFormActivity(form);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.text.TextUtils;
import android.util.Pair;

import com.google.common.reflect.TypeToken;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.domain.Form;

Expand All @@ -27,6 +28,7 @@
import org.smartregister.commonregistry.CommonPersonObjectClient;
import org.smartregister.domain.Photo;
import org.smartregister.domain.ProfileImage;
import org.smartregister.domain.form.FormLocation;
import org.smartregister.domain.tag.FormTag;
import org.smartregister.family.FamilyLibrary;
import org.smartregister.family.util.Constants;
Expand Down Expand Up @@ -867,4 +869,37 @@ public static HashMap<String, String> getEducationLevels(Context context) {
return educationLevels;
}

public static void populateLocationsTree(JSONObject form, String stepName,
ArrayList<String> healthFacilities, String locationTree) {
try {
JSONArray questions = form.getJSONObject(stepName).getJSONArray(JsonFormConstants.FIELDS);
healthFacilities.add(0, "Country");
healthFacilities.add(1, "Region");
healthFacilities.add(2, "District");
List<String> defaultFacility = LocationHelper.getInstance().generateDefaultLocationHierarchy(healthFacilities);
List<FormLocation> upToFacilities = LocationHelper.getInstance().generateLocationHierarchyTree(false, healthFacilities);

String defaultFacilityString = AssetHandler.javaToJsonString(defaultFacility,
new TypeToken<List<String>>() {
}.getType());

String upToFacilitiesString = AssetHandler.javaToJsonString(upToFacilities,
new TypeToken<List<FormLocation>>() {
}.getType());

for (int i = 0; i < questions.length(); i++) {
if (questions.getJSONObject(i).getString(JsonFormConstants.KEY).equals(locationTree)) {
if (StringUtils.isNotBlank(upToFacilitiesString)) {
questions.getJSONObject(i).put(JsonFormConstants.TREE, new JSONArray(upToFacilitiesString));
}
if (StringUtils.isNotBlank(defaultFacilityString)) {
questions.getJSONObject(i).put(JsonFormConstants.DEFAULT, defaultFacilityString);
}
}
}
} catch (Exception e) {
Timber.e(e);
}
}

}
4 changes: 2 additions & 2 deletions opensrp-chw-hf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ android {
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.2.3"
versionName "1.2.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
buildConfigField "String", 'opensrp_url', '"https://boresha-afya.smartregister.org/opensrp/"'
Expand Down Expand Up @@ -63,7 +63,7 @@ android {
buildConfigField "int", "REPORT_INDICATOR_GENERATION_MINUTES", '15'
buildConfigField "int", "HOME_VISIT_MINUTES", '60'
buildConfigField "boolean", 'SUPPORT_REPORT', 'false'
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS", '{"Ward" , "Health Facility"}'
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS", '{"Ward" , "Health Facility", "Village", "Village Sublocations"}'
buildConfigField "String", 'DEFAULT_LOCATION', '"Health Facility"'
}

Expand Down
6 changes: 3 additions & 3 deletions opensrp-chw/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ android {
dimension = 'baseDimension'
applicationIdSuffix ".ba"
versionCode 1
versionName "1.0.0"
versionName "1.0.1"
buildConfigField "String", 'opensrp_url', '"https://boresha-afya.smartregister.org/opensrp/"'
buildConfigField "String", 'opensrp_url_debug', '"https://boresha-afya-stage.smartregister.org/opensrp/"'
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS", '{"Ward" , "Health Facility"}'
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS", '{"Ward" , "Health Facility", "Village", "Village Sublocations"}'
buildConfigField "String[]", "ALLOWED_LOCATION_LEVELS_DEBUG", '{"MOH Jhpiego Facility Name" , "Village"}'
buildConfigField "String", 'DEFAULT_LOCATION', '"Health Facility"'
buildConfigField "String", 'DEFAULT_LOCATION', '"Village Sublocations"'
buildConfigField "String", 'DEFAULT_LOCATION_DEBUG', '"Village"'
buildConfigField "boolean", 'SUPPORT_QR', 'true'
buildConfigField "boolean", 'SUPPORT_REPORT', 'false'
Expand Down

0 comments on commit 18c1148

Please sign in to comment.