106 lines
3.1 KiB
Dart
106 lines
3.1 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../models/backup_model.dart';
|
|
import '../services/backup_file_service.dart';
|
|
import '../services/get_storage_services.dart';
|
|
import 'home_controller.dart';
|
|
|
|
class ExportController extends GetxController {
|
|
ExportController({BackupFileService? fileService})
|
|
: fileService = fileService ?? BackupFileService();
|
|
|
|
final BackupFileService fileService;
|
|
final storage = Get.find<GetStorageService>();
|
|
final isBusy = false.obs;
|
|
final message = RxnString();
|
|
final hasError = false.obs;
|
|
final pendingBackup = Rxn<BackupModel>();
|
|
|
|
Future<void> exportData() async {
|
|
if (isBusy.value) return;
|
|
isBusy.value = true;
|
|
message.value = null;
|
|
try {
|
|
final backup = BackupModel({
|
|
for (final key in BackupModel.keys) key: storage.getModelList(key),
|
|
});
|
|
final name =
|
|
'filament_backup_${DateFormat('yyyy-MM-dd_HH-mm-ss').format(DateTime.now())}.json';
|
|
if (await fileService.save(backup.encode(), name)) {
|
|
hasError.value = false;
|
|
message.value = 'Datensicherung als JSON-Datei exportiert.';
|
|
}
|
|
} catch (_) {
|
|
_error('Export fehlgeschlagen. Bitte erneut versuchen.');
|
|
} finally {
|
|
isBusy.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> selectImport() async {
|
|
if (isBusy.value) return;
|
|
isBusy.value = true;
|
|
message.value = null;
|
|
pendingBackup.value = null;
|
|
try {
|
|
final source = await fileService.open();
|
|
if (source != null) pendingBackup.value = BackupModel.decode(source);
|
|
} on FormatException catch (error) {
|
|
_error(error.message);
|
|
} catch (_) {
|
|
_error('Datei konnte nicht gelesen werden. Bitte erneut versuchen.');
|
|
} finally {
|
|
isBusy.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> importData() async {
|
|
final backup = pendingBackup.value;
|
|
if (isBusy.value || backup == null) return;
|
|
isBusy.value = true;
|
|
message.value = null;
|
|
final previous = <String, List<Map<String, dynamic>>>{};
|
|
final attempted = <String>[];
|
|
try {
|
|
for (final key in BackupModel.keys) {
|
|
previous[key] = storage.getModelList(key);
|
|
}
|
|
for (final key in BackupModel.keys) {
|
|
attempted.add(key);
|
|
await storage.saveModelList(key, backup.data[key]!);
|
|
}
|
|
if (Get.isRegistered<HomeController>()) {
|
|
final home = Get.find<HomeController>();
|
|
home.loadFilamente();
|
|
home.loadMaterialien();
|
|
home.resetForm();
|
|
}
|
|
pendingBackup.value = null;
|
|
hasError.value = false;
|
|
message.value = 'Alle Daten wurden importiert.';
|
|
} catch (_) {
|
|
var restored = true;
|
|
for (final key in attempted.reversed) {
|
|
try {
|
|
await storage.saveModelList(key, previous[key]!);
|
|
} catch (_) {
|
|
restored = false;
|
|
}
|
|
}
|
|
_error(
|
|
restored
|
|
? 'Import fehlgeschlagen. Die bisherigen Daten wurden beibehalten.'
|
|
: 'Import und Wiederherstellung fehlgeschlagen. Bitte die Datensicherung erneut importieren.',
|
|
);
|
|
} finally {
|
|
isBusy.value = false;
|
|
}
|
|
}
|
|
|
|
void _error(String text) {
|
|
hasError.value = true;
|
|
message.value = text;
|
|
}
|
|
}
|