Skip to content

Commit

Permalink
update location search from tree dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
bennsimon committed Mar 5, 2021
1 parent 95a1d4a commit 5edf108
Show file tree
Hide file tree
Showing 22 changed files with 191 additions and 68 deletions.
6 changes: 3 additions & 3 deletions opensrp-eusm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ android {
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "0.0.3"
versionName "0.0.6"
multiDexEnabled true
buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l"
buildConfigField "boolean", "TIME_CHECK", "false"
Expand Down Expand Up @@ -178,7 +178,7 @@ dependencies {

jarJar 'com.ibm.fhir:fhir-model:4.2.3'

implementation('org.smartregister:opensrp-client-core:4.2.4-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-core:4.2.9-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.android.support', module: 'appcompat-v7'
exclude group: 'com.github.ybq', module: 'Android-SpinKit'
Expand Down Expand Up @@ -215,7 +215,7 @@ dependencies {
}

// implementation(project(':opensrp-tasking')) {
implementation('org.smartregister:opensrp-client-tasking:0.0.3-v11-EUSM-SNAPSHOT') {
implementation('org.smartregister:opensrp-client-tasking:0.0.3-v12-EUSM-SNAPSHOT') {
transitive = true
exclude group: 'org.smartregister', module: 'opensrp-client-core'
exclude group: 'com.ibm.fhir', module: 'fhir-model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int po
if (AppConstants.EncounterType.FIX_PROBLEM.equals(taskDetail.getTaskCode())) {
productName = String.format(context.getString(R.string.fix_problem_prefix), productName);
}

if( AppConstants.TaskCode.RECORD_GPS.equals(productName)){
productName = context.getString(R.string.record_gps_task_title);
} else if( AppConstants.TaskCode.SERVICE_POINT_CHECK.equals(productName)){
productName = context.getString(R.string.service_point_check_task_title);
}

taskRegisterViewHolder.setProductName(productName, taskDetail.isChecked());
taskRegisterViewHolder.setProductSerial(taskDetail);
taskRegisterViewHolder.setProductImage(taskDetail);
Expand Down
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.content.Context;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -18,13 +19,13 @@

public class StructureRegisterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private final Context context;
private final Activity context;

private final List<StructureDetail> structureDetailList = new ArrayList<>();

private final View.OnClickListener registerActionHandler;

public StructureRegisterAdapter(Context context, View.OnClickListener registerActionHandler) {
public StructureRegisterAdapter(Activity context, View.OnClickListener registerActionHandler) {
this.context = context;
this.registerActionHandler = registerActionHandler;
}
Expand Down Expand Up @@ -52,7 +53,7 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
StructureRegisterViewHolder viewHolder = (StructureRegisterViewHolder) holder;
viewHolder.setServicePointName(structureDetail.getEntityName());
viewHolder.setServicePointIcon(structureDetail.getTaskStatus(), structureDetail.getStructureType());
viewHolder.setServicePointType(structureDetail);
viewHolder.setServicePointType(structureDetail, context);
viewHolder.setCommune(structureDetail.getCommune());
viewHolder.setTaskStatus(structureDetail);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import org.smartregister.eusm.activity.LoginActivity;
import org.smartregister.eusm.config.AppSyncConfiguration;
import org.smartregister.eusm.config.AppTaskingLibraryConfiguration;
import org.smartregister.eusm.config.EusmContext;
import org.smartregister.eusm.config.EusmStockSyncConfiguration;
import org.smartregister.eusm.config.ServicePointType;
import org.smartregister.eusm.job.AppJobCreator;
import org.smartregister.eusm.processor.AppClientProcessor;
import org.smartregister.eusm.repository.AppLocationRepository;
import org.smartregister.eusm.repository.AppRepository;
import org.smartregister.eusm.repository.AppStructureRepository;
import org.smartregister.eusm.repository.AppTaskRepository;
Expand Down Expand Up @@ -96,6 +98,8 @@ public class EusmApplication extends DrishtiApplication implements TimeChangedBr

private EventClientRepository eventClientRepository;

private AppLocationRepository appLocationRepository;

public static synchronized EusmApplication getInstance() {
return (EusmApplication) mInstance;
}
Expand Down Expand Up @@ -133,7 +137,7 @@ private static String[] getFtsSortFields(String tableName) {
public void onCreate() {
super.onCreate();
mInstance = this;
context = Context.getInstance();
context = EusmContext.getInstance();
context.updateApplicationContext(getApplicationContext());
context.updateCommonFtsObject(createCommonFtsObject());
CoreLibrary.init(context, new AppSyncConfiguration(), BuildConfig.BUILD_TIMESTAMP);
Expand Down Expand Up @@ -252,7 +256,7 @@ public TaskRepository getTaskRepository() {
}

public LocationRepository getLocationRepository() {
return CoreLibrary.getInstance().context().getLocationRepository();
return context.getLocationRepository();
}

public AllSettings getSettingsRepository() {
Expand Down Expand Up @@ -424,4 +428,11 @@ public EventClientRepository getEventClientRepository() {
}
return eventClientRepository;
}

public AppLocationRepository getAppLocationRepository() {
if (appLocationRepository == null) {
appLocationRepository = new AppLocationRepository();
}
return appLocationRepository;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public boolean isMDA() {

@Override
public String getCurrentLocationId() {
Location currentOperationalArea = Utils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
Location currentOperationalArea = AppUtils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
return currentOperationalArea == null ? null : currentOperationalArea.getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.smartregister.eusm.config;

import org.smartregister.Context;
import org.smartregister.eusm.repository.AppLocationRepository;
import org.smartregister.repository.LocationRepository;

public class EusmContext extends Context {

private AppLocationRepository appLocationRepository;

private static Context context = new EusmContext();

public static Context getInstance() {
if (context == null) {
context = new EusmContext();
}
return context;
}

@Override
public LocationRepository getLocationRepository() {
if (appLocationRepository == null) {
appLocationRepository = new AppLocationRepository();
}
return appLocationRepository;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.smartregister.tasking.contract.BaseDrawerContract;
import org.smartregister.tasking.util.LocationUtils;
import org.smartregister.tasking.util.TaskingLibraryConfiguration;
import org.smartregister.util.DisplayUtils;

import java.text.MessageFormat;
import java.util.List;
Expand Down Expand Up @@ -86,7 +87,7 @@ public Location getLastLocation() {

@Override
public void initializeAdapter() {
structureRegisterAdapter = new StructureRegisterAdapter(getContext(), registerActionHandler);
structureRegisterAdapter = new StructureRegisterAdapter(getActivity(), registerActionHandler);
clientsView.setAdapter(structureRegisterAdapter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.smartregister.eusm.application.EusmApplication;
import org.smartregister.eusm.domain.StructureDetail;
import org.smartregister.eusm.repository.AppStructureRepository;
import org.smartregister.eusm.util.AppUtils;
import org.smartregister.eusm.util.GeoJsonUtils;
import org.smartregister.tasking.contract.TaskingMapActivityContract;
import org.smartregister.tasking.interactor.TaskingMapInteractor;
Expand Down Expand Up @@ -72,6 +73,6 @@ public void run() {
}

protected Location getOperationalAreaLocation(String operationalArea) {
return Utils.getOperationalAreaLocation(operationalArea);
return AppUtils.getOperationalAreaLocation(operationalArea);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.smartregister.eusm.application.EusmApplication;
import org.smartregister.eusm.domain.StructureDetail;
import org.smartregister.eusm.repository.AppStructureRepository;
import org.smartregister.eusm.util.AppUtils;
import org.smartregister.tasking.util.PreferencesUtil;
import org.smartregister.tasking.util.Utils;

Expand All @@ -19,7 +20,7 @@ public StructureRegisterFragmentModel() {
}

public int countOfStructures(String nameFilter) {
Location location = Utils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
Location location = AppUtils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
if (location != null) {
return appStructureRepository.countOfStructures(nameFilter, location.getId(), PreferencesUtil.getInstance().getCurrentPlanId());
} else {
Expand All @@ -28,7 +29,7 @@ public int countOfStructures(String nameFilter) {
}

public List<StructureDetail> fetchStructures(int pageNo, String nameFilter) {
Location location = Utils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
Location location = AppUtils.getOperationalAreaLocation(PreferencesUtil.getInstance().getCurrentOperationalArea());
if (location != null) {
return appStructureRepository.fetchStructureDetails(pageNo, location.getId(), nameFilter, PreferencesUtil.getInstance().getCurrentPlanId());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public void onFetchedStructures(List<StructureDetail> structureDetails) {

@Override
public void onCountOfStructuresFetched(int count) {
getFragment().getPreviousButton().setVisibility(View.INVISIBLE);

totalCount = count;
totalPageCount = (int) Math.ceil((double) totalCount == 0 ? 1 : totalCount / (double) pageSize);
if (getFragment().getNextButton() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.smartregister.eusm.repository;

import android.content.ContentValues;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import org.apache.commons.lang3.StringUtils;
import org.smartregister.domain.Location;
import org.smartregister.repository.BaseRepository;
import org.smartregister.repository.LocationRepository;

import timber.log.Timber;

public class AppLocationRepository extends LocationRepository {

protected static final String GEOGRAPHICAL_LEVEL = "geographical_level";

private static final String CREATE_LOCATION_TABLE =
"CREATE TABLE " + LOCATION_TABLE + " (" +
ID + " VARCHAR NOT NULL PRIMARY KEY," +
UUID + " VARCHAR , " +
PARENT_ID + " VARCHAR , " +
NAME + " VARCHAR , " +
GEOGRAPHICAL_LEVEL + " VARCHAR , " +
SYNC_STATUS + " VARCHAR DEFAULT " + BaseRepository.TYPE_Synced + ", " +
GEOJSON + " VARCHAR NOT NULL ) ";

private static final String CREATE_LOCATION_NAME_INDEX = "CREATE INDEX "
+ LOCATION_TABLE + "_" + NAME + "_ind ON " + LOCATION_TABLE + "(" + NAME + ")";

public static void createTable(SQLiteDatabase database) {
database.execSQL(CREATE_LOCATION_TABLE);
database.execSQL(CREATE_LOCATION_NAME_INDEX);
}

public void addOrUpdate(Location location) {
if (StringUtils.isBlank(location.getId()))
throw new IllegalArgumentException("id not provided");
ContentValues contentValues = new ContentValues();
contentValues.put(ID, location.getId());
contentValues.put(UUID, location.getProperties().getUid());
contentValues.put(PARENT_ID, location.getProperties().getParentId());
contentValues.put(NAME, location.getProperties().getName());
contentValues.put(GEOGRAPHICAL_LEVEL, location.getProperties().getGeographicLevel());
contentValues.put(GEOJSON, gson.toJson(location));
contentValues.put(SYNC_STATUS, location.getSyncStatus());
getWritableDatabase().replace(getLocationTableName(), null, contentValues);
}

public Location getLocationByNameAndGeoLevel(String name, String level) {
try (Cursor cursor = getReadableDatabase().rawQuery("SELECT * FROM " + getLocationTableName() +
" WHERE " + NAME + " =? AND " + GEOGRAPHICAL_LEVEL + "=?", new String[]{name, level})) {
if (cursor.moveToFirst()) {
return readCursor(cursor);
}
} catch (Exception e) {
Timber.e(e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public List<TaskDetail> getTasksByStructureId(String structureId, String planId,

StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.UNIQUE_ID,
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.NAME,
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.QUANTITY,
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.MATERIAL_NUMBER,
StockRepository.STOCK_TABLE_NAME + "." + StockRepository.VALUE + " as stockQuantity",
StockRepository.STOCK_TABLE_NAME + "." + StockRepository.SERIAL_NUMBER + " as stockSerialNumber",
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.PHOTO_FILE_LOCATION,
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.CONDITION,
StockTypeRepository.STOCK_TYPE_TABLE_NAME + "." + StockTypeRepository.AVAILABILITY,
Expand Down Expand Up @@ -82,8 +82,8 @@ private TaskDetail readStructureTaskDetailCursor(Cursor cursor) {
String taskId = cursor.getString(cursor.getColumnIndex("taskId"));
// String taskLocation = cursor.getString(cursor.getColumnIndex("taskLocation"));
String productName = cursor.getString(cursor.getColumnIndex(StockTypeRepository.NAME));
String quantity = cursor.getString(cursor.getColumnIndex(StockTypeRepository.QUANTITY));
String productSerial = cursor.getString(cursor.getColumnIndex(StockTypeRepository.MATERIAL_NUMBER));
String stockQuantity = cursor.getString(cursor.getColumnIndex("stockQuantity"));
String stockSerialNumber = cursor.getString(cursor.getColumnIndex("stockSerialNumber"));
String productImage = cursor.getString(cursor.getColumnIndex(StockTypeRepository.PHOTO_FILE_LOCATION));
String productId = cursor.getString(cursor.getColumnIndex(StockTypeRepository.UNIQUE_ID));

Expand All @@ -106,11 +106,11 @@ private TaskDetail readStructureTaskDetailCursor(Cursor cursor) {
}
taskDetail.setEntityName(name);
}
taskDetail.setQuantity(quantity);
taskDetail.setQuantity(stockQuantity);
taskDetail.setTaskId(taskId);
taskDetail.setProductId(productId);
taskDetail.setProductImage(productImage);
taskDetail.setProductSerial(productSerial);
taskDetail.setProductSerial(stockSerialNumber);
taskDetail.setNonProductTask(productName == null);
taskDetail.setCondition(condition);
taskDetail.setAppropriateUsage(appropriateUsage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onCreate(SQLiteDatabase database) {
StockRepository.createTable(database);
CampaignRepository.createTable(database);
TaskRepository.createTable(database);
LocationRepository.createTable(database);
AppLocationRepository.createTable(database);
AppStructureRepository.createTable(database);
PlanDefinitionRepository.createTable(database);
PlanDefinitionSearchRepository.createTable(database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.smartregister.eusm.R;
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 All @@ -50,6 +52,8 @@

public class AppUtils extends Utils {

private static Cache<org.smartregister.domain.Location> cache = new Cache<>();

public static Float distanceFromUserLocation(@NonNull android.location.Location location) {
android.location.Location userLocation = EusmApplication.getInstance().getUserLocation();
if (userLocation != null && location != null) {
Expand Down Expand Up @@ -186,4 +190,11 @@ public static String getStringFromJsonElement(JsonObject jsonObject, String key)
JsonElement element = jsonObject.get(key);
return (element != null) ? element.getAsString() : AppConstants.CardDetailKeys.DISTANCE_META.equals(key) ? "-" : "";
}

public static org.smartregister.domain.Location getOperationalAreaLocation(String operationalArea) {
return cache.get(operationalArea, () -> {
return EusmApplication.getInstance().getAppLocationRepository()
.getLocationByNameAndGeoLevel(operationalArea, "2");//restrict to district geographic level
});
}
}
Loading

0 comments on commit 5edf108

Please sign in to comment.