add import export and items

This commit is contained in:
atseirjo
2026-10-28 08:10:55 +01:00
parent 07a1bb02fd
commit 4a1731f986
22 changed files with 1118 additions and 106 deletions
+72 -5
View File
@@ -43,17 +43,17 @@ class HomeController extends GetxController {
final preisController = TextEditingController();
final markeController = TextEditingController();
final gewichtController = TextEditingController();
final materialController = TextEditingController(text: 'PLA');
final kaufdatum = DateTime.now().obs;
final material = 'PLA'.obs;
final editingId = RxnString();
final materialien = [
static const defaultMaterialien = [
'PLA',
'PLA+',
'PLA light',
'PLA-CF',
'PLA+',
'PETG',
'PETG-CF',
'ABS',
@@ -61,11 +61,74 @@ class HomeController extends GetxController {
'TPU',
'Sonstiges',
];
final materialien = defaultMaterialien.toList().obs;
final materialMessage = RxnString();
final isSavingMaterial = false.obs;
@override
void onInit() {
super.onInit();
loadFilamente();
loadMaterialien();
selectMaterial(materialien.firstOrNull ?? '');
}
void loadMaterialien() {
final saved = storage.getModelList(StorageKeys.materialien);
if (saved.isNotEmpty) {
materialien.assignAll((saved.single['werte'] as List).cast<String>());
} else {
materialien.assignAll({
...defaultMaterialien,
...filamente.map((filament) => filament.material),
});
}
}
void selectMaterial(String value) {
material.value = value;
materialController.text = value;
}
Future<bool> addMaterial() async {
final value = materialController.text.trim();
if (value.isEmpty) {
materialMessage.value = 'Bitte eine Materialart eingeben.';
return false;
}
final existing = materialien.firstWhereOrNull(
(item) => item.toLowerCase() == value.toLowerCase(),
);
if (existing != null) {
materialMessage.value = null;
selectMaterial(existing);
return true;
}
final saved = await _saveMaterialien([...materialien, value]);
if (saved) selectMaterial(value);
return saved;
}
Future<bool> removeMaterial(String value) =>
_saveMaterialien(materialien.where((item) => item != value).toList());
Future<bool> _saveMaterialien(List<String> values) async {
if (isSavingMaterial.value) return false;
isSavingMaterial.value = true;
materialMessage.value = null;
try {
await storage.saveModelList(StorageKeys.materialien, [
{'werte': values},
]);
materialien.assignAll(values);
return true;
} catch (_) {
materialMessage.value =
'Artenliste konnte nicht gespeichert werden. Bitte erneut versuchen.';
return false;
} finally {
isSavingMaterial.value = false;
}
}
void loadFilamente() {
@@ -116,6 +179,8 @@ class HomeController extends GetxController {
return;
}
if (!await addMaterial()) return;
material.value = materialController.text.trim();
final filament = KaufFilamentModel(
filamentId: editingId.value ?? uuid.v4(),
marke: markeController.text.trim(),
@@ -152,7 +217,7 @@ class HomeController extends GetxController {
kaufdatum.value = filament.kaufdatum;
markeController.text = filament.marke;
haendlerController.text = filament.haendler;
material.value = filament.material;
selectMaterial(filament.material);
preisController.text = filament.preis.toStringAsFixed(2);
farbeController.text = filament.farbe;
gewichtController.text = filament.gewichtGramm == null
@@ -185,15 +250,16 @@ class HomeController extends GetxController {
}
void resetForm() {
formKey.currentState?.reset();
editingId.value = null;
haendlerController.clear();
markeController.clear();
farbeController.clear();
preisController.clear();
kaufdatum.value = DateTime.now();
material.value = 'PLA';
selectMaterial(materialien.firstOrNull ?? '');
materialMessage.value = null;
gewichtController.clear();
formKey.currentState?.reset();
}
@override
@@ -205,6 +271,7 @@ class HomeController extends GetxController {
farbeController.dispose();
preisController.dispose();
gewichtController.dispose();
materialController.dispose();
super.onClose();
}
}