Skip to content

Commit

Permalink
Merge pull request #773 from PhenoApps/issue/771/photo_file_name_fix
Browse files Browse the repository at this point in the history
Photo file name fixes for special characters
  • Loading branch information
trife authored Nov 30, 2023
2 parents 353ccf3 + 9cad12d commit 49add97
Show file tree
Hide file tree
Showing 31 changed files with 953 additions and 553 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ dependencies {
testImplementation 'org.powermock:powermock-module-junit4:2.0.9'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.9'

//androidx exif interface
implementation "androidx.exifinterface:exifinterface:1.3.6"

// Core library
androidTestImplementation "androidx.test:core:$coreVersion"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.fieldbook.tracker.activities;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
Expand Down Expand Up @@ -72,6 +71,7 @@
import com.fieldbook.tracker.utilities.FieldAudioHelper;
import com.fieldbook.tracker.utilities.KeyboardListenerHelper;
import com.fieldbook.tracker.utilities.JsonUtil;
import com.fieldbook.tracker.utilities.JsonUtil;
import com.fieldbook.tracker.utilities.LocationCollectorUtil;
import com.fieldbook.tracker.utilities.SnackbarUtils;
import com.fieldbook.tracker.utilities.SoundHelperImpl;
Expand Down Expand Up @@ -1066,8 +1066,9 @@ public void navigateToTrait(String trait) {
/**
* Helper function update user data in the memory based hashmap as well as
* the database
* @param trait, the TraitObject to be updated
* @param value the new string value to be saved in the database
*
* @param trait the trait object to update
* @param value the new string value to be saved in the database
* @param nullableRep the repeated value to update, could be null to represent the latest rep value
*/
public void updateObservation(TraitObject trait, String value, @Nullable String nullableRep) {
Expand Down Expand Up @@ -1104,7 +1105,7 @@ public void updateObservation(TraitObject trait, String value, @Nullable String
boolean pass = false;

if (trait.getFormat().equals("multicat")
|| CategoricalTraitLayout.isTraitCategorical(trait.getFormat())) {
|| CategoricalTraitLayout.isTraitCategorical(trait.getFormat())) {

if (value.equals("[]")) {

Expand All @@ -1113,7 +1114,7 @@ public void updateObservation(TraitObject trait, String value, @Nullable String
}

if (!pass) {
database.insertObservation(obsUnit, trait.getId(), value, person,
database.insertObservation(obsUnit, trait.getId(), trait.getFormat(), value, person,
getLocationByPreferences(), "", studyId, observationDbId,
lastSyncedTime, rep);
}
Expand All @@ -1131,7 +1132,7 @@ public void insertRep(String value, String rep) {
String person = getPerson();
String traitDbId = getTraitDbId();

database.insertObservation(obsUnit, traitDbId, value, person,
database.insertObservation(obsUnit, traitDbId, getTraitFormat(), value, person,
getLocationByPreferences(), "", expId, null, null, rep);
}

Expand Down Expand Up @@ -1172,7 +1173,7 @@ public void removeTrait(String parent) {

String exp_id = Integer.toString(ep.getInt(GeneralKeys.SELECTED_FIELD_ID, 0));
TraitObject trait = traitBox.getCurrentTrait();
if (database.isBrapiSynced(exp_id, getObservationUnit(), trait.getTrait(), getRep())) {
if (database.isBrapiSynced(exp_id, getObservationUnit(), trait.getId(), getRep())) {
brapiDelete(parent, true);
} else {
// Always remove existing trait before inserting again
Expand Down Expand Up @@ -1424,7 +1425,7 @@ else if(!fieldAudioHelper.isRecording()){

private void showMultiMeasureDeleteDialog() {

String labelValPref = ep.getString(GeneralKeys.LABELVAL_CUSTOMIZE,"value");
String labelValPref = ep.getString(GeneralKeys.LABELVAL_CUSTOMIZE, "value");

ObservationModel[] values = database.getRepeatedValues(
getStudyId(), getObservationUnit(), getTraitDbId());
Expand Down Expand Up @@ -2129,8 +2130,8 @@ public void insertPrintObservation(String size) {

String studyId = Integer.toString(ep.getInt(GeneralKeys.SELECTED_FIELD_ID, 0));

database.insertObservation(rangeBox.getPlotID(), trait.getId(), size,
ep.getString(GeneralKeys.FIRST_NAME, "") + " " + ep.getString(GeneralKeys.LAST_NAME, ""),
database.insertObservation(rangeBox.getPlotID(), trait.getId(), trait.getFormat(), size,
getPerson(),
getLocationByPreferences(), "", studyId, "",
null, null);

Expand Down Expand Up @@ -2345,7 +2346,7 @@ public ArrayList<String> getGeoNavPopupSpinnerItems () {
}
return new ArrayList<>();
}

@NonNull
@Override
public VibrateUtil getVibrator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,7 @@ private void showSaveDialog() {
}

if (allTraits.isChecked()) {
ArrayList<TraitObject> traits = database.getAllTraitObjects();
exportTrait.addAll(traits);
exportTrait.addAll(database.getAllTraitObjects());
}

checkDbBool = checkDB.isChecked();
Expand Down Expand Up @@ -1135,11 +1134,10 @@ protected Integer doInBackground(Integer... params) {
exportData = database.convertDatabaseToTable(newRanges, exportTrait);
CSVWriter csvWriter = new CSVWriter(fw, exportData);

ArrayList<String> labels = new ArrayList<>();
labels.addAll(Arrays.asList(newRanges));
for (TraitObject trait : exportTrait) labels.add(trait.getTrait());
ArrayList<String> labels = new ArrayList<>(Arrays.asList(newRanges));
for (TraitObject t : exportTrait) labels.add(t.getTrait());

csvWriter.writeTableFormat(labels.toArray(new String[] {}), labels.size(), traits);
csvWriter.writeTableFormat(labels.toArray(new String[]{}), newRanges.length, traits);

} catch (Exception e) {
fail = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ class DataGridActivity : ThemedActivity(), CoroutineScope by MainScope(), ITable
}
}

val traits = database.allTraitObjects;
val traits = database.allTraitObjects

//expensive database call, only asks for the unique name plot attr and all visible traits
val cursor = database.convertDatabaseToTable(arrayOf(uniqueHeader, rowHeader), mTraits)
val cursor =
database.convertDatabaseToTable(arrayOf(uniqueHeader, rowHeader), mTraits)

if (cursor.moveToFirst()) {

Expand Down Expand Up @@ -263,7 +264,8 @@ class DataGridActivity : ThemedActivity(), CoroutineScope by MainScope(), ITable

val t = traits.find { it.format in setOf("categorical", "multicat", "qualitative") }

val repeatedValues = database.getRepeatedValues(studyId, id, variable.id)
val repeatedValues =
database.getRepeatedValues(studyId, id, variable.id)
if (repeatedValues.size > 1) {
println("$studyId $id $variable has repeated values...!")
}
Expand All @@ -275,7 +277,7 @@ class DataGridActivity : ThemedActivity(), CoroutineScope by MainScope(), ITable
try {

cellValue = CategoryJsonUtil
.flattenMultiCategoryValue(CategoryJsonUtil.decode(value), showLabel)
.flattenMultiCategoryValue(decode(value), showLabel)

} catch (e: Exception) {

Expand Down Expand Up @@ -327,9 +329,11 @@ class DataGridActivity : ThemedActivity(), CoroutineScope by MainScope(), ITable

mTableView.setAdapter(mAdapter)

mAdapter.setAllItems(mTraits.map { HeaderData(it.trait, it.trait) },
mAdapter.setAllItems(
mTraits.map { HeaderData(it.trait, it.trait) },
mRowHeaders.map { HeaderData(it, it) },
dataMap.toList())
dataMap.toList()
)

//scroll to the position of the current trait/plot id
if (plotId != null && trait != null) {
Expand Down Expand Up @@ -434,7 +438,8 @@ class DataGridActivity : ThemedActivity(), CoroutineScope by MainScope(), ITable

val plotId = value.observation_unit_id

val traitIndex = mTraits.indexOfFirst { it.id == value.observation_variable_db_id.toString() }
val traitIndex =
mTraits.indexOfFirst { it.id == value.observation_variable_db_id.toString() }

navigateFromValueClicked(plotId, traitIndex, which + 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import com.fieldbook.tracker.objects.FieldFileObject;
import com.fieldbook.tracker.objects.FieldObject;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.DocumentTreeUtil;
import com.fieldbook.tracker.utilities.FieldSwitchImpl;
import com.fieldbook.tracker.utilities.SnackbarUtils;
import com.fieldbook.tracker.utilities.TapTargetUtil;
Expand Down Expand Up @@ -620,9 +619,6 @@ private void showFieldFileDialog(final String chosenFile, Boolean isCloud) {
Utils.makeToast(getApplicationContext(), getString(R.string.import_error_unsupported));
}

//utility call creates photos, audio and thumbnails folders under a new field folder
DocumentTreeUtil.Companion.createFieldDir(this, fieldFileName);

loadFile(fieldFile);

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.fieldbook.tracker.R
import com.fieldbook.tracker.adapters.SummaryAdapter
import com.fieldbook.tracker.database.DataHelper
import com.fieldbook.tracker.objects.TraitObject
import com.fieldbook.tracker.preferences.GeneralKeys
import com.fieldbook.tracker.utilities.CategoryJsonUtil
import com.google.gson.JsonParseException
Expand Down Expand Up @@ -80,7 +81,7 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {
}

private fun setupToolbar(
attributes: Array<String>, traits: Array<String>, collector: CollectActivity
attributes: Array<String>, traits: ArrayList<TraitObject>, collector: CollectActivity
) {

toolbar?.inflateMenu(R.menu.menu_fragment_summary)
Expand Down Expand Up @@ -119,7 +120,7 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {

val attributes = database.getAllObservationUnitAttributeNames(studyId.toInt())

val traits = database.visibleTrait
val traits = ArrayList(database.allTraitObjects.filter { it.visible })

loadData(collector, attributes, traits)

Expand All @@ -143,7 +144,7 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {
}

private fun loadData(
collector: CollectActivity, attributes: Array<String>, traits: Array<String>
collector: CollectActivity, attributes: Array<String>, traits: ArrayList<TraitObject>
) {

val filter = getPersistedFilter(collector)
Expand All @@ -160,7 +161,7 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {

try {

(attributes + traits).filter { if (filter == null) true else it in filter }
(attributes + traits.map { it.trait }).filter { if (filter == null) true else it in filter }
.forEach { key ->

val index = data.getColumnIndex(key)
Expand Down Expand Up @@ -199,7 +200,7 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {
SummaryAdapter.SummaryListModel(
key,
value ?: "",
key in traits
key in traits.map { it.trait }
)
)

Expand Down Expand Up @@ -233,14 +234,14 @@ class SummaryFragment : Fragment(), SummaryAdapter.SummaryController {
}

private fun showFilterDialog(
collector: CollectActivity, attributes: Array<String>, traits: Array<String>
collector: CollectActivity, attributes: Array<String>, traits: ArrayList<TraitObject>
) {

activity?.let { ctx ->

var filter: Set<String>? = getPersistedFilter(ctx)

val keys = attributes + traits
val keys = attributes + traits.map { it.trait }

//initialize which attributes are checked, if no filter is saved then check all
val checked = keys.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.fieldbook.tracker.objects.TraitObject
class TraitAdapter(private val sorter: TraitSorter):
ListAdapter<TraitObject, TraitAdapter.ViewHolder>(DiffCallback()) {

public var infoDialogShown: Boolean = false
var infoDialogShown: Boolean = false

interface TraitSorter {
fun onDrag(item: TraitAdapter.ViewHolder)
Expand Down Expand Up @@ -84,7 +84,7 @@ class TraitAdapter(private val sorter: TraitSorter):
}
}

public fun getTraitItem(position: Int): TraitObject {
fun getTraitItem(position: Int): TraitObject {
return currentList[position]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.exifinterface.media.ExifInterface;

import com.fieldbook.tracker.utilities.DocumentTreeUtil;
import com.fieldbook.tracker.utilities.FileUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

Expand Down Expand Up @@ -46,7 +47,9 @@ public FieldBookImage() {
}

public FieldBookImage(Context ctx, String filePath, String traitName, Bitmap missingPhoto) {
DocumentFile photosDir = DocumentTreeUtil.Companion.getFieldMediaDirectory(ctx, traitName);

String sanitizedTraitName = FileUtil.sanitizeFileName(traitName);
DocumentFile photosDir = DocumentTreeUtil.Companion.getFieldMediaDirectory(ctx, sanitizedTraitName);

this.missing = missingPhoto;
this.location = new GeoJSON();
Expand Down
Loading

0 comments on commit 49add97

Please sign in to comment.