up and download data
This commit is contained in:
@@ -1,17 +1,126 @@
|
||||
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);
|
||||
}
|
||||
void downloadFilaments() {
|
||||
// JSON Backup file herunterladen
|
||||
|
||||
Future<void> 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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void restoreFilaments() {
|
||||
// JSON Backup file wiederherstellen
|
||||
Future<void> _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<void> restoreFilaments() async {
|
||||
try {
|
||||
// Datei auswählen
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['json'],
|
||||
);
|
||||
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
15
lib/helpers/web_download_helper.dart
Normal file
15
lib/helpers/web_download_helper.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'dart:convert';
|
||||
// ignore: avoid_web_libraries_in_flutter
|
||||
import 'dart:html' as html;
|
||||
|
||||
void downloadJsonFileWeb(String jsonData, String fileName) {
|
||||
final bytes = utf8.encode(jsonData);
|
||||
final blob = html.Blob([bytes]);
|
||||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||||
|
||||
final anchor = html.AnchorElement(href: url)
|
||||
..setAttribute('download', fileName)
|
||||
..click();
|
||||
|
||||
html.Url.revokeObjectUrl(url);
|
||||
}
|
||||
3
lib/helpers/web_download_helper_stub.dart
Normal file
3
lib/helpers/web_download_helper_stub.dart
Normal file
@@ -0,0 +1,3 @@
|
||||
void downloadJsonFileWeb(String jsonData, String fileName) {
|
||||
throw UnsupportedError('Web download is only supported on web platform');
|
||||
}
|
||||
Reference in New Issue
Block a user