Skip to content

Commit

Permalink
Merge pull request #781 from PhenoApps/infobar_update
Browse files Browse the repository at this point in the history
Field name included in infobar attribute options
  • Loading branch information
trife authored Nov 1, 2023
2 parents 5d30333 + 892c86d commit 4600a9e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,11 @@ public String queryForLabelValue(

if (isAttribute) {

if (label.equals(context.getString(R.string.field_name_attribute))) {
String fieldName = ((CollectActivity) context).getPreferences().getString(GeneralKeys.FIELD_FILE, "");
return (fieldName == null || fieldName.isEmpty()) ? dataMissingString : fieldName;
}

String[] values = database.getDropDownRange(label, plotId);
if (values == null || values.length == 0) {
return dataMissingString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StudyDao {

//combine the case statements with commas
val selectStatement = if (select.isNotEmpty()) {
", " + select.joinToString(", ")
select.joinToString(", ") + ", "
} else ""

/**
Expand All @@ -73,7 +73,7 @@ class StudyDao {
*/
val query = """
CREATE TABLE IF NOT EXISTS $sObservationUnitPropertyViewName AS
SELECT units.${ObservationUnit.PK} AS id, units.`geo_coordinates` as "geo_coordinates" $selectStatement
SELECT $selectStatement units.${ObservationUnit.PK} AS id, units.`geo_coordinates` as "geo_coordinates"
FROM ${ObservationUnit.tableName} AS units
LEFT JOIN ${ObservationUnitValue.tableName} AS vals ON units.${ObservationUnit.PK} = vals.${ObservationUnit.FK}
LEFT JOIN ${ObservationUnitAttribute.tableName} AS attr on vals.${ObservationUnitAttribute.FK} = attr.${ObservationUnitAttribute.PK}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import java.util.ArrayList

/**
* A tab layout with tabs: attributes, traits, and other.
Expand Down Expand Up @@ -67,6 +68,9 @@ class CollectAttributeChooserDialog(private val activity: CollectActivity):
//query database for attributes/traits to use
try {
attributes = activity.getDatabase().getAllObservationUnitAttributeNames(activity.studyId.toInt())
val attributesList = attributes.toMutableList()
attributesList.add(0, context.getString(R.string.field_name_attribute))
attributes = attributesList.toTypedArray()
traits = activity.getDatabase().allTraitObjects.toTypedArray()
other = traits.filter { !it.visible }.toTypedArray()
traits = traits.filter { it.visible }.toTypedArray()
Expand Down Expand Up @@ -115,7 +119,7 @@ class CollectAttributeChooserDialog(private val activity: CollectActivity):

//manually select the first tab based on preferences
val tabIndex = activity.getPreferences()
.getInt(GeneralKeys.ATTR_CHOOSER_DIALOG_TAB, 1)
.getInt(GeneralKeys.ATTR_CHOOSER_DIALOG_TAB, 0)

tabLayout.selectTab(tabLayout.getTabAt(tabIndex))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public class LabelPrintTraitLayout extends BaseTraitLayout {
private String[] options;
private String[] labelCopiesArray;
private String[] labelSizeArray;
private String simpleSmall;
private String detailedSmall;
private String simpleLarge;
private String detailedLarge;
private ArrayList<String> optionsList;
private ArrayAdapter<String> sizeArrayAdapter;
private ArrayAdapter<String> fieldArrayAdapter;
Expand Down Expand Up @@ -169,19 +173,24 @@ public void init(Activity act) {
new IntentFilter("printer_message"));

mBluetoothUtil = new BluetoothUtil();

String[] prefixTraits = getDatabase().getRangeColumnNames();
Integer studyId = getPrefs().getInt(GeneralKeys.SELECTED_FIELD_ID, 0);
String[] prefixTraits = getDatabase().getAllObservationUnitAttributeNames(studyId);
optionsList = new ArrayList<>(Arrays.asList(prefixTraits));
optionsList.add("date");
optionsList.add("trial_name");
optionsList.add("blank");
optionsList.add(0, getContext().getString(R.string.field_name_attribute));
optionsList.add(getContext().getString(R.string.trait_layout_print_label_date_option));
optionsList.add(getContext().getString(R.string.trait_layout_print_label_blank_option));
options = new String[optionsList.size()];
optionsList.toArray(options);

fieldArrayAdapter = new ArrayAdapter<>(
getContext(), R.layout.custom_spinner_layout, options);

labelSizeArray = new String[]{"3\" x 2\" simple", "3\" x 2\" detailed", "2\" x 1\" simple", "2\" x 1\" detailed"};
simpleSmall = "2\" x 1\" " + getContext().getString(R.string.trait_layout_print_label_simple_text);
detailedSmall = "2\" x 1\" " + getContext().getString(R.string.trait_layout_print_label_detailed_text);
simpleLarge = "3\" x 2\" " + getContext().getString(R.string.trait_layout_print_label_simple_text);
detailedLarge = "3\" x 2\" " + getContext().getString(R.string.trait_layout_print_label_detailed_text);

labelSizeArray = new String[]{simpleLarge, detailedLarge, simpleSmall, detailedSmall};
sizeArrayAdapter = new ArrayAdapter<>(
getContext(), R.layout.custom_spinner_layout, labelSizeArray);

Expand Down Expand Up @@ -223,7 +232,7 @@ public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
Log.d(TAG, labelsize.getSelectedItem().toString());

if (labelsize.getSelectedItem().toString().equals("3\" x 2\" detailed") || labelsize.getSelectedItem().toString().equals("2\" x 1\" detailed")) {
if (labelsize.getSelectedItem().toString().equals(detailedLarge) || labelsize.getSelectedItem().toString().equals(detailedSmall)) {
((View) textfield2.getParent()).setVisibility(View.VISIBLE);
((View) textfield3.getParent()).setVisibility(View.VISIBLE);
((View) textfield4.getParent()).setVisibility(View.VISIBLE);
Expand All @@ -247,19 +256,19 @@ public void onNothingSelected(AdapterView<?> arg0) {
labelsize.setSelection(sizeArrayAdapter.getPosition(getPrefs().getString("SIZE", labelSizeArray[0])));
labelsize.setEnabled(true);

textfield1.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT", options[0])));
textfield1.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT", options[1])));
textfield1.setEnabled(true);

textfield2.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT2", options[0])));
textfield2.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT2", options[2])));
textfield2.setEnabled(true);

textfield3.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT3", options[0])));
textfield3.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT3", getContext().getString(R.string.trait_layout_print_label_date_option))));
textfield3.setEnabled(true);

textfield4.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT4", options[0])));
textfield4.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("TEXT4", getContext().getString(R.string.field_name_attribute))));
textfield4.setEnabled(true);

barcodefield.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("BARCODE", options[0])));
barcodefield.setSelection(fieldArrayAdapter.getPosition(getPrefs().getString("BARCODE", getPrefs().getString(GeneralKeys.UNIQUE_NAME, ""))));
barcodefield.setEnabled(true);

labelcopies.setSelection(copiesArrayAdapter.getPosition(getPrefs().getString("COPIES", labelCopiesArray[0])));
Expand Down Expand Up @@ -354,30 +363,22 @@ public void onDeviceChosen(String newDeviceName) {
ed.apply();

int length = barcode.length();
int barcode_size = 6;

// Scale barcode based on label size and variable field length
switch (size) {
case "3\" x 2\" simple":
barcode_size = 10 - (length / 15);
break;
case "3\" x 2\" detailed":
barcode_size = 9 - (length / 15);
break;
case "2\" x 1\" simple":
case "2\" x 1\" detailed":
barcode_size = 5 - (length / 15);
break;
default:
//Log.d(((MainActivity) getContext()).TAG, "Matched no sizes");
break;
int barcode_size = 6; // size for simpleSmall labels

// Scale barcode size by label size and variable field length
if (size.equals(detailedSmall)) {
barcode_size = 5 - (length / 15);
} else if (size.equals(simpleLarge)) {
barcode_size = 10 - (length / 15);
} else if (size.equals(detailedLarge)) {
barcode_size = 9 - (length / 15);
}

int dotsAvailable1;
int dotsAvailable2;

// Scale text based on label size and variable field length
if (size.equals("2\" x 1\" simple") || size.equals("2\" x 1\" detailed")) {
if (size.equals(simpleSmall) || size.equals(detailedSmall)) {
dotsAvailable1 = 399;
dotsAvailable2 = 250;

Expand Down Expand Up @@ -482,11 +483,11 @@ public String getValueFromSpinner(Spinner spinner, String[] options) {
String item = (String) spinner.getSelectedItem();

if (item != null) {
if (item.equals("date")) {
if (item.equals(getContext().getString(R.string.trait_layout_print_label_date_option))) {
value = dateFormat.format(calendar.getTime());
} else if (item.equals("trial_name")) {
} else if (item.equals(getContext().getString(R.string.field_name_attribute))) {
value = getPrefs().getString(GeneralKeys.FIELD_FILE, "");
} else if (item.equals("blank")) {
} else if (item.equals(getContext().getString(R.string.trait_layout_print_label_blank_option))) {
value = "";
} else {
int pos = spinner.getSelectedItemPosition();
Expand Down
66 changes: 30 additions & 36 deletions app/src/main/java/com/fieldbook/tracker/utilities/InfoBarHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package com.fieldbook.tracker.utilities

import android.content.Context
import android.content.SharedPreferences
import android.provider.ContactsContract.Data
import android.util.Log
import com.fieldbook.tracker.R
import com.fieldbook.tracker.activities.CollectActivity
import com.fieldbook.tracker.database.DataHelper
import com.fieldbook.tracker.dialogs.CollectAttributeChooserDialog
import com.fieldbook.tracker.objects.InfoBarModel
import com.fieldbook.tracker.preferences.GeneralKeys
import dagger.hilt.android.qualifiers.ActivityContext
import java.util.Arrays
import java.util.StringJoiner
import java.util.*
import javax.inject.Inject

/**
Expand All @@ -38,61 +37,56 @@ class InfoBarHelper @Inject constructor(@ActivityContext private val context: Co
*/
fun getInfoBarData(): ArrayList<InfoBarModel> {

val database : DataHelper = (context as CollectActivity).getDatabase()

//get the preference number of infobars to load
val numInfoBars: Int = ep.getInt(GeneralKeys.INFOBAR_NUMBER, 2)

//get all plot attribute names for the study
val attributes: MutableList<String> = ArrayList(database.rangeColumnNames.toList())
attributes.add(0, context.getString(R.string.field_name_attribute))

//get all traits for this study
val traits = database.allTraitObjects

//create a new array with just trait names
val traitNames = ArrayList<String>()
if (traits != null) {
for (t in traits) {
traitNames.add(t.trait)
}
}

//initialize a list of infobar models that will be served to the adapter
val infoBarModels = ArrayList<InfoBarModel>()

//iterate and build the arraylist
for (i in 0 until numInfoBars) {

var database : DataHelper = (context as CollectActivity).getDatabase();

//ensure that the initialLabel is actually a plot attribute

//get all plot attribute names for the study
val attributes: List<String> = ArrayList(Arrays.asList(*database.rangeColumnNames))

//get all traits for this study
val traits = database.allTraitObjects

//create a new array with just trait names
val traitNames = ArrayList<String>()
if (traits != null) {
for (t in traits) {
traitNames.add(t.trait)
}
}

//get the default label for the infobar using 'Select' (used in original adapter code)
// or the first item in the attributes list
var defaultLabel = "Select"
if (attributes.size > 0) {
defaultLabel = attributes[0]
}

//get the preferred infobar label, default to above if it doesn't exist for this position
//adapter preferred values are saved as DROP1, DROP2, DROP3, DROP4, DROP5 in preferences, intiailize the label with it
var initialLabel: String = ep.getString("DROP$i", defaultLabel) ?: defaultLabel
//get the preferred infobar label, default to "Select" if it doesn't exist for this position
//adapter preferred values are saved as DROP1, DROP2, DROP3, DROP4, DROP5 in preferences, initialize the label with it
var initialLabel: String = ep.getString("DROP$i", context.getString(R.string.infobars_attribute_placeholder)) ?: context.getString(R.string.infobars_attribute_placeholder)

//check if the label is an attribute or a trait, this will decide how to query the database for the value
val isAttribute = attributes.contains(initialLabel)
var isAttribute = attributes.contains(initialLabel)

//check if the label actually exists in the attributes/traits (this will reset on field switch)
//if it doesn't exist, default to the first attribute in the list
//if it doesn't exist, default to the next attribute in the list
if (!isAttribute) {
if (!traitNames.contains(initialLabel)) {
if (attributes.isNotEmpty()) {
initialLabel = attributes[0]
if (i in attributes.indices) {
initialLabel = attributes[i]
isAttribute = true
} else {
initialLabel = context.getString(R.string.infobars_attribute_placeholder)
}
}
}

//query the database for the label's value
(context as? CollectActivity)?.getRangeBox()?.getPlotID()?.let { plot ->

val value = (context as CollectActivity).queryForLabelValue(plot, initialLabel, isAttribute)
val value = (context).queryForLabelValue(plot, initialLabel, isAttribute)

infoBarModels.add(InfoBarModel(initialLabel, value))
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@
<string name="dialog_att_chooser_traits">Traits</string>
<string name="dialog_att_chooser_other">Other</string>
<string name="dialog_collect_att_chooser_title">Choose an attribute</string>
<string name="infobars_attribute_placeholder">Select</string>
<string name="field_name_attribute">field_name</string>

<!-- ID/Barcode Search -->
<string name="act_collect_barcode_search_exists_in_other_field">This ID is in %s</string>
Expand Down Expand Up @@ -351,6 +353,10 @@
<string name="trait_layout_print_text_field_title_4">Text4:</string>
<string name="trait_layout_print_barcode_field_title">Barcode:</string>
<string name="trait_layout_print_label_copies_title">Copies:</string>
<string name="trait_layout_print_label_simple_text">simple</string>
<string name="trait_layout_print_label_detailed_text">detailed</string>
<string name="trait_layout_print_label_date_option">date</string>
<string name="trait_layout_print_label_blank_option">blank</string>

<string name="printer_open">Printer is open</string>
<string name="printer_paused">Printer is paused</string>
Expand Down

0 comments on commit 4600a9e

Please sign in to comment.