6 Commits

79 changed files with 947 additions and 245 deletions

View File

@@ -10,8 +10,8 @@ android {
applicationId "com.stock.pignon" applicationId "com.stock.pignon"
minSdkVersion 17 minSdkVersion 17
targetSdkVersion 36 targetSdkVersion 36
versionCode 5 versionCode 7
versionName "0.5.0" versionName "0.5.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@@ -0,0 +1,316 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Éditeur de Catalogue</title>
<style>
body {
font-family: sans-serif;
padding: 20px;
background: #eee;
margin-bottom: 100px;
}
.card {
background: white;
padding: 20px;
margin-bottom: 30px;
border-radius: 10px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
table {
width: 100%;
border-collapse: collapse;
}
th,
td {
padding: 10px;
border-bottom: 1px solid #ddd;
text-align: left;
}
input[type='text'],
input[type='number'] {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
width: 100%;
box-sizing: border-box;
}
.img-p {
width: 60px;
height: 60px;
object-fit: cover;
background: #f0f0f0;
border-radius: 5px;
}
.btn-add {
background: #0049AF;
color: white;
border: none;
padding: 10px 20px;
cursor: pointer;
margin-top: 10px;
border-radius: 5px;
font-weight: bold;
}
.btn-save {
background: #2E7D32;
color: white;
padding: 15px 30px;
border: none;
border-radius: 50px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.4);
width: auto;
white-space: nowrap;
}
.btn-del {
background: #d32f2f;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
padding: 5px 10px;
}
.link-mod {
background: none;
border: none;
color: #0049AF;
text-decoration: underline;
cursor: pointer;
padding: 0;
font-size: 12px;
appearance: none;
-webkit-appearance: none;
outline: none;
}
.actions-bar {
position: fixed;
bottom: 30px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
display: flex;
gap: 15px;
}
.btn-cancel {
background: #F54927;
color: white;
padding: 15px 30px;
border: none;
border-radius: 50px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
white-space: nowrap;
}
.btn-cat {
background: #0049AF;
color: white;
padding: 15px 25px;
border: none;
border-radius: 50px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
.btn-back {
background: #000000;
color: white;
padding: 15px 25px;
border: none;
border-radius: 50px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}
.input-h2 {
font-size: 1.5rem !important;
font-weight: bold !important;
border: none !important;
background: transparent !important;
color: #555 !important;
width: auto;
max-width: 20%;
border-bottom: 1px dashed #ccc !important;
outline: none;
}
.input-h2:focus {
border-bottom: 2px solid #0049AF;
color: #0049AF;
}
</style>
</head>
<body>
<h1>Éditeur de Catalogue</h1>
<form id="editor-form" accept-charset="UTF-8">
{{GENERATED_CONTENT}}
<div class="actions-bar">
<button type="button" class="btn-back" onclick="window.location.href= '/'">← Retour </button>
<button type="button" class="btn-cat" onclick="addCategory()">+ Catégorie</button>
<button type="button" onclick="sendData()" class="btn-save" style="position:static; transform:none;">💾
Enregistrer tout</button>
<button type="button" class="btn-cancel" onclick="handleCancel(event)">✖ Tout annuler</button>
</div>
</form>
<script>
function addRow(cat) {
var t = document.getElementById('table-' + cat);
var r = t.insertRow(-1);
var id = Date.now();
r.innerHTML = '<td>' +
'<img src="" class="img-p" onerror="this.src=\'https://placehold.co/60?text=?\'" style="display:block; margin-bottom:5px;">' +
'<input type="file" accept=".jpg,.png" style="font-size:10px; width:70px" onchange="uImg(this)">' +
'<input type="hidden" name="new|' + cat + '|' + id + '|img" value="">' +
'</td>' +
'<td><input type="text" name="new|' + cat + '|' + id + '|name" placeholder="Nom..." required></td>' +
'<td><input type="number" name="new|' + cat + '|' + id + '|min" value="0" min="0" required></td>' +
'<td><input type="number" name="new|' + cat + '|' + id + '|max" value="0" min="0" required></td>' +
'<td><button type="button" class="btn-del" onclick="var l=this.parentNode.parentNode; l.parentNode.removeChild(l);">×</button></td>';
}
function uImg(element) {
var f = element.files[0];
if (!f) return;
var td = element.parentNode;
var imgTag = td.querySelector('.img-p');
var hiddenInput = td.querySelector('input[type="hidden"]');
var reader = new FileReader();
reader.onload = function(e) {
imgTag.src = e.target.result;
hiddenInput.value = e.target.result;
};
reader.readAsDataURL(f);
}
function handleCancel(e) {
// Avoid form to wait for data to send
if (e) e.preventDefault();
if (confirm("Attention : toutes vos modifications seront perdues. Continuer ?")) {
// Random URL parameters ?t= to force browser to forget previous data
window.location.href = "/edit?t=" + Date.now();
}
}
function addCategory() {
var name = prompt("Nom de la nouvelle catégorie ?");
if (!name || name.trim() === "") return;
// Temp ID
var tempId = "newcat_" + Date.now();
var container = document.createElement('div');
container.className = 'card';
container.innerHTML =
'<h2>' +
'<input type="text" class="input-h2" name="cat|' + tempId + '|name" value="' + name + '">' +
'<div style="display:inline-block; vertical-align:middle; margin: 0 15px;">' +
'<small>Fond:</small><br><input type="color" name="cat|' + tempId + '|bgColor" value="#0049AF" style="width:30px; height:25px; border:none; cursor:pointer;">' +
'</div>' +
'<div style="display:inline-block; vertical-align:middle; margin-right:15px;">' +
'<small>Texte:</small><br><input type="color" name="cat|' + tempId + '|textColor" value="#FFFFFF" style="width:30px; height:25px; border:none; cursor:pointer;">' +
'</div>' +
' <button type="button" class="btn-del" onclick="if(confirm(\'Supprimer cette catégorie ?\')) this.closest(\'.card\').remove()">×</button>' +
'</h2>' +
'<table id="table-' + tempId + '">' +
'<tr><th>Image</th><th>Nom</th><th>Prix Min</th><th>Prix Max</th><th></th></tr>' +
'</table>' +
'<button type="button" class="btn-add" onclick="addRow(\'' + tempId + '\')">+ Ajouter article</button>';
var form = document.querySelector('form');
var actionBar = document.querySelector('.actions-bar');
form.insertBefore(container, actionBar);
container.scrollIntoView({ behavior: 'smooth' });
}
function sendData() {
const form = document.getElementById('editor-form');
const data = {
cat_order_list: "",
items: {}
};
// Browse showed categories
const catOrder = [];
document.querySelectorAll('.card').forEach((card, catIdx) => {
let techId = card.querySelector('input[name^="cat|"]')?.name.split('|')[1] || "c" + catIdx; // c0, c1 but avoid globals
catOrder.push(techId);
const catInput = card.querySelector('input[name$="|name"]');
const bgInput = card.querySelector('input[name$="|bgColor"]');
const textInput = card.querySelector('input[name$="|textColor"]');
if (!catInput) return;
data.items["cat|" + techId + "|name"] = catInput.value;
data.items["cat|" + techId + "|bgColor"] = bgInput ? bgInput.value : "#0049AF";
data.items["cat|" + techId + "|textColor"] = textInput ? textInput.value : "#FFFFFF";
// Browse each line
card.querySelectorAll('table tr').forEach((row, itemIdx) => {
if (itemIdx === 0) return; // Skip title line (th)
const realIdx = itemIdx - 1; // Start at 0
row.querySelectorAll('input').forEach(input => {
const parts = input.name.split('|');
if (parts.length >= 4) {
// parts[0] = "item" ou "new", parts[3] = "name" ou "min" ou "max"
const finalKey = "item|" + techId + "|" + realIdx + "|" + parts[3];
data.items[finalKey] = input.value;
}
});
});
});
data.cat_order_list = catOrder.join(',');
// Create new clean struct : cat|c0|name, item|c0|0|name...
fetch('/save_editor', {
method: 'POST',
headers: { 'Content-Type': 'application/json; charset=UTF-8' },
body: JSON.stringify(data)
})
.then(r => {
if (r.ok) {
alert("✅ Enregistré !");
window.location.reload(); // Optionnel : rafraîchit pour voir le résultat propre
} else {
alert("❌ Erreur serveur");
}
})
.catch(() => alert("❌ Erreur réseau"));
}
</script>
</body>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

Before

Width:  |  Height:  |  Size: 6.4 KiB

After

Width:  |  Height:  |  Size: 6.4 KiB

View File

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.5 KiB

View File

Before

Width:  |  Height:  |  Size: 9.7 KiB

After

Width:  |  Height:  |  Size: 9.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

Before

Width:  |  Height:  |  Size: 8.0 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

View File

Before

Width:  |  Height:  |  Size: 5.8 KiB

After

Width:  |  Height:  |  Size: 5.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 19 KiB

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 26 KiB

After

Width:  |  Height:  |  Size: 26 KiB

View File

Before

Width:  |  Height:  |  Size: 7.0 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.9 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 16 KiB

View File

Before

Width:  |  Height:  |  Size: 8.6 KiB

After

Width:  |  Height:  |  Size: 8.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 14 KiB

View File

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 6.6 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View File

Before

Width:  |  Height:  |  Size: 13 KiB

After

Width:  |  Height:  |  Size: 13 KiB

View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 7.4 KiB

After

Width:  |  Height:  |  Size: 7.4 KiB

View File

@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>
body { font-family: sans-serif; line-height: 1.6; padding: 20px; color: #333; max-width: 800px; margin: auto; }
h1 { color: #0049AF; border-bottom: 2px solid #0049AF; }
h2 { color: #555; margin-top: 30px; }
.card { background: #f4f4f4; padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #ddd; }
.btn { display: inline-block; width: 280px; height: 45px; line-height: 45px; text-align: center; background: #0049AF; color: white; text-decoration: none; border-radius: 4px; border: none; cursor: pointer; font-weight: bold; box-sizing: border-box; padding: 0; -webkit-appearance: none; font-family: inherit; font-size: 14px; font-style: normal; letter-spacing: normal; margin: 10px 5px; vertical-align: middle;}
.btn-download { background: #2E7D32; }
input[type='file'] { margin: 10px 0; }
.status { font-weight: bold; color: green; }
</style>
</head>
<body>
<h1>Application : stock-pignon</h1>
<h2>Gestion des stocks</h2>
<div class='card'>
<h3>Récupérer les sacoches des adhérent·es</h3>
<p>Télécharger le décompte des pièces sorties de l'atelier.</p>
<a href='/output_json' class='btn'>Voir stock.json</a>
<a href='/output_csv' class='btn'>Voir stock.csv</a>
<a href='/download_output_json' class='btn'>Télécharger stock.json</a>
<a href='/download_output_csv' class='btn'>Télécharger stock.csv</a>
</div>
<h2>Modifier le catalogue</h2>
<div class='card'>
<h3>Gestion du fichier JSON</h3>
<p>Catalogue actuel : <a href='/download_input' class='btn'>Télécharger pieces.json</a></p>
<hr>
<form action='/upload_json' method='post' enctype='multipart/form-data'>
<p><strong>Envoyer un nouveau catalogue :</strong></p>
<input type='file' name='json_file' accept='.json'><br>
<input type='submit' value='Remplacer pieces.json' class='btn'>
</form>
</div>
<div class='card'>
<h3>Gestion des images</h3>
<form action='/upload_images' method='post' enctype='multipart/form-data'>
<p><strong>Ajouter/Modifier des images (PNG/JPG) :</strong></p>
<input type='file' name='images' accept='image/*' multiple><br>
<input type='submit' value='Envoyer les images' class='btn'>
</form>
</div><div class='card'>
<h3>Modifier dans le navigateur</h3>
<p><a href='/edit' class='btn'>Editeur en ligne</a></p>
</div>
</body>
</html>

View File

@@ -8,10 +8,11 @@
"bgColor": "#FF0000", "bgColor": "#FF0000",
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Vélo", "minPrice": 20, "maxPrice": 40, "image": "velo"},
{"name": "Cadre", "minPrice": 20, "maxPrice": 40, "image": "cadre"}, {"name": "Cadre", "minPrice": 20, "maxPrice": 40, "image": "cadre"},
{"name": "Selle", "minPrice": 1, "maxPrice": 7, "image": "selle"}, {"name": "Selle", "minPrice": 1, "maxPrice": 7, "image": "selle"},
{"name": "Tige de selle", "minPrice": 1, "maxPrice": 10, "image": "tigeselle"}, {"name": "Tige de selle", "minPrice": 1, "maxPrice": 10, "image": "tige_selle"},
{"name": "Collier de selle", "minPrice": 1, "maxPrice": 4, "image": "collierselle"} {"name": "Collier de selle", "minPrice": 1, "maxPrice": 4, "image": "collier_selle"}
] ]
}, },
{ {
@@ -20,10 +21,14 @@
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Fourche", "minPrice": 10, "maxPrice": 20, "image": "fourche"}, {"name": "Fourche", "minPrice": 10, "maxPrice": 20, "image": "fourche"},
{"name": "Jeu de direction", "minPrice": 1, "maxPrice": 8, "image": "jeudirection"}, {"name": "Fourche suspendue", "minPrice": 10, "maxPrice": 20, "image": "fourche_suspendue"},
{"name": "Potence headset", "minPrice": 2, "maxPrice": 8, "image": "potenceheadset"}, {"name": "Roulement direction", "minPrice": 1, "maxPrice": 8, "image": "roulement_direction"},
{"name": "Potence plongeur", "minPrice": 2, "maxPrice": 8, "image": "potenceplongeur"}, {"name": "Jeu de direction", "minPrice": 1, "maxPrice": 8, "image": "jeu_direction"},
{"name": "Tige plongeur", "minPrice": 1, "maxPrice": 4, "image": "tigeplongeur"} {"name": "Potence headset", "minPrice": 2, "maxPrice": 8, "image": "potence_headset"},
{"name": "Potence headset réglable", "minPrice": 2, "maxPrice": 8, "image": "potence_headset_reglable"},
{"name": "Potence plongeur", "minPrice": 2, "maxPrice": 8, "image": "potence_plongeur"},
{"name": "Potence plongeur réglable", "minPrice": 2, "maxPrice": 8, "image": "potence_plongeur_reglable"},
{"name": "Tige plongeur", "minPrice": 1, "maxPrice": 4, "image": "tige_plongeur"}
] ]
}, },
{ {
@@ -31,13 +36,14 @@
"bgColor": "#F16000", "bgColor": "#F16000",
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Poignée combo", "minPrice": 2, "maxPrice": 8, "image": "poigneecombo"}, {"name": "Poignée combo", "minPrice": 2, "maxPrice": 8, "image": "poignee_combo"},
{"name": "Poignée frein", "minPrice": 2, "maxPrice": 8, "image": "poigneefrein"}, {"name": "Poignée frein", "minPrice": 2, "maxPrice": 8, "image": "poignee_frein"},
{"name": "Poignée gachette", "minPrice": 2, "maxPrice": 8, "image": "poigneegachette"}, {"name": "Poignée gachette", "minPrice": 2, "maxPrice": 8, "image": "poignee_gachette"},
{"name": "Poignée tournante", "minPrice": 2, "maxPrice": 6, "image": "poigneetournante"}, {"name": "Poignée tournante", "minPrice": 2, "maxPrice": 6, "image": "poignee_tournante"},
{"name": "Manette cadre", "minPrice": 2, "maxPrice": 6, "image": "manette_cadre"},
{"name": "Gaine", "minPrice": 0, "maxPrice": 2, "image": "gaine"}, {"name": "Gaine", "minPrice": 0, "maxPrice": 2, "image": "gaine"},
{"name": "Câble", "minPrice": 0, "maxPrice": 1, "image": "cable"}, {"name": "Câble", "minPrice": 0, "maxPrice": 1, "image": "cable"},
{"name": "Poignée", "minPrice": 1, "maxPrice": 4, "image": "poignees"}, {"name": "Poignée", "minPrice": 1, "maxPrice": 4, "image": "poignee"},
{"name": "Cintre", "minPrice": 5, "maxPrice": 10,"image": "cintre"} {"name": "Cintre", "minPrice": 5, "maxPrice": 10,"image": "cintre"}
] ]
}, },
@@ -46,11 +52,15 @@
"bgColor": "#FF00FF", "bgColor": "#FF00FF",
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Étrier patin", "minPrice": 2, "maxPrice": 8, "image": "etrierpatin"}, {"name": "Étrier V-brake", "minPrice": 2, "maxPrice": 8, "image": "etrier_vbrake"},
{"name": "Étrier disque", "minPrice": 2, "maxPrice": 8, "image": "etrierdisque"}, {"name": "Étrier Cantilever", "minPrice": 2, "maxPrice": 8, "image": "etrier_cantilever"},
{"name": "Étrier Caliper ou Mafac", "minPrice": 2, "maxPrice": 8, "image": "etrier_mafac_caliper"},
{"name": "Étrier disque câble", "minPrice": 2, "maxPrice": 8, "image": "etrier_cable"},
{"name": "Étrier disque hydraulique", "minPrice": 2, "maxPrice": 8, "image": "etrier_hydraulique"},
{"name": "Patins", "minPrice": 1, "maxPrice": 2, "image": "patins"}, {"name": "Patins", "minPrice": 1, "maxPrice": 2, "image": "patins"},
{"name": "Disque", "minPrice": 1, "maxPrice": 5, "image": "disque"}, {"name": "Disque", "minPrice": 1, "maxPrice": 5, "image": "disque"},
{"name": "Visserie frein", "minPrice": 0, "maxPrice": 1, "image": "visseriefrein"} {"name": "Pont central cantilever", "minPrice": 0, "maxPrice": 1, "image": "frein_pontcentral"},
{"name": "Visserie frein", "minPrice": 0, "maxPrice": 1, "image": "visserie_frein"}
] ]
}, },
{ {
@@ -58,15 +68,23 @@
"bgColor": "#0000FF", "bgColor": "#0000FF",
"textColor": "#FFFFFF", "textColor": "#FFFFFF",
"items": [ "items": [
{"name": "Roue avant", "minPrice": 20, "maxPrice": 40, "image": "roueavant"}, {"name": "Chambre à air", "minPrice": 20, "maxPrice": 40, "image": "chambre_air"},
{"name": "Roue arrière", "minPrice": 20, "maxPrice": 40, "image": "rouearriere"}, {"name": "Roue avant", "minPrice": 20, "maxPrice": 40, "image": "roue_avant"},
{"name": "Axe rapide", "minPrice": 1, "maxPrice": 2, "image": "axerapide"}, {"name": "Roue arrière", "minPrice": 20, "maxPrice": 40, "image": "roue_arriere"},
{"name": "Axe roue", "minPrice": 1, "maxPrice": 3, "image": "axeroue"}, {"name": "Axe rapide", "minPrice": 1, "maxPrice": 2, "image": "axe_rapide"},
{"name": "Axe roue", "minPrice": 1, "maxPrice": 3, "image": "axe_roue"},
{"name": "Rayon", "minPrice": 0, "maxPrice": 2, "image": "rayon"}, {"name": "Rayon", "minPrice": 0, "maxPrice": 2, "image": "rayon"},
{"name": "Moyeu avant", "minPrice": 2, "maxPrice": 8, "image": "moyeuavant"}, {"name": "Jante", "minPrice": 0, "maxPrice": 2, "image": "jante"},
{"name": "Moyeu arrière", "minPrice": 2, "maxPrice": 10, "image": "moyeuarriere"}, {"name": "Fond de jante", "minPrice": 0, "maxPrice": 2, "image": "fond_jante"},
{"name": "Pneu", "minPrice": 10, "maxPrice": 20, "image": "pneu"}, {"name": "Moyeu avant", "minPrice": 2, "maxPrice": 8, "image": "moyeu_avant"},
{"name": "Visserie roue", "minPrice": 0, "maxPrice": 1, "image": "visserieroue"} {"name": "Moyeu arrière cassette", "minPrice": 2, "maxPrice": 10, "image": "moyeu_arriere_cassette"},
{"name": "Moyeu arrière roue libre", "minPrice": 2, "maxPrice": 10, "image": "moyeu_arriere_rouelibre"},
{"name": "Moyeu arrière rétropédalage", "minPrice": 2, "maxPrice": 10, "image": "moyeu_retropedalage"},
{"name": "Pneu petite taille", "minPrice": 10, "maxPrice": 20, "image": "pneu_petit"},
{"name": "Pneu 26\"", "minPrice": 10, "maxPrice": 20, "image": "pneu_26"},
{"name": "Pneu 28\" route", "minPrice": 10, "maxPrice": 20, "image": "pneu_route"},
{"name": "Pneu 28\" large", "minPrice": 10, "maxPrice": 20, "image": "pneu_large"},
{"name": "Visserie roue", "minPrice": 0, "maxPrice": 1, "image": "visserie_roue"}
] ]
}, },
{ {
@@ -74,12 +92,13 @@
"bgColor": "#FFCC00", "bgColor": "#FFCC00",
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Dérailleur avant", "minPrice": 2, "maxPrice": 10, "image": "fd"}, {"name": "Dérailleur arrière", "minPrice": 2, "maxPrice": 10, "image": "derailleur_arriere"},
{"name": "Dérailleur arrière", "minPrice": 4, "maxPrice": 12, "image": "rd"}, {"name": "Dérailleur avant tirage bas", "minPrice": 4, "maxPrice": 12, "image": "derailleur_avant_tiragebas"},
{"name": "Dérailleur avant tirage haut", "minPrice": 4, "maxPrice": 12, "image": "derailleur_avant_tiragebas"},
{"name": "Cassette", "minPrice": 2, "maxPrice": 6, "image": "cassette"}, {"name": "Cassette", "minPrice": 2, "maxPrice": 6, "image": "cassette"},
{"name": "Roue libre", "minPrice": 2, "maxPrice": 6, "image": "rouelibre"}, {"name": "Roue libre", "minPrice": 2, "maxPrice": 6, "image": "roue_libre"},
{"name": "Chaîne", "minPrice": 2, "maxPrice": 6, "image": "chaine"}, {"name": "Chaîne", "minPrice": 2, "maxPrice": 6, "image": "chaine"},
{"name": "Visserie transmission", "minPrice": 0, "maxPrice": 1, "image": "visserietransmission"} {"name": "Visserie transmission", "minPrice": 0, "maxPrice": 1, "image": "visserie_transmission"}
] ]
}, },
{ {
@@ -87,13 +106,17 @@
"bgColor": "#00FFFF", "bgColor": "#00FFFF",
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Boîtier pédalier", "minPrice": 4, "maxPrice": 12, "image": "boitierpedalier"}, {"name": "Axe pédalier carré ou clavette", "minPrice": 4, "maxPrice": 12, "image": "axe_pedalier"},
{"name": "Manivelle gauche", "minPrice": 4, "maxPrice": 12, "image": "manivellegauche"}, {"name": "Roulement pédalier", "minPrice": 4, "maxPrice": 12, "image": "roulement_pedalier"},
{"name": "Manivelle droite", "minPrice": 4, "maxPrice": 12, "image": "manivelledroite"}, {"name": "Boîtier pédalier cartouche", "minPrice": 4, "maxPrice": 12, "image": "boitier_cartouche"},
{"name": "Pédalier complet", "minPrice": 6, "maxPrice": 15, "image": "pedaliercomplet"}, {"name": "Boîtier pédalier Hollowtech 2", "minPrice": 4, "maxPrice": 12, "image": "boitier_hollowtech"},
{"name": "Manivelle gauche", "minPrice": 4, "maxPrice": 12, "image": "manivelle_gauche"},
{"name": "Manivelle droite", "minPrice": 4, "maxPrice": 12, "image": "manivelle_droite"},
{"name": "Pédalier complet", "minPrice": 6, "maxPrice": 15, "image": "pedalier_complet"},
{"name": "Pédales", "minPrice": 2, "maxPrice": 8, "image": "pedales"}, {"name": "Pédales", "minPrice": 2, "maxPrice": 8, "image": "pedales"},
{"name": "Plateau", "minPrice": 2, "maxPrice": 8, "image": "plateau"}, {"name": "Plateau 4 branches", "minPrice": 2, "maxPrice": 8, "image": "plateau_4"},
{"name": "Visserie pédalier", "minPrice": 0, "maxPrice": 1, "image": "visseriepedalier"} {"name": "Plateau 5 branches", "minPrice": 2, "maxPrice": 8, "image": "plateau_5"},
{"name": "Visserie pédalier", "minPrice": 0, "maxPrice": 1, "image": "visserie_pedalier"}
] ]
}, },
{ {
@@ -102,10 +125,13 @@
"textColor": "#000000", "textColor": "#000000",
"items": [ "items": [
{"name": "Béquille", "minPrice": 2, "maxPrice": 8, "image": "bequille"}, {"name": "Béquille", "minPrice": 2, "maxPrice": 8, "image": "bequille"},
{"name": "Garde boue", "minPrice": 2, "maxPrice": 8, "image": "gardeboue"}, {"name": "Garde boue", "minPrice": 2, "maxPrice": 8, "image": "garde_boue"},
{"name": "Lampe", "minPrice": 1, "maxPrice": 5, "image": "lampe"}, {"name": "Lampe", "minPrice": 1, "maxPrice": 5, "image": "lampe"},
{"name": "Porte bagage", "minPrice": 2, "maxPrice": 10, "image": "portebagage"}, {"name": "Porte bagage", "minPrice": 2, "maxPrice": 10, "image": "porte_bagage"},
{"name": "Porte bidon", "minPrice": 0, "maxPrice": 1, "image": "portebidon"} {"name": "Porte bidon", "minPrice": 0, "maxPrice": 1, "image": "porte_bidon"},
{"name": "Pompe", "minPrice": 0, "maxPrice": 1, "image": "pompe"},
{"name": "Panier", "minPrice": 0, "maxPrice": 1, "image": "panier"},
{"name": "Rétroviseur", "minPrice": 0, "maxPrice": 1, "image": "retroviseur"}
] ]
} }
] ]

