mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-10 19:27:15 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWXTUM9dpnCS2wtFkx4
38 lines
1.2 KiB
Dart
38 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:data_transfer_flutter/data_transfer_flutter.dart';
|
|
import 'package:workout_app/services/storage_service.dart';
|
|
|
|
String _exportFileName(DateTime now) =>
|
|
'workout_app-export-${now.toIso8601String().substring(0, 10)}.json';
|
|
|
|
/// Parses [source] and merges it; throws [FormatException] on a bad file.
|
|
Future<ImportReport> importExportText(String source) async {
|
|
final decoded = jsonDecode(source);
|
|
if (decoded is! Map<String, dynamic>) {
|
|
throw const FormatException('export is not a JSON object');
|
|
}
|
|
return await StorageService.instance.importDataReport(decoded);
|
|
}
|
|
|
|
// coverage:ignore-start
|
|
|
|
/// Saves an export via the system Save-as picker; false when cancelled.
|
|
Future<bool> exportUserData() async {
|
|
final json = const JsonEncoder.withIndent(
|
|
' ',
|
|
).convert(await StorageService.instance.exportData());
|
|
return await exportJsonText(
|
|
fileName: _exportFileName(DateTime.now()),
|
|
text: json,
|
|
);
|
|
}
|
|
|
|
/// Picks an export and merges it; null when cancelled.
|
|
Future<ImportReport?> importUserData() async {
|
|
final text = await pickJsonText();
|
|
if (text == null) return null;
|
|
return await importExportText(text);
|
|
}
|
|
// coverage:ignore-end
|