Skip to content

Commit

Permalink
Merge pull request #944 from PhenoApps/print_obs_fix
Browse files Browse the repository at this point in the history
Print observation fix
  • Loading branch information
trife authored Apr 3, 2024
2 parents fd96d5d + 8202f59 commit f7593dd
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,8 @@ public void onPause() {

preferences.edit().putInt(GeneralKeys.DATA_LOCK_STATE, dataLocked).apply();

traitLayouts.unregisterAllReceivers();

super.onPause();
}

Expand Down Expand Up @@ -1039,6 +1041,8 @@ public void onResume() {

dataLocked = preferences.getInt(GeneralKeys.DATA_LOCK_STATE, UNLOCKED);

traitLayouts.registerAllReceivers();

refreshLock();
}

Expand Down Expand Up @@ -2207,15 +2211,15 @@ public boolean isReturnFirstTrait() {
/**
* Inserts a user observation whenever a label is printed.
* See ResultReceiver onReceiveResult in LabelPrintLayout
* @param size: The size of the label. e.g "2 x 4 detailed"
* @param labelNumber: The number of labels printed.
*/
public void insertPrintObservation(String size) {
public void insertPrintObservation(String labelNumber) {

TraitObject trait = getCurrentTrait();

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

database.insertObservation(rangeBox.getPlotID(), trait.getId(), trait.getFormat(), size,
database.insertObservation(rangeBox.getPlotID(), trait.getId(), trait.getFormat(), labelNumber,
getPerson(),
getLocationByPreferences(), "", studyId, "",
null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public class LabelPrintTraitLayout extends BaseTraitLayout {

private Activity mActivity = null;

public void registerReceiver() {
Log.d(TAG, "Registering mPrinterMessageReceiver");
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mPrinterMessageReceiver,
new IntentFilter("printer_message"));
}

public void unregisterReceiver() {
Log.d(TAG, "Unregistering mPrinterMessageReceiver");
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mPrinterMessageReceiver);
}

public LabelPrintTraitLayout(Context context) { super(context); }

public LabelPrintTraitLayout(Context context, AttributeSet attrs) { super(context, attrs); }
Expand Down Expand Up @@ -141,24 +152,25 @@ private boolean checkPermissions(Activity act) {
@Override
public void onReceive(Context context, Intent intent) {

if (intent != null && intent.getExtras() != null) {
if (intent != null && intent.getExtras() != null) {

String message = intent.getExtras().getString("message");
String size = intent.getExtras().getString("size");
String message = intent.getExtras().getString("message");
int numLabels = intent.getExtras().getInt("numLabels", 0);

if (message != null) {

Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
if (message != null) {

}
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

if (size != null) {
}

// record each print event
((CollectActivity) getContext()).insertPrintObservation(size);
if (numLabels > 0) {
String labelNumber = String.valueOf(numLabels);
// Use the number of labels printed to record each print event
((CollectActivity) getContext()).insertPrintObservation(labelNumber);
}
}
}
}
};

@Override
Expand All @@ -168,9 +180,6 @@ public void init(Activity act) {

connectPrinter = act.findViewById(R.id.connectPrinterButton);
printLabel = act.findViewById(R.id.printLabelButton);
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mPrinterMessageReceiver);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mPrinterMessageReceiver,
new IntentFilter("printer_message"));

mBluetoothUtil = new BluetoothUtil();
Integer studyId = getPrefs().getInt(GeneralKeys.SELECTED_FIELD_ID, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ public void enableViews(Boolean toggle, ViewGroup layout) {
}
}

public void registerAllReceivers() {
for (BaseTraitLayout layout : this.traitLayouts) {
if (layout instanceof LabelPrintTraitLayout) {
((LabelPrintTraitLayout) layout).registerReceiver();
}
}
}

public void unregisterAllReceivers() {
for (BaseTraitLayout layout : this.traitLayouts) {
if (layout instanceof LabelPrintTraitLayout) {
((LabelPrintTraitLayout) layout).unregisterReceiver();
}
}
}

/**
* Triggers trait specific code for refreshing lock status.
* Some traits may need to refresh UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PrintThread(private val ctx: Context, private val btName: String) : Thread
}

intent.putExtra("message", success)
intent.putExtra("size", mSize)
intent.putExtra("numLabels", mLabelCommands.size)

} else if (printerStatus.isHeadOpen) {

Expand Down
99 changes: 61 additions & 38 deletions app/src/main/res/layout/trait_label_print.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,37 +50,45 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/labelsizetext"
android:id="@+id/labelcopiestext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_label_size_title" />
android:text="@string/trait_layout_print_label_copies_title" />

<Spinner
android:id="@+id/labelsize"
android:layout_width="wrap_content"
android:id="@+id/labelcopies"
android:layout_height="wrap_content"
android:minWidth="200dp" />
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />

</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/textfieldtitle"
android:id="@+id/labelsizetext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_text_field_title" />
android:text="@string/trait_layout_print_label_size_title" />

<Spinner
android:id="@+id/textfield"
android:id="@+id/labelsize"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
Expand All @@ -91,40 +99,44 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:visibility="gone">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/textfieldtitle2"
android:id="@+id/textfieldtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_text_field_title_2" />
android:text="@string/trait_layout_print_text_field_title" />

<Spinner
android:id="@+id/textfield2"
android:id="@+id/textfield"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout style="@style/TextViewStyle.Bold"
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:visibility="gone">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp">

<TextView
android:id="@+id/textfieldtitle3"
<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/barcodefieldtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_text_field_title_3" />
android:text="@string/trait_layout_print_barcode_field_title" />

<Spinner
android:id="@+id/textfield3"
android:id="@+id/barcodefield"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
Expand All @@ -135,39 +147,46 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:visibility="gone">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/textfieldtitle4"
android:id="@+id/textfieldtitle2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_text_field_title_4" />
android:text="@string/trait_layout_print_text_field_title_2" />

<Spinner
android:id="@+id/textfield4"
android:id="@+id/textfield2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

<LinearLayout
<LinearLayout style="@style/TextViewStyle.Bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:visibility="gone">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/barcodefieldtitle"
android:id="@+id/textfieldtitle3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_barcode_field_title" />
android:text="@string/trait_layout_print_text_field_title_3" />

<Spinner
android:id="@+id/barcodefield"
android:id="@+id/textfield3"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
Expand All @@ -178,21 +197,25 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="8dp">
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:visibility="gone">

<TextView style="@style/TextViewStyle.Bold"
android:id="@+id/labelcopiestext"
android:id="@+id/textfieldtitle4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/trait_layout_print_label_copies_title" />
android:text="@string/trait_layout_print_text_field_title_4" />

<Spinner
android:id="@+id/labelcopies"
android:layout_width="wrap_content"
android:id="@+id/textfield4"
android:layout_height="wrap_content"
android:minWidth="200dp" />

android:layout_width="0dp"
android:layout_weight="3"
android:maxLines="1" />
</LinearLayout>

</com.fieldbook.tracker.traits.LabelPrintTraitLayout>

0 comments on commit f7593dd

Please sign in to comment.