View File

@@ -20,8 +20,10 @@ public class CartItem {
public int getMinPrice() { return minPrice; } public int getMinPrice() { return minPrice; }
public int getMaxPrice() { return maxPrice; } public int getMaxPrice() { return maxPrice; }
public int getQuantity() { return quantity; } public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public int getTotalMin() { return minPrice * quantity; } public int getTotalMin() { return minPrice * quantity; }
public int getTotalMax() { return maxPrice * quantity; } public int getTotalMax() { return maxPrice * quantity; }
public String getImageFile() { return imageFile; } public String getImageFile() { return imageFile; }
// Setters
public void setQuantity(int quantity) { this.quantity = quantity; }
} }

View File

@@ -8,10 +8,14 @@ public class CartManager {
// Unified and global list for whole application // Unified and global list for whole application
private static final List<CartItem> items = new ArrayList<>(); 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() {} 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() { public static List<CartItem> getItems() {
return items; return items;
} }
@@ -25,7 +29,9 @@ public class CartManager {
return null; 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) { public static void addOrUpdateItem(String name, int minPrice, int maxPrice, int quantity, String imageFile) {
CartItem current = getItemByName(name); CartItem current = getItemByName(name);
@@ -40,20 +46,27 @@ public class CartManager {
} }
} }
// Range-based pricing /**
* Range-based pricing
*/
public static int getGlobalTotalMin() { public static int getGlobalTotalMin() {
int total = 0; int total = 0;
for (CartItem item : items) total += item.getTotalMin(); for (CartItem item : items) total += item.getTotalMin();
return total; return total;
} }
/**
* Range-based pricing
*/
public static int getGlobalTotalMax() { public static int getGlobalTotalMax() {
int total = 0; int total = 0;
for (CartItem item : items) total += item.getTotalMax(); for (CartItem item : items) total += item.getTotalMax();
return total; return total;
} }
/**
* Remove item from cart
*/
public static void clear() { public static void clear() {
items.clear(); items.clear();
} }

