Skip to content

Commit

Permalink
Feat: 导入下载任务数据备份
Browse files Browse the repository at this point in the history
  • Loading branch information
honjow committed Oct 23, 2021
1 parent f4b457f commit 47dc76f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
10 changes: 3 additions & 7 deletions lib/common/controller/download_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class DownloadController extends GetxController {
// 'DownloadController onInit multiDownload:${ehConfigService.multiDownload}');
dState.executor = Executor(concurrency: ehConfigService.multiDownload);
allowMediaScan(ehConfigService.allowMediaScan);
_initGalleryTasks();
initGalleryTasks();
}

/// 移除任务
Expand Down Expand Up @@ -301,7 +301,7 @@ class DownloadController extends GetxController {
dState.executor = Executor(concurrency: ehConfigService.multiDownload);

// 重新加入
_initGalleryTasks();
initGalleryTasks();
}

void _addAllImages(int gid, List<GalleryImage> galleryImages) {
Expand Down Expand Up @@ -668,20 +668,16 @@ class DownloadController extends GetxController {
);
}

Future<void> _initGalleryTasks() async {
// logger5.v(' _initGalleryTasks');
Future<void> initGalleryTasks() async {
GalleryTaskDao _galleryTaskDao;
// ImageTaskDao _imageTaskDao;
try {
_galleryTaskDao = await getGalleryTaskDao();
// _imageTaskDao = await getImageTaskDao();
} catch (e, stack) {
logger.e('$e\n$stack ');
rethrow;
}

final _tasks = await _galleryTaskDao.findAllGalleryTasks();
// dState.galleryTaskList(_tasks);

// 添加到map中
for (final _task in _tasks) {
Expand Down
73 changes: 73 additions & 0 deletions lib/pages/tab/controller/download_view_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import 'package:fehviewer/utils/utility.dart';
import 'package:fehviewer/utils/vibrate.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_app_restart/flutter_app_restart.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:line_icons/line_icons.dart';
import 'package:path/path.dart' as path;
import 'package:restart_app/restart_app.dart';
import 'package:share/share.dart';

enum DownloadType {
Expand Down Expand Up @@ -390,6 +393,41 @@ class DownloadViewController extends GetxController {
return _zipPath;
}

Future<void> _readTaskInfoFile(File importFile) async {
final decoder = ZipDecoder();
final archive = decoder.decodeBytes(importFile.readAsBytesSync());
final archivePath = path.join(Global.tempPath, 'archive');

for (final file in archive) {
final filename = file.name;
if (file.isFile && filename == EHConst.DB_NAME) {
final data = file.content as List<int>;
File(path.join(archivePath, filename))
..createSync(recursive: true)
..writeAsBytesSync(data);
} else {
Directory(path.join(archivePath, filename)).create(recursive: true);
}
}

final _pathImportDB = path.join(archivePath, EHConst.DB_NAME);
final _importDBFile = File(_pathImportDB);
if (!_importDBFile.existsSync()) {
return;
}

final importDb = await Global.getDatabase(path: _pathImportDB);
final allImageTasks = await importDb.imageTaskDao.findAllTasks();
final allTasks = await importDb.galleryTaskDao.findAllGalleryTasks();

logger.d('import task ${allTasks.length}');

// 从临时db导入任务数据
final ehDB = await Global.getDatabase();
await ehDB.imageTaskDao.insertOrReplaceImageTasks(allImageTasks);
await ehDB.galleryTaskDao.insertOrReplaceTasks(allTasks);
}

String _getLocalFilePath() {
final DateTime _now = DateTime.now();
final DateFormat formatter = DateFormat('yyyyMMdd_HHmmss');
Expand Down Expand Up @@ -424,13 +462,48 @@ class DownloadViewController extends GetxController {
}

Future importTaskInfoFile() async {
await requestManageExternalStoragePermission();
final FilePickerResult? result = await FilePicker.platform.pickFiles();
logger.d('import file $result');
if (result != null) {
final File _file = File(result.files.single.path!);
await _readTaskInfoFile(_file);
_downloadController.initGalleryTasks();
update([idDownloadGalleryView]);
await _showRestartAppDialog();
}
}
}

Future<void> _showRestartAppDialog() async {
return showCupertinoDialog<void>(
context: Get.overlayContext!,
barrierDismissible: true,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: const Text('Restart App'),
content: const Text('重启应用以生效?'),
actions: [
CupertinoDialogAction(
onPressed: () async {
Get.back();
// Restart.restartApp();
await FlutterRestart.restartApp();
},
child: const Text('Restart Now'),
),
CupertinoDialogAction(
onPressed: () async {
Get.back();
},
child: Text(L10n.of(context).cancel),
),
],
);
},
);
}

Future<String?> _exportGallery(
BuildContext context,
FunctionExport funExport, {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/tab/view/download_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class _DownloadTabState extends State<DownloadTab> {
CupertinoDialogAction(
onPressed: () async {
Get.back();
controller.importTaskInfoFile();
await controller.importTaskInfoFile();
},
child: Row(
mainAxisSize: MainAxisSize.min,
Expand Down
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_app_restart:
dependency: "direct main"
description:
name: flutter_app_restart
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.0"
flutter_blurhash:
dependency: transitive
description:
Expand Down Expand Up @@ -1256,6 +1263,13 @@ packages:
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.4.1"
restart_app:
dependency: "direct main"
description:
name: restart_app
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.0"
rxdart:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: fehviewer

publish_to: 'none' # Remove this line if you wish to publish to pub.dev

version: 1.1.28+321
version: 1.1.29+322

environment:
sdk: '>=2.14.0 <3.0.0'
Expand Down Expand Up @@ -142,6 +142,8 @@ dependencies:
encrypt: ^5.0.1
wakelock: ^0.5.3+3
jinja: ^0.3.4
restart_app: ^1.1.0
flutter_app_restart: ^1.0.0

dev_dependencies:
# flutter packages pub run build_runner build --delete-conflicting-outputs
Expand Down

0 comments on commit 47dc76f

Please sign in to comment.