Skip to content

Commit

Permalink
moved fuzzy search to config activity
Browse files Browse the repository at this point in the history
code improvement
  • Loading branch information
chaneylc committed Jul 12, 2023
1 parent 3f1c6b0 commit eb8381a
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -43,10 +42,9 @@
import com.fieldbook.tracker.adapters.InfoBarAdapter;
import com.fieldbook.tracker.brapi.model.Observation;
import com.fieldbook.tracker.database.DataHelper;
import com.fieldbook.tracker.database.dao.ObservationUnitDao;
import com.fieldbook.tracker.database.dao.StudyDao;
import com.fieldbook.tracker.database.models.ObservationModel;
import com.fieldbook.tracker.database.models.ObservationUnitModel;
import com.fieldbook.tracker.interfaces.FieldSwitcher;
import com.fieldbook.tracker.objects.FieldObject;
import com.fieldbook.tracker.objects.GeoNavHelper;
import com.fieldbook.tracker.objects.InfoBarModel;
Expand All @@ -59,9 +57,11 @@
import com.fieldbook.tracker.traits.LayoutCollections;
import com.fieldbook.tracker.traits.PhotoTraitLayout;
import com.fieldbook.tracker.utilities.CategoryJsonUtil;
import com.fieldbook.tracker.utilities.FieldSwitchImpl;
import com.fieldbook.tracker.utilities.InfoBarHelper;
import com.fieldbook.tracker.utilities.LocationCollectorUtil;
import com.fieldbook.tracker.utilities.SnackbarUtils;
import com.fieldbook.tracker.utilities.SoundHelperImpl;
import com.fieldbook.tracker.utilities.TapTargetUtil;
import com.fieldbook.tracker.utilities.Utils;
import com.fieldbook.tracker.views.CollectInputView;
Expand Down Expand Up @@ -126,6 +126,11 @@ public class CollectActivity extends ThemedActivity
@Inject
InfoBarHelper infoBarHelper;

@Inject
FieldSwitchImpl fieldSwitcher;

@Inject
SoundHelperImpl soundHelper;
public static boolean searchReload;
public static String searchRange;
public static String searchPlot;
Expand Down Expand Up @@ -260,18 +265,11 @@ public void handleMessage(Message msg) {
checkForInitialBarcodeSearch();
}