View File

@@ -22,16 +22,9 @@ import java.util.List;
public class CartViewHelper { public class CartViewHelper {
/** /**
* Short version for app resume : update without changing validate/back button state * Refreshes cart UI by synchronizing the visual list with the current CartManager state
*/ */
public static void updateCartView(LinearLayout cartList, Context context) { 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; if (cartList == null) return;
// Clean item lists // Clean item lists
@@ -60,11 +53,6 @@ public class CartViewHelper {
} }
updateTotalDisplay(context, totalMin, totalMax); updateTotalDisplay(context, totalMin, totalMax);
} }
// Update validate/back button
if (updateButton && context instanceof MainActivity) {
((MainActivity) context).updateActionButton(isHome);
}
} }
/** /**

View File

@@ -14,12 +14,25 @@ public class Category {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private List<Item> items; 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 getName() { return name; }
public String getBgColor() { return bgColor; } public String getBgColor() { return bgColor; }
public String getTextColor() { return textColor; } public String getTextColor() { return textColor; }
public List<Item> getItems() { return items != null ? items : new ArrayList<>(); }
// Avoid crash if json isn't readable // Setters
public List<Item> getItems() { public void setBgColor(String bgColor) { this.bgColor = bgColor; }
return items != null ? items : new ArrayList<>(); public void setTextColor(String textColor) { this.textColor = textColor; }
}
} }

View File

@@ -1,7 +1,10 @@
// ControlServer.java // ControlServer.java
package com.stock.pignon; package com.stock.pignon;
import android.content.Context;
import android.os.Environment; import android.os.Environment;
import android.util.Log;
import org.json.JSONObject;
import fi.iki.elonen.NanoHTTPD; import fi.iki.elonen.NanoHTTPD;
import java.io.File; import java.io.File;
@@ -10,60 +13,74 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Scanner;
/** /**
* Create a web server to remote management of app assets. * 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 { public class ControlServer extends NanoHTTPD {
private final Context context;
// Assets folders // Assets folders
private final File storageRoot = Environment.getExternalStorageDirectory(); private final File storageRoot = Environment.getExternalStorageDirectory();
private final File baseDir = new File(storageRoot, Config.EXTERNAL_DIR_NAME); private final File baseDir = new File(storageRoot, Config.EXTERNAL_DIR_NAME);
private final File imagesDir = new File(baseDir, Config.IMAGES_SUBDIR_NAME); private final File imagesDir = new File(baseDir, Config.IMAGES_SUBDIR_NAME);
public ControlServer(int port) { public ControlServer(Context context, int port) {
super(port); super(port);
this.context = context;
} }
/** /**
* Main method which receive remote request * Main method which manage requests
*/ */
@Override @Override
public Response serve(IHTTPSession session) { public Response serve(IHTTPSession session) {
// Get requested address // Get requested address
String uri = session.getUri(); String uri = session.getUri();
switch (uri) { if (uri.startsWith("/img_view/")) {
case "/download_input": return serveImage(uri.replace("/img_view/", ""));
return downloadFile(Config.INPUT_JSON_NAME); }
case "/output_json": if (session.getMethod() == Method.GET) {
return viewFile(Config.OUTPUT_JSON_NAME); switch (uri) {
// Soon deprecated with online editor
case "/download_output_json": case "/download_input":
return downloadFile(Config.OUTPUT_JSON_NAME); return downloadFile(Config.INPUT_JSON_NAME);
// Soon deprecated with online editor
case "/output_csv": case "/output_json":
return viewFile(Config.OUTPUT_CSV_NAME); return viewFile(Config.OUTPUT_JSON_NAME);
case "/download_output_json":
case "/download_output_csv": return downloadFile(Config.OUTPUT_JSON_NAME);
return downloadFile(Config.OUTPUT_CSV_NAME); case "/output_csv":
return viewFile(Config.OUTPUT_CSV_NAME);
case "/": case "/download_output_csv":
case "/index.html": return downloadFile(Config.OUTPUT_CSV_NAME);
return newFixedLengthResponse(getHtmlResponse()); case "/edit":
return newFixedLengthResponse(fillEditor());
case "/":
return newFixedLengthResponse(getHtml("index.html"));
}
} }
// File upload management
if (session.getMethod() == Method.POST) { if (session.getMethod() == Method.POST) {
if (uri.equals("/upload_json")) { switch (uri) {
// Soon deprecated with online editor
case "/upload_json":
return handleJsonUpload(session); return handleJsonUpload(session);
} else if (uri.equals("/upload_images")) { // Soon deprecated with online editor
case "/upload_images":
return handleImagesUpload(session); return handleImagesUpload(session);
case "/save_editor":
return handleSaveEditor(session);
} }
} }
@@ -71,70 +88,247 @@ public class ControlServer extends NanoHTTPD {
} }
/** /**
* HTML UI for users * Read from asset and return html file
*/ */
private String getHtmlResponse() { private String getHtml(String file) {
return "<html>" + try {
"<head>" + InputStream is = context.getAssets().open(file);
"<meta charset='UTF-8'>" + // Scanner html file from the beginning with "\\A"
"<meta name='viewport' content='width=device-width, initial-scale=1'>" + Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");
"<style>" + String html = s.hasNext() ? s.next() : "";
"body { font-family: sans-serif; line-height: 1.6; padding: 20px; color: #333; max-width: 800px; margin: auto; }" + is.close();
"h1 { color: #0049AF; border-bottom: 2px solid #0049AF; }" + return html;
"h2 { color: #555; margin-top: 30px; }" + } catch (IOException e) {
".card { background: #f4f4f4; padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #ddd; }" + return "<html><body>❌ Erreur de chargement de la page</body></html>";
".btn { display: inline-block; width: 280px; height: 45px; line-height: 45px; text-align: center; background: #0049AF; color: white; text-decoration: none; border-radius: 4px; border: none; cursor: pointer; font-weight: bold; box-sizing: border-box; padding: 0; -webkit-appearance: none; font-family: inherit; font-size: 14px; font-style: normal; letter-spacing: normal; margin: 10px 5px; vertical-align: middle;}" + }
".btn-download { background: #2E7D32; }" +
"input[type='file'] { margin: 10px 0; }" +
".status { font-weight: bold; color: green; }" +
"</style>" +
"</head>" +
"<body>" +
"<h1>Application : stock-pignon</h1>" +
"<h2>Gestion des stocks</h2>" +
"<div class='card'>" +
"<h3>Récupérer les sacoches des adhérent·es</h3>" +
"<p>Télécharger le décompte des pièces sorties de l'atelier.</p>" +
"<a href='/output_json' class='btn'>Voir stock.json</a>" +
"<a href='/output_csv' class='btn'>Voir stock.csv</a>" +
"<a href='/download_output_json' class='btn'>Télécharger stock.json</a>" +
"<a href='/download_output_csv' class='btn'>Télécharger stock.csv</a>" +
"</div>" +
"<h2>Modifier le catalogue</h2>" +
"<div class='card'>" +
"<h3>Gestion du fichier JSON</h3>" +
"<p>Catalogue actuel : <a href='/download_input' class='btn'>Télécharger pieces.json</a></p>" +
"<hr>" +
"<form action='/upload_json' method='post' enctype='multipart/form-data'>" +
"<p><strong>Envoyer un nouveau catalogue :</strong></p>" +
"<input type='file' name='json_file' accept='.json'><br>" +
"<input type='submit' value='Remplacer pieces.json' class='btn'>" +
"</form>" +
"</div>" +
"<div class='card'>" +
"<h3>Gestion des images</h3>" +
"<form action='/upload_images' method='post' enctype='multipart/form-data'>" +
"<p><strong>Ajouter/Modifier des images (PNG/JPG) :</strong></p>" +
"<input type='file' name='images' accept='image/*' multiple><br>" +
"<input type='submit' value='Envoyer les images' class='btn'>" +
"</form>" +
"</div>" +
"</body>" +
"</html>";
} }
/**
* Read from asset and return html file
*/
private String fillEditor() {
// Get data from assets
DataLoader.loadData();
// Create an html string to fill the template
StringBuilder html = new StringBuilder();
// Save categories order
List<String> orderList = new ArrayList<>();
// Globals
String globalId = "global";
Category globalCat = new Category("global", DataLoader.getGlobalItems());
html.append(renderCategorySection(globalId, globalCat));
orderList.add(globalId);
// Browser categories
int index = 0;
for (Category cat : DataLoader.getCategories()) {
// cat_0, cat_1...
String techId = "cat_" + index;
html.append(renderCategorySection(techId, cat));
orderList.add(techId);
index++;
}
String orderField = "<input type='hidden' name='cat_order_list' value='" +
android.text.TextUtils.join(",", orderList) + "'>";
return getHtml("editor.html").replace("{{GENERATED_CONTENT}}", html + orderField);
}
/**
* Generate HTML section for each category
*/
private String renderCategorySection(String techId, Category cat) {
String displayName = cat.getName();
List<Item> items = cat.getItems();
StringBuilder sb = new StringBuilder();
sb.append("<div class='card'>");
sb.append("<h2>");
if ("global".equals(displayName)) {
sb.append("Articles globaux");
sb.append("<input type='hidden' name='cat|global|name' value='global'>");
techId = "global";
} else {
// Nom
sb.append("<input type='text' name='cat|").append(techId).append("|name' value=\"").append(displayName).append("\" class='input-h2'>");
// Couleur de Fond (on utilise cat.getBgColor())
sb.append("<div style='display:inline-block; vertical-align:middle; margin-right:15px;'>");
sb.append("<div style='font-weight: bold; font-size: 0.7em; padding-bottom: 5px;'>Fond</div>");
sb.append("<input type='color' name='cat|").append(techId).append("|bgColor' value='").append(cat.getBgColor()).append("' style='width:30px; height:25px; border:none; cursor:pointer;'>");
sb.append("</div>");
// Couleur de Texte (on utilise cat.getTextColor())
sb.append("<div style='display:inline-block; vertical-align:middle; margin-right:15px;'>");
sb.append("<div style='font-weight: bold; font-size: 0.7em; padding-bottom: 5px;'>Texte</div>");
sb.append("<input type='color' name='cat|").append(techId).append("|textColor' value='").append(cat.getTextColor()).append("' style='width:30px; height:25px; border:none; cursor:pointer;'>");
sb.append("</div>");
// Bouton supprimer
sb.append(" <button type='button' class='btn-del' style='vertical-align: middle; margin-left: 10px;' ")
.append("onclick=\"if(confirm('Supprimer cette catégorie?')) this.closest('.card').remove()\">×</button>");
}
sb.append("</h2>");
// Build table
sb.append("<table id='table-").append(techId).append("'>");
sb.append("<tr><th>Image</th><th>Nom</th><th>Prix Min</th><th>Prix Max</th></tr>");
int rowIdx = 0;
for (Item item : items) {
sb.append(renderItemRow(techId, item, rowIdx++));
}
sb.append("</table>");
// Button to add item in this category
sb.append("<button type='button' class='btn-add' onclick=\"addRow('").append(techId).append("')\">+ Ajouter article</button>");
sb.append("</div>");
return sb.toString();
}
/**
* Generate HTML row for each item
*/
private String renderItemRow(String techId, Item item, int idx) {
String prefix = "item|" + techId + "|" + idx;
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");
}
}
/**
* Receive JSON and update cache and storage catalog
*/
private Response handleSaveEditor(IHTTPSession session) {
try {
// NanoHTTPD read request
Map<String, String> files = new HashMap<>();
session.parseBody(files);
String jsonStr = files.get("postData");
if (jsonStr == null || jsonStr.isEmpty()) {
return newFixedLengthResponse(Response.Status.BAD_REQUEST, MIME_PLAINTEXT, "Données vides");
}
JSONObject fullJson = new JSONObject(jsonStr);
// Get items
JSONObject allFields = fullJson.getJSONObject("items");
// Get categories order
String[] orderedIds = fullJson.getString("cat_order_list").split(",");
List<Category> finalData = new ArrayList<>();
// For each category
for (String techId : orderedIds) {
String catNameKey = "cat|" + techId + "|name";
// Does it exist
if (!allFields.has(catNameKey)) continue;
String name = allFields.getString(catNameKey);
List<Item> itemsInCategory = new ArrayList<>();
// Browse and create items
int i = 0;
while (true) {
String itemBase = "item|" + techId + "|" + i + "|";
// 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
imgVal,
parseSafely(allFields.optString(itemBase + "min", "0")),
parseSafely(allFields.optString(itemBase + "max", "0"))
));
i++;
}
// 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);
return newFixedLengthResponse(Response.Status.OK, MIME_PLAINTEXT, "OK");
} catch (Exception e) {
Log.e("ControlServer", "Erreur save", e);
return newFixedLengthResponse(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "Erreur");
}
}
private int parseSafely(String val) {
if (val == null) return 0;
try {
return Integer.parseInt(val.trim());
} catch (Exception e) {
return 0;
}
}
// Soon deprecated with online editor
/** /**
* Manage JSON sent by remote. * Manage JSON sent by remote.
*/ */
private Response handleJsonUpload(IHTTPSession session) { private Response handleJsonUpload(IHTTPSession session) {
Map<String, String> tmpFiles = new HashMap<>(); Map<String, String> tmpFiles = new HashMap<>();
try { try {
// NanoHTTPD stores loaded file in temp file
session.parseBody(tmpFiles); 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"); String tmpPath = tmpFiles.get("json_file");
if (tmpPath == null) return newFixedLengthResponse("❌ Aucun fichier reçu."); if (tmpPath == null) return newFixedLengthResponse("❌ Aucun fichier reçu.");
@@ -148,6 +342,7 @@ public class ControlServer extends NanoHTTPD {
} }
} }
// Soon deprecated with online editor
/** /**
* Manage multiple images sent by remote. * Manage multiple images sent by remote.
*/ */
@@ -163,17 +358,17 @@ public class ControlServer extends NanoHTTPD {
int count = 0; 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()) { 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")) { if (entry.getKey().startsWith("images")) {
String tmpPath = entry.getValue(); 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()); List<String> params = session.getParameters().get(entry.getKey());
if (params != null && !params.isEmpty()) { 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(); String originalName = new File(params.get(0)).getName();
File src = new File(tmpPath); File src = new File(tmpPath);
@@ -193,6 +388,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 * Prepare file and offer as download
*/ */
@@ -236,19 +447,4 @@ public class ControlServer extends NanoHTTPD {
return newFixedLengthResponse("❌ Erreur de lecture."); 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);
}
}
}
} }

