Skip to content

Commit

Permalink
Optimize data export logic
Browse files Browse the repository at this point in the history
优化数据导出逻辑
  • Loading branch information
jiangtian616 committed Aug 2, 2024
1 parent 4215c54 commit 86dcfe7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 24 deletions.
5 changes: 5 additions & 0 deletions changelog/v8.0.0+251.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- 优化数据导出逻辑

------------------------------------------------------------------------------------------

- Optimize data export logic
85 changes: 63 additions & 22 deletions lib/src/pages/setting/advanced/setting_advanced_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -374,34 +374,77 @@ class _SettingAdvancedPageState extends State<SettingAdvancedPage> {
context: context,
builder: (_) => EHConfigTypeSelectDialog(title: 'selectExportItems'.tr),
);

if (result?.isEmpty ?? true) {
return;
}

String? path;
String fileName = '${CloudConfigService.configFileName}-${DateFormat('yyyyMMddHHmmss').format(DateTime.now())}.json';
if (GetPlatform.isMobile) {
return _exportDataMobile(fileName, result);
} else {
return _exportDataDesktop(fileName, result);
}
}

Future<void> _exportDataMobile(String fileName, List<CloudConfigTypeEnum>? result) async {
if (_exportDataLoadingState == LoadingState.loading) {
return;
}
setStateSafely(() => _exportDataLoadingState = LoadingState.loading);

List<CloudConfig> uploadConfigs = [];
for (CloudConfigTypeEnum type in result!) {
CloudConfig? config = await cloudConfigService.getLocalConfig(type);
if (config != null) {
uploadConfigs.add(config);
}
}

try {
path = await FilePicker.platform.getDirectoryPath();
String? savedPath = await FilePicker.platform.saveFile(
fileName: fileName,
type: FileType.custom,
allowedExtensions: ['json'],
bytes: utf8.encode(await isolateService.jsonEncodeAsync(uploadConfigs)),
lockParentWindow: true,
);
if (savedPath != null) {
log.info('Export data to $savedPath success');
toast('success'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.success);
}
} on Exception catch (e) {
log.error('Pick export path failed', e);
return;
log.error('Export data failed', e);
toast('internalError'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.error);
}
}

if (path == null) {
Future<void> _exportDataDesktop(String fileName, List<CloudConfigTypeEnum>? result) async {
if (_exportDataLoadingState == LoadingState.loading) {
return;
}
if (!checkPermissionForPath(path)) {
toast('invalidPath'.tr, isShort: false);
setStateSafely(() => _exportDataLoadingState = LoadingState.loading);

String? savedPath;
try {
savedPath = await FilePicker.platform.saveFile(
fileName: fileName,
type: FileType.custom,
allowedExtensions: ['json'],
lockParentWindow: true,
);
} on Exception catch (e) {
log.error('Select save path for exporting data failed', e);
toast('internalError'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.error);
return;
}

if (_exportDataLoadingState == LoadingState.loading) {
if (savedPath == null) {
return;
}

log.info('Export data to $path');
setStateSafely(() => _exportDataLoadingState = LoadingState.loading);

List<CloudConfig> uploadConfigs = [];
for (CloudConfigTypeEnum type in result!) {
CloudConfig? config = await cloudConfigService.getLocalConfig(type);
Expand All @@ -410,22 +453,20 @@ class _SettingAdvancedPageState extends State<SettingAdvancedPage> {
}
}

File file = File(join(path, '${CloudConfigService.configFileName}-${DateFormat('yyyyMMddHHmmss').format(DateTime.now())}.json'));
if (await file.exists()) {
await file.create(recursive: true);
}

File file = File(savedPath);
try {
if (await file.exists()) {
await file.create(recursive: true);
}
await file.writeAsString(await isolateService.jsonEncodeAsync(uploadConfigs));
log.info('Export data to $savedPath success');
toast('success'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.success);
} on Exception catch (e) {
log.error('Export data failed', e);
toast('internalError'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.error);
file.delete();
return;
file.delete().ignore();
}

toast('success'.tr);
setStateSafely(() => _exportDataLoadingState = LoadingState.success);
}
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,10 +487,10 @@ packages:
dependency: "direct main"
description:
name: file_picker
sha256: b6283d7387310ad83bc4f3bc245b75d223a032ae6eba275afcd585de2b9a1476
sha256: "824f5b9f389bfc4dddac3dea76cd70c51092d9dff0b2ece7ef4f53db8547d258"
url: "https://pub.dev"
source: hosted
version: "8.0.1"
version: "8.0.6"
fixnum:
dependency: transitive
description:
Expand Down

0 comments on commit 86dcfe7

Please sign in to comment.