private void switchField(int studyId, String fieldName, @Nullable String obsUnitId) {
private void switchField(int studyId, @Nullable String obsUnitId) {

try {

//updates obs. range view in database
database.switchField(studyId);

FieldObject fo = database.getFieldObject(studyId);

ep.edit().putString(GeneralKeys.UNIQUE_NAME, fo.getUnique_id()).apply();
ep.edit().putString(GeneralKeys.PRIMARY_NAME, fo.getPrimary_id()).apply();
ep.edit().putString(GeneralKeys.SECONDARY_NAME, fo.getSecondary_id()).apply();
fieldSwitcher.switchField(studyId);

rangeBox.setAllRangeID();
int[] rangeID = rangeBox.getRangeID();
Expand All @@ -289,11 +287,7 @@ private void switchField(int studyId, String fieldName, @Nullable String obsUnit
moveToSearch("id", rangeID, null, null, obsUnitId, -1);
}

//update selected item in field adapter using preference
ep.edit().putString(GeneralKeys.FIELD_FILE, fieldName).apply();
ep.edit().putInt(GeneralKeys.SELECTED_FIELD_ID, studyId).apply();

playSound("hero_simple_celebration");
soundHelper.playCelebrate();

} catch (Exception e) {

Expand Down Expand Up @@ -337,7 +331,7 @@ private void checkForInitialBarcodeSearch() {

if (fo != null && fo.getExp_name() != null) {

switchField(model.getStudy_id(), fo.getExp_name(), barcode);
switchField(model.getStudy_id(), barcode);

}
}
Expand Down Expand Up @@ -477,22 +471,6 @@ public void refreshMain() {
refreshLock();
}

@Override
public void playSound(String sound) {
try {
int resID = getResources().getIdentifier(sound, "raw", getPackageName());
MediaPlayer chimePlayer = MediaPlayer.create(CollectActivity.this, resID);
chimePlayer.start();

chimePlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
} catch (Exception ignore) {
}
}

/**
* Is used to ensure the UI entered data is within the bounds of the trait's min/max
*
Expand Down Expand Up @@ -528,7 +506,7 @@ public boolean validateData() {
removeTrait(trait);
collectInputView.clear();

playSound("error");
soundHelper.playError();

return false;
}
Expand Down Expand Up @@ -1678,15 +1656,15 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

//play success or error sound if the plotId was not found
if (success) {
playSound("hero_simple_celebration");
soundHelper.playCelebrate();
} else {
boolean found = false;
FieldObject studyObj = null;
ObservationUnitModel[] models = ObservationUnitDao.Companion.getAll();
ObservationUnitModel[] models = database.getAllObservationUnits();
for (ObservationUnitModel m : models) {
if (m.getObservation_unit_db_id().equals(inputPlotId)) {

FieldObject study = StudyDao.Companion.getFieldObject(m.getStudy_id());
FieldObject study = database.getFieldObject(m.getStudy_id());
if (study != null && study.getExp_name() != null) {
studyObj = study;
found = true;
Expand All @@ -1703,11 +1681,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String msg = getString(R.string.act_collect_barcode_search_exists_in_other_field, fieldName);

SnackbarUtils.showNavigateSnack(getLayoutInflater(), findViewById(R.id.traitHolder), msg, 8000, null,
(v) -> switchField(studyId, fieldName, null));
(v) -> switchField(studyId, null));

} else {

playSound("alert_error");
soundHelper.playError();

Utils.makeToast(getApplicationContext(), getString(R.string.main_toolbar_moveto_no_match));

Expand Down Expand Up @@ -2060,4 +2038,16 @@ public void onInfoBarClicked(int position) {
infoBarHelper.showInfoBarChoiceDialog(position);

}

@NonNull
@Override
public FieldSwitcher getFieldSwitcher() {
return fieldSwitcher;
}

@NonNull
@Override
public SoundHelperImpl getSoundHelper() {
return soundHelper;
}
}
142 changes: 124 additions & 18 deletions app/src/main/java/com/fieldbook/tracker/activities/ConfigActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@
import com.fieldbook.tracker.brapi.service.BrAPIService;
import com.fieldbook.tracker.database.DataHelper;
import com.fieldbook.tracker.database.dao.StudyDao;
import com.fieldbook.tracker.database.models.ObservationUnitModel;
import com.fieldbook.tracker.objects.FieldObject;
import com.fieldbook.tracker.objects.TraitObject;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.AppLanguageUtil;
import com.fieldbook.tracker.utilities.CSVWriter;
import com.fieldbook.tracker.utilities.Constants;
import com.fieldbook.tracker.utilities.FieldSwitchImpl;
import com.fieldbook.tracker.utilities.OldPhotosMigrator;
import com.fieldbook.tracker.utilities.SoundHelperImpl;
import com.fieldbook.tracker.utilities.TapTargetUtil;
import com.fieldbook.tracker.utilities.Utils;
import com.fieldbook.tracker.utilities.ZipUtil;
Expand Down Expand Up @@ -111,6 +114,10 @@ public class ConfigActivity extends ThemedActivity {
private final Runnable exportData = () -> new ExportDataTask().execute(0);
@Inject
public DataHelper database;
@Inject
public FieldSwitchImpl fieldSwitcher;
@Inject
public SoundHelperImpl soundHelper;
Handler mHandler = new Handler();
boolean doubleBackToExitPressedOnce = false;
ListView settingsList;
Expand Down Expand Up @@ -711,18 +718,131 @@ private void showSaveDialog() {
});
}

private void resolveFuzzySearchResult(FieldObject f, @Nullable String plotId) {

soundHelper.playCelebrate();

int studyId = ep.getInt(GeneralKeys.SELECTED_FIELD_ID, 0);

int newStudyId = f.getExp_id();

if (studyId != newStudyId) {

switchField(newStudyId);

}

Intent intent = new Intent(this, CollectActivity.class);
CollectActivity.reloadData = true;

if (plotId != null) {

ep.edit().putString(GeneralKeys.LAST_PLOT, plotId).apply();

}

startActivity(intent);

}
@Nullable
private FieldObject searchStudiesForBarcode(String barcode) {

// first, search to try and match study alias (brapi stores study_db_id here)
ArrayList<FieldObject> fields = database.getAllFieldObjects();

// start by searching for alias
for (FieldObject f : fields) {

if (f != null && f.getExp_alias() != null && f.getExp_alias().equals(barcode)) {

return f;

}
}

// second, if field is not found search for study name
for (FieldObject f : fields) {

if (f != null && f.getExp_name() != null && f.getExp_name().equals(barcode)) {

return f;

}
}

return null;
}

@Nullable
private ObservationUnitModel searchPlotsForBarcode(String barcode) {

// search for barcode in database
ObservationUnitModel[] models = database.getAllObservationUnits();
for (ObservationUnitModel m : models) {
if (m.getObservation_unit_db_id().equals(barcode)) {

return m;
}
}

return null;
}

//1) study alias, 2) study names, 3) plotdbids
private void fuzzyBarcodeSearch(String barcode) {

// search for studies
FieldObject f = searchStudiesForBarcode(barcode);

if (f == null) {

// search for plots
ObservationUnitModel m = searchPlotsForBarcode(barcode);

if (m != null && m.getStudy_id() != -1) {

FieldObject study = database.getFieldObject(m.getStudy_id());

resolveFuzzySearchResult(study, barcode);

} else {

soundHelper.playError();

Utils.makeToast(this, getString(R.string.act_config_fuzzy_search_failed, barcode));
}

} else {

resolveFuzzySearchResult(f, null);

}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_STORAGE_DEFINER) {
if (resultCode != Activity.RESULT_OK) finish();
} else if (requestCode == REQUEST_BARCODE) {
if (resultCode == RESULT_OK) {
// store barcode value as data

// get barcode from scan result
IntentResult plotDataResult = IntentIntegrator.parseActivityResult(resultCode, data);
String scannedBarcode = plotDataResult.getContents();
startActivity(new Intent(this, CollectActivity.class)
.putExtra("barcode", scannedBarcode));

try {

fuzzyBarcodeSearch(scannedBarcode);

} catch (Exception e) {

e.printStackTrace();

Utils.makeToast(this, getString(R.string.act_config_fuzzy_search_error, scannedBarcode));

soundHelper.playError();
}
}
}
}
Expand Down Expand Up @@ -871,22 +991,8 @@ public void selectFirstField() {
*/
private void switchField(int studyId) {

FieldObject f = StudyDao.Companion.getFieldObject(studyId);

if (f != null) {
fieldSwitcher.switchField(studyId);

database.switchField(studyId);

//clear field selection after updates
ep.edit().putInt(GeneralKeys.SELECTED_FIELD_ID, studyId)
.putString(GeneralKeys.FIELD_FILE, f.getExp_name())
.putString(GeneralKeys.FIELD_OBS_LEVEL, f.getObservation_level())
.putString(GeneralKeys.UNIQUE_NAME, f.getUnique_id())
.putString(GeneralKeys.PRIMARY_NAME, f.getPrimary_id())
.putString(GeneralKeys.SECONDARY_NAME, f.getSecondary_id())
.putBoolean(GeneralKeys.IMPORT_FIELD_FINISHED, true)
.putString(GeneralKeys.LAST_PLOT, null).apply();
}
}

private class ExportDataTask extends AsyncTask<Integer, Integer, Integer> {
Expand Down
Loading

0 comments on commit eb8381a

Please sign in to comment.