mirror of
https://github.com/lucasroyerdev/stock-pignon.git
synced 2026-05-10 11:02:26 +00:00
feat: add online editor, work in progress
This commit is contained in:
@@ -20,8 +20,10 @@ public class CartItem {
|
||||
public int getMinPrice() { return minPrice; }
|
||||
public int getMaxPrice() { return maxPrice; }
|
||||
public int getQuantity() { return quantity; }
|
||||
public void setQuantity(int quantity) { this.quantity = quantity; }
|
||||
public int getTotalMin() { return minPrice * quantity; }
|
||||
public int getTotalMax() { return maxPrice * quantity; }
|
||||
public String getImageFile() { return imageFile; }
|
||||
|
||||
// Setters
|
||||
public void setQuantity(int quantity) { this.quantity = quantity; }
|
||||
}
|
||||
|
||||
@@ -8,10 +8,14 @@ public class CartManager {
|
||||
// Unified and global list for whole application
|
||||
private static final List<CartItem> items = new ArrayList<>();
|
||||
|
||||
// Private constructor: utility class, it should not be instantiated
|
||||
/**
|
||||
* Private constructor: utility class, it should not be instantiated
|
||||
*/
|
||||
private CartManager() {}
|
||||
|
||||
// Returns the direct reference to the list to save memory on older devices
|
||||
/**
|
||||
* Returns the direct reference to the list to save memory on older devices
|
||||
*/
|
||||
public static List<CartItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
@@ -25,7 +29,9 @@ public class CartManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Logic for adding, updating or removing items based on quantity, prevents duplicate entries
|
||||
/**
|
||||
* Logic for adding, updating or removing items based on quantity, prevents duplicate entries
|
||||
*/
|
||||
public static void addOrUpdateItem(String name, int minPrice, int maxPrice, int quantity, String imageFile) {
|
||||
CartItem current = getItemByName(name);
|
||||
|
||||
@@ -40,20 +46,27 @@ public class CartManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Range-based pricing
|
||||
|
||||
/**
|
||||
* Range-based pricing
|
||||
*/
|
||||
public static int getGlobalTotalMin() {
|
||||
int total = 0;
|
||||
for (CartItem item : items) total += item.getTotalMin();
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Range-based pricing
|
||||
*/
|
||||
public static int getGlobalTotalMax() {
|
||||
int total = 0;
|
||||
for (CartItem item : items) total += item.getTotalMax();
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove item from cart
|
||||
*/
|
||||
public static void clear() {
|
||||
items.clear();
|
||||
}
|
||||
|
||||
@@ -14,12 +14,21 @@ public class Category {
|
||||
@SuppressWarnings("unused")
|
||||
private List<Item> items;
|
||||
|
||||
// Empty constructor for GSON
|
||||
public Category() {}
|
||||
|
||||
// Full constructor for online editor
|
||||
public Category(String name, List<Item> items) {
|
||||
this.name = name;
|
||||
this.items = items;
|
||||
// Default colors
|
||||
this.bgColor = "#0049AF";
|
||||
this.textColor = "#FFFFFF";
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String getName() { return name; }
|
||||
public String getBgColor() { return bgColor; }
|
||||
public String getTextColor() { return textColor; }
|
||||
|
||||
// Avoid crash if json isn't readable
|
||||
public List<Item> getItems() {
|
||||
return items != null ? items : new ArrayList<>();
|
||||
}
|
||||
public List<Item> getItems() { return items != null ? items : new ArrayList<>(); }
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.stock.pignon;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import java.io.File;
|
||||
@@ -11,15 +12,17 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* Create a web server to remote management of app assets.
|
||||
* Inherit what is needed to build a web server from NanoHTTPD
|
||||
*/
|
||||
// Inherit what is needed to build a web server
|
||||
public class ControlServer extends NanoHTTPD {
|
||||
|
||||
private final Context context;
|
||||
@@ -35,17 +38,23 @@ public class ControlServer extends NanoHTTPD {
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method which receive remote request
|
||||
* Main method which manage requests
|
||||
*/
|
||||
@Override
|
||||
public Response serve(IHTTPSession session) {
|
||||
// Get requested address
|
||||
String uri = session.getUri();
|
||||
|
||||
if (uri.startsWith("/img_view/")) {
|
||||
return serveImage(uri.replace("/img_view/", ""));
|
||||
}
|
||||
|
||||
if (session.getMethod() == Method.GET) {
|
||||
switch (uri) {
|
||||
// Soon deprecated with online editor
|
||||
case "/download_input":
|
||||
return downloadFile(Config.INPUT_JSON_NAME);
|
||||
// Soon deprecated with online editor
|
||||
case "/output_json":
|
||||
return viewFile(Config.OUTPUT_JSON_NAME);
|
||||
case "/download_output_json":
|
||||
@@ -54,17 +63,23 @@ public class ControlServer extends NanoHTTPD {
|
||||
return viewFile(Config.OUTPUT_CSV_NAME);
|
||||
case "/download_output_csv":
|
||||
return downloadFile(Config.OUTPUT_CSV_NAME);
|
||||
case "/edit":
|
||||
return newFixedLengthResponse(fillEditor());
|
||||
case "/":
|
||||
return newFixedLengthResponse(getHome());
|
||||
return newFixedLengthResponse(getHtml("index.html"));
|
||||
}
|
||||
}
|
||||
|
||||
if (session.getMethod() == Method.POST) {
|
||||
switch (uri) {
|
||||
// Soon deprecated with online editor
|
||||
case "/upload_json":
|
||||
return handleJsonUpload(session);
|
||||
// Soon deprecated with online editor
|
||||
case "/upload_images":
|
||||
return handleImagesUpload(session);
|
||||
case "/save_editor":
|
||||
return handleSaveEditor(session);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,28 +87,233 @@ public class ControlServer extends NanoHTTPD {
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML UI for users
|
||||
* Read from asset and return html file
|
||||
*/
|
||||
private String getHome() {
|
||||
private String getHtml(String file) {
|
||||
try {
|
||||
InputStream is = context.getAssets().open("index.html");
|
||||
InputStream is = context.getAssets().open(file);
|
||||
// Scanner html file from the beginning with "\\A"
|
||||
Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");
|
||||
String html = s.hasNext() ? s.next() : "";
|
||||
is.close();
|
||||
return html;
|
||||
} catch (IOException e) {
|
||||
return "<html><body>❌ Erreur de chargement du template</body></html>";
|
||||
return "<html><body>❌ Erreur de chargement de la page</body></html>";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from asset and return html file
|
||||
*/
|
||||
private String fillEditor() {
|
||||
// Get data for assets
|
||||
DataLoader.loadData();
|
||||
// Create an html string to fill the template
|
||||
StringBuilder html = new StringBuilder();
|
||||
|
||||
// Browse map : 'id' for categories name, 'items' for items list
|
||||
for (Map.Entry<String, List<Item>> section : DataLoader.getAllSections().entrySet()) {
|
||||
String id = section.getKey();
|
||||
List<Item> items = section.getValue();
|
||||
|
||||
// For each category, render a HTML card
|
||||
html.append(renderCategorySection(id, items));
|
||||
}
|
||||
|
||||
return getHtml("editor.html").replace("{{GENERATED_CONTENT}}", html.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate HTML section for each category
|
||||
*/
|
||||
private String renderCategorySection(String id, List<Item> items) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.append("<div class='card'>");
|
||||
sb.append("<h2>");
|
||||
|
||||
if ("global".equals(id)) {
|
||||
sb.append("Articles globaux");
|
||||
// Hidden input to mark cat for server
|
||||
sb.append("<input type='hidden' name='cat|").append(id).append("|name' value='global'>");
|
||||
} else {
|
||||
// Copy H2 theme
|
||||
sb.append("<input type='text' name='cat|").append(id).append("|name' ")
|
||||
.append("value=\"").append(id).append("\" ")
|
||||
.append("style='font-size: 1.5rem; font-weight: bold; border: none; background: transparent; color: #555; width: auto; max-width: 70%; border-bottom: 1px dashed #ccc;'>");
|
||||
|
||||
// Delete category button
|
||||
sb.append(" <button type='button' class='btn-del' style='vertical-align: middle; margin-left: 10px;' ")
|
||||
.append("onclick=\"if(confirm('Supprimer cette catégorie et tous ses articles ?')) this.closest('.card').remove()\">×</button>");
|
||||
}
|
||||
|
||||
sb.append("</h2>");
|
||||
|
||||
// Build table
|
||||
sb.append("<table id='table-").append(id).append("'>");
|
||||
sb.append("<tr><th>Image</th><th>Nom</th><th>Prix Min</th><th>Prix Max</th><th></th></tr>");
|
||||
|
||||
for (Item item : items) {
|
||||
sb.append(renderItemRow(id, item));
|
||||
}
|
||||
|
||||
sb.append("</table>");
|
||||
|
||||
// Button to add item in this category
|
||||
sb.append("<button type='button' class='btn-add' onclick=\"addRow('").append(id).append("')\">+ Ajouter article</button>");
|
||||
sb.append("</div>");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate HTML row for each item
|
||||
*/
|
||||
private String renderItemRow(String catName, Item item) {
|
||||
String prefix = "item|" + catName + "|" + item.getName();
|
||||
return "<tr>" +
|
||||
"<td><img src='/img_view/" + item.getImage() + "' class='img-p' onerror=\"this.src='https://placehold.co/60?text=?'\">" +
|
||||
"<br><input type='file' accept='.jpg,.png' style='font-size:10px; width:70px' onchange='uImg(this)'>" +
|
||||
"<input type='hidden' name='" + prefix + "|img' value='" + item.getImage() + "'></td>" +
|
||||
"<td><input type='text' name='" + prefix + "|name' value=\"" + item.getName() + "\"></td>" +
|
||||
"<td><input type='number' name='" + prefix + "|min' value='" + item.getMinPrice() + "'></td>" +
|
||||
"<td><input type='number' name='" + prefix + "|max' value='" + item.getMaxPrice() + "'></td>" +
|
||||
// Retro JS compatibility
|
||||
"<td><button type='button' class='btn-del' onclick='var r=this.parentNode.parentNode; r.parentNode.removeChild(r);'>×</button></td>" +
|
||||
"</tr>";
|
||||
}
|
||||
|
||||
private Response serveImage(String filename) {
|
||||
File imgJpg = new File(imagesDir, filename + ".jpg");
|
||||
File imgPng = new File(imagesDir, filename + ".png");
|
||||
File file = imgJpg.exists() ? imgJpg : (imgPng.exists() ? imgPng : null);
|
||||
|
||||
if (file == null || !file.exists()) return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/plain", "No image");
|
||||
|
||||
try {
|
||||
Response res = newChunkedResponse(Response.Status.OK, "image/jpeg", new FileInputStream(file));
|
||||
// Avoid cache if user change image
|
||||
res.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
res.addHeader("Pragma", "no-cache");
|
||||
res.addHeader("Expires", "0");
|
||||
return res;
|
||||
} catch (IOException e) {
|
||||
return newFixedLengthResponse("Error");
|
||||
}
|
||||
}
|
||||
|
||||
private Response handleSaveEditor(IHTTPSession session) {
|
||||
try {
|
||||
// NanoHTTPD reads post request : copy all data then sort parameters
|
||||
Map<String, String> files = new HashMap<>();
|
||||
session.parseBody(files);
|
||||
Map<String, List<String>> params = session.getParameters();
|
||||
|
||||
// Temp struct for modified categories
|
||||
// Key is category is, value is item list
|
||||
Map<String, List<Item>> categoriesMap = new LinkedHashMap<>();
|
||||
// Map id and categories names
|
||||
Map<String, String> categoryNames = new HashMap<>();
|
||||
|
||||
// Identify categories with "cat|id123|name"
|
||||
for (Map.Entry<String, List<String>> paramEntry : params.entrySet()) {
|
||||
String key = paramEntry.getKey();
|
||||
if (key.startsWith("cat|") && key.endsWith("|name")) {
|
||||
String[] parts = key.split("\\|");
|
||||
String catId = parts[1];
|
||||
String catName = paramEntry.getValue().get(0);
|
||||
|
||||
// Create category only if it doesn't exist already
|
||||
if (!categoriesMap.containsKey(catId)) {
|
||||
categoriesMap.put(catId, new ArrayList<>());
|
||||
categoryNames.put(catId, catName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Group field for each items
|
||||
// Key = "catId|itemId", Value = "name, min, max, img""
|
||||
Map<String, Map<String, String>> itemDataCollector = new LinkedHashMap<>();
|
||||
|
||||
for (String key : params.keySet()) {
|
||||
if (key.startsWith("item|") || key.startsWith("new|")) {
|
||||
String[] parts = key.split("\\|"); // [type, catId, itemId, field]
|
||||
String catId = parts[1];
|
||||
String itemId = parts[2];
|
||||
String field = parts[3];
|
||||
List<String> values = params.get(key);
|
||||
String value = (values != null && !values.isEmpty()) ? values.get(0) : "";
|
||||
|
||||
String fullId = catId + "|" + itemId;
|
||||
Map<String, String> itemFields = itemDataCollector.get(fullId);
|
||||
|
||||
if (itemFields == null) {
|
||||
itemFields = new HashMap<>();
|
||||
itemDataCollector.put(fullId, itemFields);
|
||||
}
|
||||
|
||||
itemFields.put(field, value);
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, Map<String, String>> entry : itemDataCollector.entrySet()) {
|
||||
String catId = entry.getKey().split("\\|")[0];
|
||||
Map<String, String> fields = entry.getValue();
|
||||
|
||||
String name = fields.get("name");
|
||||
if (name == null) name = "Sans nom";
|
||||
|
||||
String img = fields.get("img");
|
||||
if (img == null) img = "";
|
||||
|
||||
int min = 0;
|
||||
int max = 0;
|
||||
try {
|
||||
String minStr = fields.get("min");
|
||||
if (minStr != null) min = Integer.parseInt(minStr);
|
||||
|
||||
String maxStr = fields.get("max");
|
||||
if (maxStr != null) max = Integer.parseInt(maxStr);
|
||||
} catch (Exception e) {
|
||||
// If error, keep 0
|
||||
}
|
||||
|
||||
Item item = new Item(name, img, min, max);
|
||||
List<Item> categoryItems = categoriesMap.get(catId);
|
||||
|
||||
if (categoryItems != null) {
|
||||
categoryItems.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
// Stop to use cat ID and replace with cat name
|
||||
Map<String, List<Item>> finalData = new LinkedHashMap<>();
|
||||
for (String catId : categoriesMap.keySet()) {
|
||||
String realName = categoryNames.get(catId);
|
||||
finalData.put(realName, categoriesMap.get(catId));
|
||||
}
|
||||
|
||||
// Save data to disk in JSON format
|
||||
DataLoader.saveData(finalData);
|
||||
|
||||
return newFixedLengthResponse("✅ Catalogue enregistré ! <a href='/'>Retour</a>");
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.e("ControlServer", "Erreur lors de la sauvegarde", e);
|
||||
return newFixedLengthResponse("❌ Erreur : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Soon deprecated with online editor
|
||||
/**
|
||||
* Manage JSON sent by remote.
|
||||
*/
|
||||
private Response handleJsonUpload(IHTTPSession session) {
|
||||
Map<String, String> tmpFiles = new HashMap<>();
|
||||
try {
|
||||
// NanoHTTPD stores loaded file in temp file
|
||||
session.parseBody(tmpFiles);
|
||||
// NanoHTTPD stocke le fichier avec le nom du champ HTML (json_file)
|
||||
// tmpFiles map stores file name and path to tmp folder
|
||||
String tmpPath = tmpFiles.get("json_file");
|
||||
if (tmpPath == null) return newFixedLengthResponse("❌ Aucun fichier reçu.");
|
||||
|
||||
@@ -107,6 +327,7 @@ public class ControlServer extends NanoHTTPD {
|
||||
}
|
||||
}
|
||||
|
||||
// Soon deprecated with online editor
|
||||
/**
|
||||
* Manage multiple images sent by remote.
|
||||
*/
|
||||
@@ -122,17 +343,17 @@ public class ControlServer extends NanoHTTPD {
|
||||
|
||||
int count = 0;
|
||||
|
||||
// On parcourt les fichiers temporaires créés par NanoHTTPD
|
||||
// Browse temp files created by NanoHTTPD
|
||||
for (Map.Entry<String, String> entry : tmpFiles.entrySet()) {
|
||||
// NanoHTTPD indexe les envois multiples (images, images1, images2...)
|
||||
// Manage multiple files uppload
|
||||
if (entry.getKey().startsWith("images")) {
|
||||
String tmpPath = entry.getValue();
|
||||
|
||||
// Récupération sécurisée des paramètres pour obtenir le nom original
|
||||
// Get file name
|
||||
List<String> params = session.getParameters().get(entry.getKey());
|
||||
|
||||
if (params != null && !params.isEmpty()) {
|
||||
// Nettoyage du nom de fichier (pour ne garder que "image.jpg" sans le chemin PC)
|
||||
// Clean file name to remove path, only keep image.jpg or image.png
|
||||
String originalName = new File(params.get(0)).getName();
|
||||
|
||||
File src = new File(tmpPath);
|
||||
@@ -152,6 +373,22 @@ public class ControlServer extends NanoHTTPD {
|
||||
}
|
||||
}
|
||||
|
||||
// Soon deprecated with online editor
|
||||
/**
|
||||
* Utility method to copy file byte per byte.
|
||||
* Not always possible to simply move a file on Android.
|
||||
*/
|
||||
private void copyFile(File src, File dst) throws IOException {
|
||||
try (InputStream in = new FileInputStream(src);
|
||||
OutputStream out = new FileOutputStream(dst)) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare file and offer as download
|
||||
*/
|
||||
@@ -195,19 +432,4 @@ public class ControlServer extends NanoHTTPD {
|
||||
return newFixedLengthResponse("❌ Erreur de lecture.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method to copy file byte per byte.
|
||||
* Not always possible to simply move a file on Android.
|
||||
*/
|
||||
private void copyFile(File src, File dst) throws IOException {
|
||||
try (InputStream in = new FileInputStream(src);
|
||||
OutputStream out = new FileOutputStream(dst)) {
|
||||
byte[] buf = new byte[1024];
|
||||
int len;
|
||||
while ((len = in.read(buf)) > 0) {
|
||||
out.write(buf, 0, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,16 @@ package com.stock.pignon;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class DataLoader {
|
||||
private static final String TAG = "DataLoader"; // For readable logs
|
||||
@@ -20,6 +25,9 @@ public class DataLoader {
|
||||
private static List<Item> cachedGlobals = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* Get data from input JSON PIECES_FILE
|
||||
*/
|
||||
public static void loadData() {
|
||||
File dir = new File(Environment.getExternalStorageDirectory(), EXTERNAL_DIR);
|
||||
File jsonFile = new File(dir, PIECES_FILE);
|
||||
@@ -56,7 +64,10 @@ public class DataLoader {
|
||||
}
|
||||
}
|
||||
|
||||
// Compromise between CPU usage and memory usage : keep cache raw data and combinate global and specific items at call
|
||||
/**
|
||||
* Create a merge list with global items and items from a specified category for main activity
|
||||
* Compromise between CPU usage and memory usage : keep cache raw data and combinate global and specific items at call
|
||||
*/
|
||||
public static List<Item> getItemsForCategory(String categoryName) {
|
||||
|
||||
// Add global items
|
||||
@@ -72,13 +83,71 @@ public class DataLoader {
|
||||
return combinedList;
|
||||
}
|
||||
|
||||
/**
|
||||
* To fill online editor, create a map with all items sorted by category
|
||||
*/
|
||||
public static Map<String, List<Item>> getAllSections() {
|
||||
// LinkedHashMap remember item order instead of HashMap
|
||||
Map<String, List<Item>> sections = new LinkedHashMap<>();
|
||||
sections.put("global", cachedGlobals);
|
||||
for (Category cat : cachedCategories) {
|
||||
sections.put(cat.getName(), cat.getItems());
|
||||
}
|
||||
return sections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a web server to remote management of app assets.
|
||||
*/
|
||||
public static List<Category> getCategories() {
|
||||
return cachedCategories;
|
||||
}
|
||||
|
||||
// Internal class for Gson
|
||||
/**
|
||||
* Internal class for GSON
|
||||
*/
|
||||
private static class CategoriesWrapper {
|
||||
List<Item> globalItems;
|
||||
List<Category> categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write JSON from online editor data
|
||||
*/
|
||||
public static void saveData(Map<String, List<Item>> 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<Item> globalList = sections.get("global");
|
||||
if (globalList == null) {
|
||||
globalList = new ArrayList<>();
|
||||
}
|
||||
|
||||
CategoriesWrapper wrapper = new CategoriesWrapper();
|
||||
wrapper.globalItems = globalList;
|
||||
wrapper.categories = new ArrayList<>();
|
||||
|
||||
// Fill each category
|
||||
for (Map.Entry<String, List<Item>> entry : sections.entrySet()) {
|
||||
if (!"global".equals(entry.getKey())) {
|
||||
wrapper.categories.add(new Category(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to pretty JSON, human readable
|
||||
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")) {
|
||||
writer.write(jsonString);
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
// Update app cache
|
||||
cachedGlobals = wrapper.globalItems;
|
||||
cachedCategories = wrapper.categories;
|
||||
}
|
||||
}
|
||||
@@ -14,9 +14,24 @@ public class Item {
|
||||
// Empty constructor for GSON
|
||||
public Item() {}
|
||||
|
||||
// Full constructor for online editor
|
||||
public Item(String name, String image, int minPrice, int maxPrice) {
|
||||
this.name = name;
|
||||
this.image = image;
|
||||
this.minPrice = minPrice;
|
||||
this.maxPrice = maxPrice;
|
||||
}
|
||||
|
||||
// Getters
|
||||
public String getName() { return name; }
|
||||
public int getMinPrice() { return minPrice; }
|
||||
public int getMaxPrice() { return maxPrice; }
|
||||
public String getImage() { return image; }
|
||||
|
||||
// Setters
|
||||
public void setName(String name) { this.name = name; }
|
||||
public void setMinPrice(int minPrice) { this.minPrice = minPrice; }
|
||||
public void setMaxPrice(int maxPrice) { this.maxPrice = maxPrice; }
|
||||
public void setImage(String image) { this.image = image; }
|
||||
}
|
||||
|
||||
|
||||
@@ -89,7 +89,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
// Launch server
|
||||
server = new ControlServer(8080);
|
||||
server = new ControlServer(this,8080);
|
||||
try {
|
||||
server.start();
|
||||
String url = "http://" + getDeviceIP() + ":8080";
|
||||
|
||||
Reference in New Issue
Block a user