import 'dart:io'; import 'package:flutter/foundation.dart' show kIsWeb; import 'package:get/get.dart'; import 'package:file_picker/file_picker.dart'; import 'package:path_provider/path_provider.dart'; import 'package:share_plus/share_plus.dart'; import 'dart:convert'; import '../helpers/filament_repository.dart'; import '../pages/list_view.dart'; import '../helpers/web_download_helper_stub.dart' if (dart.library.html) '../helpers/web_download_helper.dart'; class HomeController extends GetxController { void loadFilaments() { // Navigiere zur Listenseite Get.toNamed(ListPage.namedRoute); } Future downloadFilaments() async { try { // JSON-Daten vom Repository holen final jsonData = FilamentRepository.to.exportToJson(); if (kIsWeb) { // Web: Download direkt im Browser final fileName = 'filament_backup_${DateTime.now().millisecondsSinceEpoch}.json'; downloadJsonFileWeb(jsonData, fileName); } else { // Mobile/Desktop: Datei speichern und teilen await _downloadForMobile(jsonData); } Get.snackbar( 'Erfolg', 'Backup wurde erstellt', snackPosition: SnackPosition.BOTTOM, backgroundColor: Get.theme.colorScheme.primary.withAlpha(26), colorText: Get.theme.colorScheme.primary, duration: Duration(seconds: 3), ); } catch (e) { Get.snackbar( 'Fehler', 'Backup konnte nicht erstellt werden: $e', snackPosition: SnackPosition.BOTTOM, backgroundColor: Get.theme.colorScheme.error.withAlpha(26), colorText: Get.theme.colorScheme.error, ); } } Future _downloadForMobile(String jsonData) async { // Temporäre Datei erstellen final directory = await getTemporaryDirectory(); final fileName = 'filament_backup_${DateTime.now().millisecondsSinceEpoch}.json'; final file = File('${directory.path}/$fileName'); await file.writeAsString(jsonData); // Datei über Share-Dialog teilen await Share.shareXFiles([XFile(file.path)], text: 'Filament Backup'); } Future restoreFilaments() async { try { // Datei auswählen FilePickerResult? result = await FilePicker.platform.pickFiles( type: FileType.any, ); if (result != null) { String? jsonData; if (kIsWeb) { // Web: Bytes direkt lesen final bytes = result.files.first.bytes; if (bytes != null) { jsonData = utf8.decode(bytes); } } else { // Mobile/Desktop: Datei von Pfad lesen final path = result.files.first.path; if (path != null) { final file = File(path); jsonData = await file.readAsString(); } } if (jsonData != null) { // Daten importieren final success = await FilamentRepository.to.importFromJson(jsonData); if (success) { Get.snackbar( 'Erfolg', 'Backup wurde erfolgreich wiederhergestellt', snackPosition: SnackPosition.BOTTOM, backgroundColor: Get.theme.colorScheme.primary.withAlpha(26), colorText: Get.theme.colorScheme.primary, duration: Duration(seconds: 3), ); } else { Get.snackbar( 'Fehler', 'Ungültige Backup-Datei', snackPosition: SnackPosition.BOTTOM, backgroundColor: Get.theme.colorScheme.error.withAlpha(26), colorText: Get.theme.colorScheme.error, ); } } } } catch (e) { Get.snackbar( 'Fehler', 'Wiederherstellung fehlgeschlagen: $e', snackPosition: SnackPosition.BOTTOM, backgroundColor: Get.theme.colorScheme.error.withAlpha(26), colorText: Get.theme.colorScheme.error, ); } } }