");
@@ -147,19 +151,28 @@ public class ControlServer extends NanoHTTPD {
if ("global".equals(displayName)) {
sb.append("Articles globaux");
- // Hidden input to mark cat for server
- sb.append("
");
+ sb.append("
");
techId = "global";
} else {
- // Copy H2 theme
- sb.append("
");
+ // Nom
+ sb.append("
");
- // Delete category button
+ // Couleur de Fond (on utilise cat.getBgColor())
+ sb.append("
");
+ sb.append("
Fond
");
+ sb.append("
");
+ sb.append("
");
+
+ // Couleur de Texte (on utilise cat.getTextColor())
+ sb.append("
");
+ sb.append("
Texte
");
+ sb.append("
");
+ sb.append("
");
+
+ // Bouton supprimer
sb.append("
");
+ .append("onclick=\"if(confirm('Supprimer cette catégorie?')) this.closest('.card').remove()\">×");
}
-
sb.append("");
// Build table
@@ -236,18 +249,15 @@ public class ControlServer extends NanoHTTPD {
// Get categories order
String[] orderedIds = fullJson.getString("cat_order_list").split(",");
- // LinkedHashMap to keep order
- Map
> finalData = new LinkedHashMap<>();
+ List finalData = new ArrayList<>();
// For each category
for (String techId : orderedIds) {
- // Create awaited label
String catNameKey = "cat|" + techId + "|name";
// Does it exist
if (!allFields.has(catNameKey)) continue;
-
- // Get data
- String realCatName = allFields.getString(catNameKey);
+
+ String name = allFields.getString(catNameKey);
List- itemsInCategory = new ArrayList<>();
// Browse and create items
@@ -257,17 +267,38 @@ public class ControlServer extends NanoHTTPD {
// On vérifie si l'item suivant existe (via son champ name)
if (!allFields.has(itemBase + "name")) break;
+ // Manage image
+ String imgVal = allFields.optString(itemBase + "img", "");
+ if (imgVal.startsWith("data:image")) {
+ try {
+ String newImgName = "img_" + System.currentTimeMillis() + "_" + i;
+ String base64Data = imgVal.split(",")[1];
+ byte[] decoded = android.util.Base64.decode(base64Data, android.util.Base64.DEFAULT);
+
+ File imageFile = new File(imagesDir, newImgName + ".jpg");
+ try (FileOutputStream fos = new FileOutputStream(imageFile)) {
+ fos.write(decoded);
+ }
+ imgVal = newImgName; // Replace base64 with name of created jpg file
+ } catch (Exception e) { Log.e("ControlServer", "Img Error", e); }
+ }
+
itemsInCategory.add(new Item(
// Mandatory
allFields.getString(itemBase + "name"),
// Not mandatory
- allFields.optString(itemBase + "img", ""),
+ imgVal,
parseSafely(allFields.optString(itemBase + "min", "0")),
parseSafely(allFields.optString(itemBase + "max", "0"))
));
i++;
}
- finalData.put(realCatName, itemsInCategory);
+
+ // Create category with items and colors
+ Category cat = new Category(name, itemsInCategory);
+ cat.setBgColor(allFields.optString("cat|" + techId + "|bgColor", "#0049AF"));
+ cat.setTextColor(allFields.optString("cat|" + techId + "|textColor", "#FFFFFF"));
+ finalData.add(cat);
}
DataLoader.saveData(finalData);
diff --git a/app/src/main/java/com/stock/pignon/DataLoader.java b/app/src/main/java/com/stock/pignon/DataLoader.java
index 717f796..73bd6c5 100644
--- a/app/src/main/java/com/stock/pignon/DataLoader.java
+++ b/app/src/main/java/com/stock/pignon/DataLoader.java
@@ -102,6 +102,9 @@ public class DataLoader {
public static List getCategories() {
return cachedCategories;
}
+ public static List
- getGlobalItems() {
+ return cachedGlobals;
+ }
/**
* Internal class for GSON
@@ -114,36 +117,30 @@ public class DataLoader {
/**
* Write JSON from online editor data
*/
- public static void saveData(Map> sections) throws Exception {
- File dir = new File(Environment.getExternalStorageDirectory(), EXTERNAL_DIR);
- File jsonFile = new File(dir, PIECES_FILE);
-
- // To respect original format, we use the same format as GSON
- List
- globalList = sections.get("global");
- if (globalList == null) {
- globalList = new ArrayList<>();
- }
-
+ public static void saveData(List categoriesList) throws Exception {
CategoriesWrapper wrapper = new CategoriesWrapper();
- wrapper.globalItems = globalList;
wrapper.categories = new ArrayList<>();
+ wrapper.globalItems = new ArrayList<>();
- // Fill each category
- for (Map.Entry> entry : sections.entrySet()) {
- if (!"global".equals(entry.getKey())) {
- wrapper.categories.add(new Category(entry.getKey(), entry.getValue()));
+ // Browse category
+ for (Category cat : categoriesList) {
+ if ("global".equals(cat.getName())) {
+ wrapper.globalItems = cat.getItems();
+ } else {
+ wrapper.categories.add(cat);
}
}
- // Convert to pretty JSON, human readable
+ // Create JSON
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String jsonString = gson.toJson(wrapper);
- // Write to disk
- try (FileOutputStream fos = new FileOutputStream(jsonFile);
- OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8")) {
+ File dir = new File(Environment.getExternalStorageDirectory(), Config.EXTERNAL_DIR_NAME);
+ File jsonFile = new File(dir, Config.INPUT_JSON_NAME);
+
+ // Write JSON
+ try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(jsonFile), "UTF-8")) {
writer.write(jsonString);
- writer.flush();
}
// Update app cache