add service for appwrite
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import '../models/home_model.dart';
|
||||
import '../services/appwrite_service.dart';
|
||||
import '../widgets/add_weight_dialog.dart';
|
||||
|
||||
class HomeController extends GetxController {
|
||||
final isLoggedIn = false.obs;
|
||||
final isloading = false.obs;
|
||||
final List<WeightModel> weights = <WeightModel>[].obs;
|
||||
final appwriteService = AppwriteService();
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -19,26 +22,48 @@ class HomeController extends GetxController {
|
||||
@override
|
||||
void onClose() {}
|
||||
|
||||
void _loadDataList() {
|
||||
void _loadDataList() async {
|
||||
isloading.value = true;
|
||||
if (weights.isNotEmpty) {
|
||||
weights.clear();
|
||||
}
|
||||
// Simulate loading data from a database or API
|
||||
weights.assignAll(WeightModel.sampleData);
|
||||
isLoggedIn.value = await appwriteService.login();
|
||||
if (isLoggedIn.value) {
|
||||
final documents = await appwriteService.getDocumentsFromCollection();
|
||||
if (documents.isEmpty) {
|
||||
print(
|
||||
'Keine Dokumente gefunden. Stelle sicher, dass die Collection Einträge enthält.',
|
||||
);
|
||||
} else {
|
||||
print(
|
||||
'Dokumente erfolgreich geladen: ${documents.length} Einträge gefunden.',
|
||||
);
|
||||
weights.assignAll(
|
||||
documents.map((doc) => WeightModel.fromJson(doc.data)),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
print('Fehler beim Einloggen. Keine Daten geladen.');
|
||||
}
|
||||
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.id == updated.id);
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -67,8 +92,9 @@ class HomeController extends GetxController {
|
||||
|
||||
// ── Dialog-Logik ─────────────────────────────────────────────────────────
|
||||
|
||||
Future<void> openAddDialog(String personName) async {
|
||||
Future<void> openAddDialog(String personName, String userId) async {
|
||||
final entry = await AddWeightDialog.show(
|
||||
userId: userId,
|
||||
personName: personName,
|
||||
lastWeight: getLastWeight(personName),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user