View File

@@ -4,11 +4,16 @@ package com.stock.pignon;
import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.LinkedHashMap;
public class DataLoader { public class DataLoader {
private static final String TAG = "DataLoader"; // For readable logs private static final String TAG = "DataLoader"; // For readable logs
@@ -20,6 +25,9 @@ public class DataLoader {
private static List<Item> cachedGlobals = new ArrayList<>(); private static List<Item> cachedGlobals = new ArrayList<>();
/**
* Get data from input JSON PIECES_FILE
*/
public static void loadData() { public static void loadData() {
File dir = new File(Environment.getExternalStorageDirectory(), EXTERNAL_DIR); File dir = new File(Environment.getExternalStorageDirectory(), EXTERNAL_DIR);
File jsonFile = new File(dir, PIECES_FILE); 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) { public static List<Item> getItemsForCategory(String categoryName) {
// Add global items // Add global items
@@ -72,13 +83,68 @@ public class DataLoader {
return combinedList; 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() { public static List<Category> getCategories() {
return cachedCategories; return cachedCategories;
} }
public static List<Item> getGlobalItems() {
return cachedGlobals;
}
// Internal class for Gson /**
* Internal class for GSON
*/
private static class CategoriesWrapper { private static class CategoriesWrapper {
List<Item> globalItems; List<Item> globalItems;
List<Category> categories; List<Category> categories;
} }
/**
* Write JSON from online editor data
*/
public static void saveData(List<Category> categoriesList) throws Exception {
CategoriesWrapper wrapper = new CategoriesWrapper();
wrapper.categories = new ArrayList<>();
wrapper.globalItems = new ArrayList<>();
// Browse category
for (Category cat : categoriesList) {
if ("global".equals(cat.getName())) {
wrapper.globalItems = cat.getItems();
} else {
wrapper.categories.add(cat);
}
}
// Create JSON
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String jsonString = gson.toJson(wrapper);
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);
}
// Update app cache
cachedGlobals = wrapper.globalItems;
cachedCategories = wrapper.categories;
}
} }

View File

@@ -14,9 +14,24 @@ public class Item {
// Empty constructor for GSON // Empty constructor for GSON
public Item() {} 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 String getName() { return name; }
public int getMinPrice() { return minPrice; } public int getMinPrice() { return minPrice; }
public int getMaxPrice() { return maxPrice; } public int getMaxPrice() { return maxPrice; }
public String getImage() { return image; } 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; }
} }

View File

@@ -12,6 +12,7 @@ import android.widget.Button;
import android.widget.GridLayout; import android.widget.GridLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
import android.os.Environment; import android.os.Environment;
import java.io.File; import java.io.File;
@@ -35,7 +36,7 @@ public class MainActivity extends AppCompatActivity {
// UI components // UI components
private LinearLayout cartList; private LinearLayout cartList;
private LinearLayout homeLayout; private LinearLayout homeLayout;
private LinearLayout categoryItemsLayout; private FrameLayout categoryItemsLayout;
private LinearLayout categoriesLayout; private LinearLayout categoriesLayout;
private GridLayout gridPieces; private GridLayout gridPieces;
private ImageView mainImage; private ImageView mainImage;
@@ -88,12 +89,12 @@ public class MainActivity extends AppCompatActivity {
} }
// Launch server // Launch server
server = new ControlServer(8080); server = new ControlServer(this,8080);
try { try {
server.start(); server.start();
String url = "http://" + getDeviceIP() + ":8080"; String url = "http://" + getDeviceIP() + ":8080";
// On met l'URL directement dans le sous-titre de l'ActionBar // Print URL in action bar for user
if (getSupportActionBar() != null) { if (getSupportActionBar() != null) {
getSupportActionBar().setSubtitle("Serveur en ligne : " + url + " - " + versionName); getSupportActionBar().setSubtitle("Serveur en ligne : " + url + " - " + versionName);
} }
@@ -103,6 +104,24 @@ public class MainActivity extends AppCompatActivity {
getSupportActionBar().setSubtitle("Erreur serveur" + " - " + versionName); getSupportActionBar().setSubtitle("Erreur serveur" + " - " + versionName);
} }
} }
// Back button (blue)
Button btnBack = findViewById(R.id.btnBackFromCategory);
if (btnBack != null) {
btnBack.setOnClickListener(v -> showHome());
}
// Empty button (red)
Button btnClear = findViewById(R.id.clearCartBtn);
if (btnClear != null) {
btnClear.setOnClickListener(v -> CartActionHelper.emptyCart(cartList, this));
}
// Validate button (green)
Button btnValidate = findViewById(R.id.validateCartBtn);
if (btnValidate != null) {
btnValidate.setOnClickListener(v -> CartActionHelper.validateCart(cartList, this));
}
} }
@Override @Override
@@ -194,8 +213,6 @@ public class MainActivity extends AppCompatActivity {
homeLayout.setVisibility(View.GONE); homeLayout.setVisibility(View.GONE);
categoryItemsLayout.setVisibility(View.VISIBLE); categoryItemsLayout.setVisibility(View.VISIBLE);
updateActionButton(false);
gridPieces.removeAllViews(); gridPieces.removeAllViews();
// Calculate item width for a 4-column grid // Calculate item width for a 4-column grid
@@ -265,8 +282,6 @@ public class MainActivity extends AppCompatActivity {
public void showHome() { public void showHome() {
categoryItemsLayout.setVisibility(View.GONE); categoryItemsLayout.setVisibility(View.GONE);
homeLayout.setVisibility(View.VISIBLE); homeLayout.setVisibility(View.VISIBLE);
updateActionButton(true);
} }
private void loadMainImage() { private void loadMainImage() {
@@ -274,31 +289,6 @@ public class MainActivity extends AppCompatActivity {
ImageLoader.loadImage(mainImage, "_velo", 800,800); ImageLoader.loadImage(mainImage, "_velo", 800,800);
} }
public void emptyCart(View view) {
CartActionHelper.emptyCart(cartList, this);
}
public void validateCart(View view) {
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) { private int dpToPx(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
} }

View File

@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- activity_main.xml --> <!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" android:orientation="horizontal"
android:background="#ffffff"> android:background="#ffffff"
android:baselineAligned="false"> <FrameLayout
<FrameLayout
android:id="@+id/contentFrame" android:id="@+id/contentFrame"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -17,9 +18,9 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" android:orientation="horizontal"
android:paddingLeft="20dp"> android:paddingStart="20dp"
android:paddingEnd="20dp"
<LinearLayout tools:ignore="RtlSymmetry"> <LinearLayout
android:id="@+id/categoriesLayout" android:id="@+id/categoriesLayout"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -31,27 +32,49 @@
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:scaleType="fitCenter" /> android:scaleType="fitCenter"
</LinearLayout> android:contentDescription="@string/app_name" /> </LinearLayout>
<LinearLayout <FrameLayout
android:id="@+id/categoryItemsLayout" android:id="@+id/categoryItemsLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:visibility="gone">
android:visibility="gone"
android:padding="10dp">
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout <GridLayout
android:id="@+id/gridPieces" android:id="@+id/gridPieces"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:columnCount="4" /> android:columnCount="4" />
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout> </LinearLayout>
</ScrollView>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnBackFromCategory"
android:layout_width="200dp"
android:layout_height="70dp"
android:layout_gravity="bottom|start"
android:layout_margin="20dp"
android:text="@string/cart_back_btn"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
app:backgroundTint="#1E88E5"
android:elevation="8dp"
tools:targetApi="l" />
</FrameLayout>
</FrameLayout> </FrameLayout>
<include layout="@layout/partial_cart" /> <include layout="@layout/partial_cart" />

View File

@@ -19,13 +19,15 @@
<TextView <TextView
android:id="@+id/itemName" android:id="@+id/itemName"
android:layout_width="wrap_content" android:layout_width="150dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:text="Item name" tools:text="Item name"
android:textSize="20sp" android:textSize="20sp"
android:layout_marginTop="0dp" android:layout_marginTop="0dp"
android:lines="1" android:minLines="2"
android:ellipsize="end" /> android:maxLines="2"
android:ellipsize="end"
android:gravity="center"/>
<TextView <TextView
android:id="@+id/itemPrice" android:id="@+id/itemPrice"

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- partial_panier.xml --> <!-- partial_panier.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cartContainer" android:id="@+id/cartContainer"
android:layout_width="350dp" android:layout_width="350dp"
android:layout_height="match_parent" android:layout_height="match_parent"
@@ -56,29 +57,29 @@
android:orientation="horizontal" android:orientation="horizontal"
android:weightSum="2"> android:weightSum="2">
<Button <androidx.appcompat.widget.AppCompatButton
android:id="@+id/clearCartBtn" android:id="@+id/clearCartBtn"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="60dp" android:layout_height="70dp"
android:text="@string/cart_empty_btn" android:text="@string/cart_empty_btn"
android:textSize="18sp" android:textSize="18sp"
android:textStyle="bold"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
android:background="#E53935" app:backgroundTint="#E53935"
android:layout_marginEnd="5dp" android:layout_marginEnd="5dp"/>
android:onClick="emptyCart"/>
<Button <androidx.appcompat.widget.AppCompatButton
android:id="@+id/validateCartBtn" android:id="@+id/validateCartBtn"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="1" android:layout_weight="1"
android:layout_height="60dp" android:layout_height="70dp"
android:text="@string/cart_validate_btn" android:text="@string/cart_validate_btn"
android:textSize="18sp" android:textSize="18sp"
android:textStyle="bold"
android:textColor="#FFFFFF" android:textColor="#FFFFFF"
android:background="#43A047" app:backgroundTint="#43A047"
android:layout_marginStart="5dp" android:layout_marginStart="5dp"/>
android:onClick="validateCart"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>