diff --git a/opensrp-eusm/src/main/java/org/smartregister/eusm/interactor/TaskRegisterActivityInteractor.java b/opensrp-eusm/src/main/java/org/smartregister/eusm/interactor/TaskRegisterActivityInteractor.java index 7eae085..eda8cce 100644 --- a/opensrp-eusm/src/main/java/org/smartregister/eusm/interactor/TaskRegisterActivityInteractor.java +++ b/opensrp-eusm/src/main/java/org/smartregister/eusm/interactor/TaskRegisterActivityInteractor.java @@ -2,16 +2,23 @@ import android.content.IntentFilter; +import androidx.core.util.Pair; import androidx.localbroadcastmanager.content.LocalBroadcastManager; +import com.google.gson.JsonArray; + import org.json.JSONException; import org.json.JSONObject; import org.smartregister.domain.Event; +import org.smartregister.domain.Geometry; +import org.smartregister.domain.Location; import org.smartregister.eusm.application.EusmApplication; import org.smartregister.eusm.contract.TaskRegisterActivityContract; import org.smartregister.eusm.domain.StructureDetail; +import org.smartregister.eusm.repository.AppStructureRepository; import org.smartregister.eusm.util.AppConstants; import org.smartregister.eusm.util.AppUtils; +import org.smartregister.repository.BaseRepository; import org.smartregister.tasking.receiver.TaskGenerationReceiver; import org.smartregister.util.AppExecutors; @@ -61,7 +68,26 @@ public void saveRecordGps(JSONObject form, TaskRegisterActivityContract.InteractorCallBack interactorCallBack, StructureDetail structureDetail) { saveEventAndInitiateProcessing(AppConstants.EncounterType.RECORD_GPS, form, "", interactorCallBack, AppConstants.EventEntityType.SERVICE_POINT); - //updates the task + + //updates the structure with coordinates + Pair latLngPair = AppUtils.getLatLongFromForm(form); + if (latLngPair != null) { + AppStructureRepository structureRepository = EusmApplication.getInstance().getStructureRepository(); + Location location = structureRepository.getLocationById(structureDetail.getStructureId()); + location.setSyncStatus(BaseRepository.TYPE_Created); + + JsonArray jsonArray = new JsonArray(); + jsonArray.add(latLngPair.second); + jsonArray.add(latLngPair.first); + + Geometry geometry = new Geometry(); + geometry.setCoordinates(jsonArray); + geometry.setType(Geometry.GeometryType.POINT); + + location.setGeometry(geometry); + + structureRepository.addOrUpdate(location); + } } @Override diff --git a/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppConstants.java b/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppConstants.java index ac9122a..83345b6 100644 --- a/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppConstants.java +++ b/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppConstants.java @@ -128,6 +128,7 @@ interface BusinessStatus { interface JsonFormKey { String PRODUCT_PICTURE = "product_picture"; + String GPS = "gps"; } interface CardDetailKeys { diff --git a/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppUtils.java b/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppUtils.java index 9e13acf..35a056e 100644 --- a/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppUtils.java +++ b/opensrp-eusm/src/main/java/org/smartregister/eusm/util/AppUtils.java @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable; import androidx.annotation.NonNull; +import androidx.core.util.Pair; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -25,7 +26,6 @@ import org.smartregister.eusm.application.EusmApplication; import org.smartregister.tasking.util.Utils; import org.smartregister.util.Cache; -import org.smartregister.util.CacheableData; import org.smartregister.util.JsonFormUtils; import java.io.File; @@ -197,4 +197,24 @@ public static org.smartregister.domain.Location getOperationalAreaLocation(Strin .getLocationByNameAndGeoLevel(operationalArea, "2");//restrict to district geographic level }); } + + public static Pair getLatLongFromForm(@NonNull JSONObject form) { + JSONArray formFields = JsonFormUtils.getMultiStepFormFields(form); + for (int i = 0; i < formFields.length(); i++) { + JSONObject fieldObject = formFields.optJSONObject(i); + if (fieldObject != null) { + String key = fieldObject.optString(JsonFormConstants.KEY); + if (AppConstants.JsonFormKey.GPS.equals(key)) { + String value = fieldObject.optString(JsonFormConstants.VALUE); + String[] values = value.split(" "); + if (values.length >= 2) { + float latitude = Float.parseFloat(values[0]); + float longitude = Float.parseFloat(values[1]); + return Pair.create(latitude, longitude); + } + } + } + } + return null; + } } diff --git a/opensrp-eusm/src/test/java/org/smartregister/eusm/adapter/StructureRegisterAdapterTest.java b/opensrp-eusm/src/test/java/org/smartregister/eusm/adapter/StructureRegisterAdapterTest.java index 1fd8211..8850d9f 100644 --- a/opensrp-eusm/src/test/java/org/smartregister/eusm/adapter/StructureRegisterAdapterTest.java +++ b/opensrp-eusm/src/test/java/org/smartregister/eusm/adapter/StructureRegisterAdapterTest.java @@ -1,5 +1,6 @@ package org.smartregister.eusm.adapter; +import android.app.Activity; import android.view.View; import android.widget.LinearLayout; @@ -9,6 +10,7 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.Robolectric; import org.robolectric.RuntimeEnvironment; import org.smartregister.eusm.BaseUnitTest; import org.smartregister.eusm.domain.StructureDetail; @@ -30,10 +32,13 @@ public class StructureRegisterAdapterTest extends BaseUnitTest { @Mock private View.OnClickListener onClickListener; + private Activity activity; + @Before public void setUp() { MockitoAnnotations.initMocks(this); - structureRegisterAdapter = spy(new StructureRegisterAdapter(RuntimeEnvironment.application, onClickListener)); + activity = Robolectric.buildActivity(Activity.class).get(); + structureRegisterAdapter = spy(new StructureRegisterAdapter(activity, onClickListener)); } @Test @@ -81,7 +86,7 @@ public void testOnBindViewHolderShouldPopulateDataViews() { verify(viewHolder).setCommune(eq(structureDetail.getCommune())); verify(viewHolder).setTaskStatus(eq(structureDetail)); verify(viewHolder).setServicePointName(eq(structureDetail.getEntityName())); - verify(viewHolder).setServicePointType(eq(structureDetail)); + verify(viewHolder).setServicePointType(eq(structureDetail), eq(activity)); verify(viewHolder).setServicePointIcon(eq(structureDetail.getTaskStatus()), eq(structureDetail.getStructureType())); } } \ No newline at end of file