add two views
This commit is contained in:
@@ -3,17 +3,40 @@ import 'package:get/get.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../models/kauf_filament_model.dart';
|
||||
import '../models/bauteil_model.dart';
|
||||
import '../helper/storage_keys.dart';
|
||||
import '../helper/filament_bestand.dart';
|
||||
import '../helper/display_formats.dart';
|
||||
import '../helper/input_validators.dart';
|
||||
import '../services/get_storage_services.dart';
|
||||
|
||||
class HomeController extends GetxController {
|
||||
static const storageKey = 'kauf_filamente';
|
||||
static const storageKey = StorageKeys.filamente;
|
||||
|
||||
final storage = Get.find<GetStorageService>();
|
||||
final uuid = const Uuid();
|
||||
|
||||
final filamente = <KaufFilamentModel>[].obs;
|
||||
final verbrauch = <String, double>{}.obs;
|
||||
|
||||
void loadVerbrauch() {
|
||||
verbrauch.assignAll(
|
||||
FilamentBestand.verbrauch(
|
||||
storage.getModelList(StorageKeys.bauteile).map(BauteilModel.fromJson),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
double? restgewicht(KaufFilamentModel filament) {
|
||||
final gewicht = filament.gewichtGramm;
|
||||
return gewicht == null
|
||||
? null
|
||||
: gewicht - (verbrauch[filament.filamentId] ?? 0);
|
||||
}
|
||||
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final pageScrollController = ScrollController();
|
||||
final formScrollController = ScrollController();
|
||||
|
||||
final haendlerController = TextEditingController();
|
||||
final farbeController = TextEditingController();
|
||||
@@ -46,19 +69,16 @@ class HomeController extends GetxController {
|
||||
}
|
||||
|
||||
void loadFilamente() {
|
||||
loadVerbrauch();
|
||||
final data = storage.getModelList(storageKey);
|
||||
|
||||
filamente.assignAll(
|
||||
data.map(KaufFilamentModel.fromJson),
|
||||
);
|
||||
filamente.assignAll(data.map(KaufFilamentModel.fromJson));
|
||||
|
||||
sortFilamente();
|
||||
}
|
||||
|
||||
void sortFilamente() {
|
||||
filamente.sort(
|
||||
(a, b) => b.kaufdatum.compareTo(a.kaufdatum),
|
||||
);
|
||||
filamente.sort((a, b) => b.kaufdatum.compareTo(a.kaufdatum));
|
||||
}
|
||||
|
||||
Future<void> persist() async {
|
||||
@@ -80,6 +100,22 @@ class HomeController extends GetxController {
|
||||
return;
|
||||
}
|
||||
|
||||
loadVerbrauch();
|
||||
final gewicht = InputValidators.number(gewichtController.text);
|
||||
final bereitsVerbraucht = editingId.value == null
|
||||
? 0
|
||||
: (verbrauch[editingId.value!] ?? 0);
|
||||
if (gewicht == null ||
|
||||
!gewicht.isFinite ||
|
||||
gewicht <= 0 ||
|
||||
gewicht < bereitsVerbraucht) {
|
||||
Get.snackbar(
|
||||
'Ungültiges Gewicht',
|
||||
'Das Startgewicht muss positiv sein und mindestens dem bereits gebuchten Verbrauch entsprechen.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
final filament = KaufFilamentModel(
|
||||
filamentId: editingId.value ?? uuid.v4(),
|
||||
marke: markeController.text.trim(),
|
||||
@@ -88,9 +124,7 @@ class HomeController extends GetxController {
|
||||
material: material.value,
|
||||
preis: preis,
|
||||
farbe: farbeController.text.trim(),
|
||||
gewicht: gewichtController.text.trim().isEmpty
|
||||
? '1kg'
|
||||
: gewichtController.text.trim(),
|
||||
gewicht: gewicht.toString(),
|
||||
);
|
||||
|
||||
if (editingId.value == null) {
|
||||
@@ -113,7 +147,7 @@ class HomeController extends GetxController {
|
||||
resetForm();
|
||||
}
|
||||
|
||||
void editFilament(KaufFilamentModel filament) {
|
||||
Future<void> editFilament(KaufFilamentModel filament) async {
|
||||
editingId.value = filament.filamentId;
|
||||
kaufdatum.value = filament.kaufdatum;
|
||||
markeController.text = filament.marke;
|
||||
@@ -121,15 +155,24 @@ class HomeController extends GetxController {
|
||||
material.value = filament.material;
|
||||
preisController.text = filament.preis.toStringAsFixed(2);
|
||||
farbeController.text = filament.farbe;
|
||||
gewichtController.text = filament.gewicht;
|
||||
gewichtController.text = filament.gewichtGramm == null
|
||||
? filament.gewicht
|
||||
: DisplayFormats.number(filament.gewichtGramm!);
|
||||
|
||||
final scroll = formScrollController.hasClients
|
||||
? formScrollController
|
||||
: pageScrollController;
|
||||
if (scroll.hasClients) {
|
||||
await scroll.animateTo(
|
||||
0,
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeOutCubic,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> duplicateFilament(
|
||||
KaufFilamentModel filament,
|
||||
) async {
|
||||
filamente.add(
|
||||
filament.copyWith(filamentId: uuid.v4()),
|
||||
);
|
||||
Future<void> duplicateFilament(KaufFilamentModel filament) async {
|
||||
filamente.add(filament.copyWith(filamentId: uuid.v4()));
|
||||
|
||||
sortFilamente();
|
||||
await persist();
|
||||
@@ -151,11 +194,12 @@ class HomeController extends GetxController {
|
||||
material.value = 'PLA';
|
||||
gewichtController.clear();
|
||||
formKey.currentState?.reset();
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
pageScrollController.dispose();
|
||||
formScrollController.dispose();
|
||||
haendlerController.dispose();
|
||||
markeController.dispose();
|
||||
farbeController.dispose();
|
||||
@@ -163,4 +207,4 @@ class HomeController extends GetxController {
|
||||
gewichtController.dispose();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user