From 2c225f68215f185c534a851a9e703367fb523769 Mon Sep 17 00:00:00 2001 From: lucasroyerdev Date: Mon, 19 Jan 2026 14:56:59 +0100 Subject: [PATCH] feat: remove previous back button and add a switching validate/back button for better UX --- app/build.gradle | 4 +- .../com/stock/pignon/CartActionHelper.java | 5 +- .../java/com/stock/pignon/CartViewHelper.java | 99 ++++++++----------- .../main/java/com/stock/pignon/Config.java | 4 +- .../java/com/stock/pignon/ControlServer.java | 8 +- .../java/com/stock/pignon/MainActivity.java | 33 +++++-- app/src/main/res/layout/activity_main.xml | 6 -- app/src/main/res/values/strings.xml | 1 + 8 files changed, 78 insertions(+), 82 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c6ecb0b..41e8be8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.stock.pignon" minSdkVersion 17 targetSdkVersion 36 - versionCode 4 - versionName "0.4.0" + versionCode 5 + versionName "0.5.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/stock/pignon/CartActionHelper.java b/app/src/main/java/com/stock/pignon/CartActionHelper.java index d7761d6..3a5a03c 100644 --- a/app/src/main/java/com/stock/pignon/CartActionHelper.java +++ b/app/src/main/java/com/stock/pignon/CartActionHelper.java @@ -3,7 +3,6 @@ package com.stock.pignon; import android.app.AlertDialog; import android.content.Context; -import android.content.Intent; import android.os.Handler; import android.os.Environment; import android.text.Html; @@ -111,8 +110,8 @@ public class CartActionHelper { */ private static void saveCartToExternalFile(List cartItems) { File dir = new File(Environment.getExternalStorageDirectory(), Config.EXTERNAL_DIR_NAME); - File stockFile = new File(dir, Config.OUPUT_JSON_NAME); - File csvFile = new File(dir, Config.OUPUT_CSV_NAME); + File stockFile = new File(dir, Config.OUTPUT_JSON_NAME); + File csvFile = new File(dir, Config.OUTPUT_CSV_NAME); String today = DateHelper.getTodayIso(); Gson gson = new Gson(); diff --git a/app/src/main/java/com/stock/pignon/CartViewHelper.java b/app/src/main/java/com/stock/pignon/CartViewHelper.java index 8799007..bf0d025 100644 --- a/app/src/main/java/com/stock/pignon/CartViewHelper.java +++ b/app/src/main/java/com/stock/pignon/CartViewHelper.java @@ -22,17 +22,23 @@ import java.util.List; public class CartViewHelper { /** - * Refreshes the entire cart side-panel or list - * Clears everything and rebuild on current CartManager data + * Short version for app resume : update without changing validate/back button state */ public static void updateCartView(LinearLayout cartList, Context context) { + updateCartView(cartList, context, false, false); + } + + /** + * Full version with validate/back button management + */ + public static void updateCartView(LinearLayout cartList, Context context, boolean updateButton, boolean isHome) { if (cartList == null) return; - // Clear and get current items + // Clean item lists cartList.removeAllViews(); List cartItems = CartManager.getItems(); - // If empty cart, show it to user + // Manage empty cart if (cartItems.isEmpty()) { TextView empty = new TextView(context); empty.setText(context.getString(R.string.cart_empty)); @@ -40,35 +46,24 @@ public class CartViewHelper { empty.setGravity(Gravity.CENTER); empty.setPadding(0, 50, 0, 0); cartList.addView(empty); - updateTotalDisplay(context, 0, 0); - return; + } else { + // Create list if not empty + int totalMin = 0; + int totalMax = 0; + for (CartItem item : cartItems) { + totalMin += item.getTotalMin(); + totalMax += item.getTotalMax(); + + // Create a line for each item + cartList.addView(createCartItemView(item, cartList, context)); + } + updateTotalDisplay(context, totalMin, totalMax); } - // Build list and calculate total at the same time - int totalMin = 0; - int totalMax = 0; - - for (CartItem item : cartItems) { - totalMin += item.getTotalMin(); - totalMax += item.getTotalMax(); - - // Create and add the row view - cartList.addView(createCartItemView(item, cartList, context)); - } - - // Display global price range at the end - updateTotalDisplay(context, totalMin, totalMax); - } - - /** - * Updates the global price range at the bottom of the screen - */ - private static void updateTotalDisplay(Context context, int min, int max) { - TextView totalView = ((Activity) context).findViewById(R.id.totalView); - if (totalView != null) { - // Using string resources - totalView.setText(context.getString(R.string.price_range, min, max, "€")); + // Update validate/back button + if (updateButton && context instanceof MainActivity) { + ((MainActivity) context).updateActionButton(isHome); } } @@ -81,7 +76,6 @@ public class CartViewHelper { row.setOrientation(LinearLayout.HORIZONTAL); row.setGravity(Gravity.CENTER_VERTICAL); - // Layout parameters LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); @@ -90,13 +84,13 @@ public class CartViewHelper { // Image ImageView image = new ImageView(context); - ImageLoader.loadImage(image, item.getImageFile(),200,200); + ImageLoader.loadImage(image, item.getImageFile(), 200, 200); LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(100, 100); imgParams.setMargins(0, 0, 20, 0); image.setLayoutParams(imgParams); row.addView(image); - // Sublayout for name + [quantity + price] + // Sublayout infos LinearLayout infoLayout = new LinearLayout(context); infoLayout.setOrientation(LinearLayout.VERTICAL); infoLayout.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); @@ -108,7 +102,7 @@ public class CartViewHelper { nameView.setTypeface(null, Typeface.BOLD); infoLayout.addView(nameView); - // Quantity and price range + // Quantity / price TextView detailsView = new TextView(context); String details = context.getString(R.string.cart_item, item.getQuantity(), item.getTotalMin(), item.getTotalMax()); detailsView.setText(details); @@ -124,42 +118,35 @@ public class CartViewHelper { } /** - * Creates the delete button - */ + * Creates the delete button for each item + */ private static Button createRemoveButton(CartItem item, LinearLayout cartList, Context context) { Button btn = new Button(context); btn.setText("✖"); - btn.setTextSize(18); btn.setTextColor(Color.WHITE); - btn.setGravity(Gravity.CENTER); - - // Conversion DP to PX for consistent size on all screens float scale = context.getResources().getDisplayMetrics().density; int sizePx = (int) (48 * scale + 0.5f); + btn.setLayoutParams(new LinearLayout.LayoutParams(sizePx, sizePx)); - LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(sizePx, sizePx); - btn.setLayoutParams(params); - - // Background: grey rounded rectangle (API 17 fallback) GradientDrawable shape = new GradientDrawable(); - shape.setColor(Color.parseColor("#E53935")); // Reddish to indicate delete + shape.setColor(Color.parseColor("#E53935")); shape.setCornerRadius(4 * scale); btn.setBackground(shape); btn.setOnClickListener(v -> { - // setting quantity to 0 removes the item - CartManager.addOrUpdateItem( - item.getName(), - item.getMinPrice(), - item.getMaxPrice(), - 0, // Setting quantity to 0 triggers removal - item.getImageFile() - ); - - // visual refresh of the cart list + CartManager.addOrUpdateItem(item.getName(), item.getMinPrice(), item.getMaxPrice(), 0, item.getImageFile()); updateCartView(cartList, context); }); - return btn; } + + /** + * Manage price range for whole cart + */ + private static void updateTotalDisplay(Context context, int min, int max) { + TextView totalView = ((Activity) context).findViewById(R.id.totalView); + if (totalView != null) { + totalView.setText(context.getString(R.string.price_range, min, max, "€")); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/stock/pignon/Config.java b/app/src/main/java/com/stock/pignon/Config.java index 919ed66..4dd6a3f 100644 --- a/app/src/main/java/com/stock/pignon/Config.java +++ b/app/src/main/java/com/stock/pignon/Config.java @@ -12,8 +12,8 @@ public class Config { public static final String INPUT_JSON_NAME = "pieces.json"; // Output json - public static final String OUPUT_JSON_NAME = "stock.json"; + public static final String OUTPUT_JSON_NAME = "stock.json"; // Output json - public static final String OUPUT_CSV_NAME = "stock.csv"; + public static final String OUTPUT_CSV_NAME = "stock.csv"; } \ No newline at end of file diff --git a/app/src/main/java/com/stock/pignon/ControlServer.java b/app/src/main/java/com/stock/pignon/ControlServer.java index 8644783..2338840 100644 --- a/app/src/main/java/com/stock/pignon/ControlServer.java +++ b/app/src/main/java/com/stock/pignon/ControlServer.java @@ -42,16 +42,16 @@ public class ControlServer extends NanoHTTPD { return downloadFile(Config.INPUT_JSON_NAME); case "/output_json": - return viewFile(Config.OUPUT_JSON_NAME); + return viewFile(Config.OUTPUT_JSON_NAME); case "/download_output_json": - return downloadFile(Config.OUPUT_JSON_NAME); + return downloadFile(Config.OUTPUT_JSON_NAME); case "/output_csv": - return viewFile(Config.OUPUT_CSV_NAME); + return viewFile(Config.OUTPUT_CSV_NAME); case "/download_output_csv": - return downloadFile(Config.OUPUT_CSV_NAME); + return downloadFile(Config.OUTPUT_CSV_NAME); case "/": case "/index.html": diff --git a/app/src/main/java/com/stock/pignon/MainActivity.java b/app/src/main/java/com/stock/pignon/MainActivity.java index 7cd01d1..72c7252 100644 --- a/app/src/main/java/com/stock/pignon/MainActivity.java +++ b/app/src/main/java/com/stock/pignon/MainActivity.java @@ -61,12 +61,6 @@ public class MainActivity extends AppCompatActivity { gridPieces = findViewById(R.id.gridPieces); mainImage = findViewById(R.id.mainImage); - // Setup Back Button - Button backBtn = findViewById(R.id.backToHomeBtn); - if (backBtn != null) { - backBtn.setOnClickListener(v -> showHome()); - } - // Copy assets to sd card if not founded copyAssetsIfEmpty(); // Get data from sd card @@ -86,7 +80,7 @@ public class MainActivity extends AppCompatActivity { } // Get app version - String versionName = ""; + String versionName; try { versionName = "App v" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName; } catch (Exception e) { @@ -200,6 +194,8 @@ public class MainActivity extends AppCompatActivity { homeLayout.setVisibility(View.GONE); categoryItemsLayout.setVisibility(View.VISIBLE); + updateActionButton(false); + gridPieces.removeAllViews(); // Calculate item width for a 4-column grid @@ -269,6 +265,8 @@ public class MainActivity extends AppCompatActivity { public void showHome() { categoryItemsLayout.setVisibility(View.GONE); homeLayout.setVisibility(View.VISIBLE); + + updateActionButton(true); } private void loadMainImage() { @@ -284,6 +282,23 @@ public class MainActivity extends AppCompatActivity { CartActionHelper.validateCart(cartList, this); } + public void updateActionButton(boolean isHome) { + Button btn = findViewById(R.id.validateCartBtn); + if (btn == null) return; + + if (isHome) { + // Home mode : validate button + btn.setText(getString(R.string.cart_validate_btn)); + btn.setBackgroundColor(Color.parseColor("#43A047")); // Vert + btn.setOnClickListener(v -> validateCart(v)); + } else { + // Category mode : back button + btn.setText(getString(R.string.cart_back_btn)); + btn.setBackgroundColor(Color.parseColor("#1E88E5")); // Bleu + btn.setOnClickListener(v -> showHome()); + } + } + private int dpToPx(int dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); } @@ -309,8 +324,8 @@ public class MainActivity extends AppCompatActivity { } // Check stock.json and stock.csv output files to avoid control server error - checkOrCreateEmptyFile(new File(rootDir, Config.OUPUT_JSON_NAME), "[]"); - checkOrCreateEmptyFile(new File(rootDir, Config.OUPUT_CSV_NAME), ""); + checkOrCreateEmptyFile(new File(rootDir, Config.OUTPUT_JSON_NAME), "[]"); + checkOrCreateEmptyFile(new File(rootDir, Config.OUTPUT_CSV_NAME), ""); // Check images folder File imgDir = new File(rootDir, Config.IMAGES_SUBDIR_NAME); diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ac8c80c..ab6fb4f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -42,12 +42,6 @@ android:visibility="gone" android:padding="10dp"> -