Skip to content

Commit

Permalink
update structure with gps coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
bennsimon committed Jul 5, 2021
1 parent f600fde commit e6a34c5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Float, Float> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ interface BusinessStatus {

interface JsonFormKey {
String PRODUCT_PICTURE = "product_picture";
String GPS = "gps";
}

interface CardDetailKeys {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -197,4 +197,24 @@ public static org.smartregister.domain.Location getOperationalAreaLocation(Strin
.getLocationByNameAndGeoLevel(operationalArea, "2");//restrict to district geographic level
});
}

public static Pair<Float, Float> 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.smartregister.eusm.adapter;

import android.app.Activity;
import android.view.View;
import android.widget.LinearLayout;

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit e6a34c5

Please sign in to comment.