add detail page
This commit is contained in:
129
lib/controllers/detail_controller.dart
Normal file
129
lib/controllers/detail_controller.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../models/filament_model.dart';
|
||||
import '../services/appwrite_service.dart';
|
||||
import '../widgets/add_filament_dialog.dart';
|
||||
|
||||
class DetailController extends GetxController {
|
||||
final appwriteService = AppwriteService();
|
||||
|
||||
/// Das aktuell angezeigte Filament – wird beim Öffnen der Seite gesetzt.
|
||||
final filament = Rx<FilamentModel>(FilamentModel.empty);
|
||||
final isLoading = false.obs;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
// Filament kommt via Get.arguments von der HomeView
|
||||
if (Get.arguments != null && Get.arguments is FilamentModel) {
|
||||
filament.value = Get.arguments as FilamentModel;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onReady() {}
|
||||
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
double get remaining => (filament.value.weight - filament.value.weightUsed)
|
||||
.clamp(0, filament.value.weight);
|
||||
|
||||
double get progress =>
|
||||
filament.value.weight > 0 ? remaining / filament.value.weight : 0;
|
||||
|
||||
/// Öffnet den Bearbeitungs-Dialog und aktualisiert lokal + in Appwrite.
|
||||
Future<void> openEditDialog() async {
|
||||
final updated = await AddFilamentDialog.showEdit(entry: filament.value);
|
||||
if (updated == null) return;
|
||||
|
||||
isLoading.value = true;
|
||||
try {
|
||||
final success = await appwriteService.updateFilamentDocument(
|
||||
updated.documentId,
|
||||
FilamentModel.toMapForAppwrite(updated),
|
||||
);
|
||||
if (success) filament.value = updated; // bleibt auf Detail-Seite
|
||||
} catch (e) {
|
||||
print('Fehler beim Aktualisieren: $e');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Löscht das Filament und navigiert zurück.
|
||||
Future<void> deleteFilament() async {
|
||||
final confirm = await Get.dialog<bool>(
|
||||
AlertDialog(
|
||||
backgroundColor: const Color(0xFF1A2035),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||
title: const Text(
|
||||
'Filament löschen?',
|
||||
style: TextStyle(color: Colors.white),
|
||||
),
|
||||
content: Text(
|
||||
'${filament.value.name} wird dauerhaft gelöscht.',
|
||||
style: const TextStyle(color: Colors.white70),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Get.back(result: false),
|
||||
child: const Text(
|
||||
'Abbrechen',
|
||||
style: TextStyle(color: Colors.white60),
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Get.back(result: true),
|
||||
child: const Text(
|
||||
'Löschen',
|
||||
style: TextStyle(color: Color(0xFFFF6B6B)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirm != true) return;
|
||||
|
||||
isLoading.value = true;
|
||||
try {
|
||||
await appwriteService.deleteFilamentDocument(filament.value.documentId);
|
||||
} catch (e) {
|
||||
print('Fehler beim Löschen: $e');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
Get.back(result: 'deleted');
|
||||
}
|
||||
|
||||
/// Aktualisiert nur das verbrauchte Gewicht.
|
||||
Future<void> updateWeightUsed(double newWeightUsed) async {
|
||||
final updated = FilamentModel(
|
||||
documentId: filament.value.documentId,
|
||||
name: filament.value.name,
|
||||
type: filament.value.type,
|
||||
color: filament.value.color,
|
||||
weight: filament.value.weight,
|
||||
weightUsed: newWeightUsed,
|
||||
price: filament.value.price,
|
||||
manufacturer: filament.value.manufacturer,
|
||||
purchaseDate: filament.value.purchaseDate,
|
||||
notes: filament.value.notes,
|
||||
printingTemp: filament.value.printingTemp,
|
||||
bedTemp: filament.value.bedTemp,
|
||||
);
|
||||
isLoading.value = true;
|
||||
try {
|
||||
final success = await appwriteService.updateFilamentDocument(
|
||||
updated.documentId,
|
||||
FilamentModel.toMapForAppwrite(updated),
|
||||
);
|
||||
if (success) filament.value = updated;
|
||||
} catch (e) {
|
||||
print('Fehler beim Aktualisieren des Gewichts: $e');
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../models/home_model.dart';
|
||||
import '../models/filament_model.dart';
|
||||
import '../services/appwrite_service.dart';
|
||||
import '../widgets/add_weight_dialog.dart';
|
||||
import '../widgets/add_filament_dialog.dart';
|
||||
|
||||
class HomeController extends GetxController {
|
||||
final isloading = false.obs;
|
||||
final List<WeightModel> weights = <WeightModel>[].obs;
|
||||
final filaments = <FilamentModel>[].obs;
|
||||
final appwriteService = AppwriteService();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
_loadDataList();
|
||||
_loadFilaments();
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
@@ -21,85 +21,73 @@ class HomeController extends GetxController {
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
void _loadDataList() async {
|
||||
void _loadFilaments() async {
|
||||
isloading.value = true;
|
||||
if (weights.isNotEmpty) {
|
||||
weights.clear();
|
||||
}
|
||||
final loggedIn = await appwriteService.login();
|
||||
if (!loggedIn) {
|
||||
print('Login fehlgeschlagen – Daten können nicht geladen werden.');
|
||||
filaments.clear();
|
||||
try {
|
||||
final loggedIn = await appwriteService.login();
|
||||
if (!loggedIn) {
|
||||
print('Login fehlgeschlagen – Daten können nicht geladen werden.');
|
||||
return;
|
||||
}
|
||||
final documents = await appwriteService.getFilamentDocuments();
|
||||
if (documents.isEmpty) {
|
||||
print('Keine Filamente gefunden.');
|
||||
} else {
|
||||
print('${documents.length} Filamente geladen.');
|
||||
filaments.assignAll(
|
||||
documents.map(
|
||||
(doc) => FilamentModel.fromJson({'\$id': doc.$id, ...doc.data}),
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (e, st) {
|
||||
print('Fehler beim Laden der Filamente: $e\n$st');
|
||||
} finally {
|
||||
isloading.value = false;
|
||||
return;
|
||||
}
|
||||
final documents = await appwriteService.getDocumentsFromCollection();
|
||||
if (documents.isEmpty) {
|
||||
print('Keine Dokumente gefunden.');
|
||||
} else {
|
||||
print('${documents.length} Einträge geladen.');
|
||||
weights.assignAll(documents.map((doc) => WeightModel.fromJson(doc.data)));
|
||||
}
|
||||
isloading.value = false;
|
||||
update();
|
||||
}
|
||||
|
||||
void addWeightEntry(WeightModel entry) {
|
||||
weights.add(entry);
|
||||
var map = WeightModel.toMapForAppwrite(entry);
|
||||
appwriteService.createDocumentInCollection(map);
|
||||
update();
|
||||
}
|
||||
|
||||
void editWeightEntry(WeightModel updated) {
|
||||
final idx = weights.indexWhere(
|
||||
(w) => w.documentId == updated.documentId && w.date == updated.date,
|
||||
);
|
||||
if (idx != -1) {
|
||||
weights[idx] = updated;
|
||||
var map = WeightModel.toMapForAppwrite(updated);
|
||||
appwriteService.updateDocumentInCollection(updated.documentId, map);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
/// Gewicht des Eintrags unmittelbar VOR [date] einer Person.
|
||||
/// Wird für die weightChange-Berechnung beim Bearbeiten genutzt.
|
||||
double getPreviousWeight(String personName, DateTime date) {
|
||||
final before = weights
|
||||
.where((w) => w.name == personName && w.date.isBefore(date))
|
||||
.toList();
|
||||
if (before.isEmpty) return 0.0;
|
||||
before.sort((a, b) => b.date.compareTo(a.date));
|
||||
return before.first.weight;
|
||||
}
|
||||
|
||||
/// Gibt das zuletzt eingetragene Gewicht einer Person zurück.
|
||||
/// 0.0 wenn noch kein Eintrag existiert (wird als "erster Eintrag" behandelt).
|
||||
double getLastWeight(String personName) {
|
||||
final personEntries = weights.where((w) => w.name == personName).toList();
|
||||
if (personEntries.isEmpty) return 0.0;
|
||||
personEntries.sort((a, b) => b.date.compareTo(a.date));
|
||||
return personEntries.first.weight;
|
||||
}
|
||||
|
||||
void addWeight(String personName) {}
|
||||
|
||||
// ── Dialog-Logik ─────────────────────────────────────────────────────────
|
||||
|
||||
Future<void> openAddDialog(String personName, String userId) async {
|
||||
final entry = await AddWeightDialog.show(
|
||||
userId: userId,
|
||||
personName: personName,
|
||||
lastWeight: getLastWeight(personName),
|
||||
void addFilament(FilamentModel entry) {
|
||||
filaments.add(entry);
|
||||
appwriteService.createFilamentDocument(
|
||||
FilamentModel.toMapForAppwrite(entry),
|
||||
);
|
||||
if (entry != null) addWeightEntry(entry);
|
||||
update();
|
||||
}
|
||||
|
||||
Future<void> openEditDialog(WeightModel existing) async {
|
||||
final updated = await AddWeightDialog.showEdit(
|
||||
entry: existing,
|
||||
previousWeight: getPreviousWeight(existing.name, existing.date),
|
||||
);
|
||||
if (updated != null) editWeightEntry(updated);
|
||||
void editFilament(FilamentModel updated) {
|
||||
final idx = filaments.indexWhere((f) => f.documentId == updated.documentId);
|
||||
if (idx != -1) {
|
||||
filaments[idx] = updated;
|
||||
appwriteService.updateFilamentDocument(
|
||||
updated.documentId,
|
||||
FilamentModel.toMapForAppwrite(updated),
|
||||
);
|
||||
update();
|
||||
}
|
||||
}
|
||||
|
||||
void deleteFilament(FilamentModel filament) {
|
||||
filaments.remove(filament);
|
||||
appwriteService.deleteFilamentDocument(filament.documentId);
|
||||
update();
|
||||
}
|
||||
|
||||
/// Nur lokal löschen (Appwrite wurde bereits vom DetailController aufgerufen).
|
||||
void deleteFilamentLocal(FilamentModel filament) {
|
||||
filaments.removeWhere((f) => f.documentId == filament.documentId);
|
||||
update();
|
||||
}
|
||||
|
||||
Future<void> openAddDialog() async {
|
||||
final entry = await AddFilamentDialog.show();
|
||||
if (entry != null) addFilament(entry);
|
||||
}
|
||||
|
||||
Future<void> openEditDialog(FilamentModel existing) async {
|
||||
final updated = await AddFilamentDialog.showEdit(entry: existing);
|
||||
if (updated != null) editFilament(updated);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user