From bede6bf0c4b12084b256883761e426b4d2f064ad Mon Sep 17 00:00:00 2001 From: "sentry-autofix[bot]" <157164994+sentry-autofix[bot]@users.noreply.github.com> Date: Tue, 12 Nov 2024 09:47:20 +0000 Subject: [PATCH] File change --- .../vu/android/empowerplant/MainFragment.java | 66 +++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/example/vu/android/empowerplant/MainFragment.java b/app/src/main/java/com/example/vu/android/empowerplant/MainFragment.java index 24b7ee8..982279d 100644 --- a/app/src/main/java/com/example/vu/android/empowerplant/MainFragment.java +++ b/app/src/main/java/com/example/vu/android/empowerplant/MainFragment.java @@ -346,14 +346,32 @@ public void onResponse(@NotNull Call call, @NotNull Response response) throws IO boolean success = response.isSuccessful(); response.close(); if (!success) { - Log.d("checkout", "response failed"); + Log.d("checkout", "Checkout request failed - retrying..."); getActivity().runOnUiThread(new Runnable() { @Override public void run() { progressDialog.dismiss(); - - processDeliveryItem(checkoutTransaction); - + // Show retry dialog + new AlertDialog.Builder(getActivity()) + .setTitle("Checkout Failed") + .setMessage("Would you like to retry?") + .setPositiveButton("Retry", (dialog, which) -> { + // Retry checkout + progressDialog.show(); + client.newCall(request).enqueue(this); + }) + .setNegativeButton("Cancel", (dialog, which) -> { + processDeliveryItem(checkoutTransaction); + dialog.dismiss(); + }) + .create() + .show(); + + // Track failed attempt + Sentry.addBreadcrumb( + new Breadcrumb("Checkout attempt failed") + ); + checkoutTransaction.finish(SpanStatus.INTERNAL_ERROR); } }); @@ -431,27 +449,51 @@ private void processDeliveryItem(ITransaction checkoutTransaction) { } class BackendAPIException extends RuntimeException { - - public BackendAPIException(String message) { - super(message); + + try { + // Process delivery workflow + if (selectedStoreItems != null && !selectedStoreItems.isEmpty()) { + for (StoreItem item : selectedStoreItems.values()) { + updateDeliveryStatus(item); + } + processDeliverySpan.setStatus(SpanStatus.OK); + } else { + processDeliverySpan.setStatus(SpanStatus.INVALID_ARGUMENT); + Log.w("processDeliveryItem", "No items to process for delivery"); + } } } protected Boolean addAttachment() { File f = null; - try { - Context c = getActivity().getApplicationContext(); - File cacheDirectory = c.getCacheDir(); - f = File.createTempFile("tmp", ".txt", cacheDirectory); + + // Show error to user + getActivity().runOnUiThread(() -> { + Toast.makeText(getContext(), + "Failed to process delivery. Please try again later.", + Toast.LENGTH_LONG).show(); + }); System.out.println("File path: " + f.getAbsolutePath()); f.deleteOnExit(); try (FileOutputStream fos = new FileOutputStream(f)) { fos.write("test".getBytes(UTF_8)); } String dateStr = new SimpleDateFormat("yyyyMMddHHmm").format(new Date()); - Attachment attachment1 = new Attachment(f.getAbsolutePath(), "tmp_" + dateStr + ".txt", "text/plain"); + } + } + + private void updateDeliveryStatus(StoreItem item) { + // Update delivery status in local DB + try { + AppDatabase.getInstance(getActivity().getApplicationContext()) + .StoreItemDAO() + .updateDeliveryStatus(item.getItemId(), "PENDING"); + } catch (Exception e) { + Log.e("updateDeliveryStatus", "Failed to update status for item " + item.getItemId()); + Sentry.captureException(e); + throw e; Sentry.configureScope( scope -> { String json = "{ \"number\": 10 }";