Skip to content

Commit

Permalink
generalize choose function, handle printer-specific logic in LabelPri…
Browse files Browse the repository at this point in the history
…ntTraitLayout
  • Loading branch information
bellerbrock committed Aug 25, 2023
1 parent ad103fe commit 4432038
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fieldbook.tracker.R;
import com.fieldbook.tracker.activities.CollectActivity;
import com.fieldbook.tracker.preferences.GeneralKeys;
import com.fieldbook.tracker.utilities.BluetoothChooseCallback;
import com.fieldbook.tracker.utilities.BluetoothUtil;
import com.fieldbook.tracker.utilities.Constants;

Expand Down Expand Up @@ -414,8 +415,19 @@ public void onNothingSelected(AdapterView<?> arg0) {
* A local broadcast receiver is used to communicate with the print thread within this utility class.
*/
String printerName = getPrefs().getString(GeneralKeys.LABEL_PRINT_DEVICE_NAME, null);
Log.d("LabelPrintTraitLayout", "printerName is $printerName");
mBluetoothUtil.print(getContext(), printerName, size, labels);
Log.d("LabelPrintTraitLayout", "retrieved printerName is " + printerName);
if (printerName == null) {
mBluetoothUtil.choose(getContext(), new BluetoothChooseCallback() {
@Override
public void onDeviceChosen(String newDeviceName) {
Log.d("LabelPrintTraitLayout", "Chosen printerName is " + newDeviceName);
saveDeviceNamePreference(newDeviceName);
mBluetoothUtil.print(getContext(), newDeviceName, size, labels);
}
});
} else {
mBluetoothUtil.print(getContext(), printerName, size, labels);
}
}
}
} else {
Expand All @@ -426,6 +438,12 @@ public void onNothingSelected(AdapterView<?> arg0) {
});
}

private void saveDeviceNamePreference(String newDeviceName) {
SharedPreferences.Editor editor = getPrefs().edit();
editor.putString(GeneralKeys.LABEL_PRINT_DEVICE_NAME, newDeviceName);
editor.apply();
}

@Override
public void deleteTraitListener() {

Expand Down
34 changes: 12 additions & 22 deletions app/src/main/java/com/fieldbook/tracker/utilities/BluetoothUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ import androidx.appcompat.app.AlertDialog
import com.fieldbook.tracker.R
import com.fieldbook.tracker.preferences.GeneralKeys


interface BluetoothChooseCallback {
fun onDeviceChosen(deviceName: String)
}
//Bluetooth Utility class for printing ZPL code and choosing bluetooth devices to print from.
class BluetoothUtil {

private var ep: SharedPreferences? = null

private var mBtName: String = String()

private val mBluetoothAdapter: BluetoothAdapter? by lazy {
BluetoothAdapter.getDefaultAdapter()
}

//operation that uses the provided context to prompt the user for a paired bluetooth device
private fun choose(ctx: Context) {
fun choose(ctx: Context, callback: BluetoothChooseCallback) {

ep = ctx.getSharedPreferences(GeneralKeys.SHARED_PREF_FILE_NAME, 0)
var deviceName: String = ""

mBluetoothAdapter?.let {

Expand Down Expand Up @@ -58,8 +56,9 @@ class BluetoothUtil {
if (input.checkedRadioButtonId == -1) return@setPositiveButton
else {
Log.d("BluetoothUtil", "Setting mBtName")
mBtName = map[input.checkedRadioButtonId]?.name ?: ""
ep.edit().putString(GeneralKeys.LABEL_PRINT_DEVICE_NAME, mBtName)
deviceName = map[input.checkedRadioButtonId]?.name ?: ""
Log.d("BluetoothUtil", "Selected Bluetooth Device: $deviceName")
callback.onDeviceChosen(deviceName)
}
}
}
Expand All @@ -69,23 +68,14 @@ class BluetoothUtil {
}

/**
* This function will first ask the user to select a printer.
* As long as the same object is used the user only needs to ask once.
* This sends a list of label commands, and only updates the printer message when the
* This function will send a list of label commands to the printer, and only updates the printer message when the
* button is pressed.
*/
fun print(ctx: Context, printerName: String? = null, size: String, labelCommand: List<String>) {
Log.d("BluetoothUtil", "Selected Bluetooth Device: $mBtName")
if (printerName == null) {
Log.d("BluetoothUtil", "Printer name is null, asking user to choose.")
choose(ctx)
} else {
mBtName = printerName
}
fun print(ctx: Context, printerName: String, size: String, labelCommand: List<String>) {
Log.d("BluetoothUtil", "Label Command is: $labelCommand")
if (labelCommand.isNotEmpty()) {
Log.d("BluetoothUtil", "printing")
PrintThread(ctx, mBtName).print(size, labelCommand)
Log.d("BluetoothUtil", "printing to $printerName")
PrintThread(ctx, printerName).print(size, labelCommand)
}
}
}

0 comments on commit 4432038

Please sign in to comment.