app/lib/ui/tools/downloader/downloader_dialog_ui_model.dart

128 lines
3.4 KiB
Dart
Raw Normal View History

2023-10-09 09:32:07 +08:00
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:hive/hive.dart';
2023-10-09 09:32:07 +08:00
import 'package:starcitizen_doctor/base/ui_model.dart';
2024-02-06 20:19:53 +08:00
import 'package:starcitizen_doctor/common/rust/api/downloader_api.dart'
as rust_downloader;
import 'package:starcitizen_doctor/common/rust/downloader.dart';
2023-10-09 09:32:07 +08:00
class DownloaderDialogUIModel extends BaseUIModel {
final String fileName;
String savePath;
final String downloadUrl;
final bool showChangeSavePathDialog;
final int threadCount;
final bool isP4kDownload;
2023-10-09 09:32:07 +08:00
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
{this.showChangeSavePathDialog = false,
this.threadCount = 1,
this.isP4kDownload = false});
2023-10-09 09:32:07 +08:00
bool isInMerging = false;
String? downloadTaskId;
2023-10-09 09:32:07 +08:00
double? progress;
int? speed;
2023-10-09 09:32:07 +08:00
int? count;
int? total;
@override
void initModel() {
super.initModel();
_initDownload();
}
_initDownload() async {
if (showChangeSavePathDialog) {
final userSelect = await FilePicker.platform.saveFile(
initialDirectory: savePath,
fileName: fileName,
lockParentWindow: true);
if (userSelect == null) {
Navigator.pop(context!);
return;
}
final f = File(userSelect);
if (await f.exists()) {
await f.delete();
}
savePath = userSelect;
dPrint(savePath);
notifyListeners();
}
if (savePath.endsWith("\\$fileName")) {
savePath = savePath.substring(0, savePath.length - fileName.length - 1);
}
if (isP4kDownload) {
final box = await Hive.openBox("p4k_cache");
await box.put(
"last_save_dir",
{"save_path": savePath, "file_name": fileName},
);
}
final downloaderSavePath = "$savePath//$fileName.downloading";
try {
2024-02-06 20:19:53 +08:00
rust_downloader
.startDownload(
url: downloadUrl,
savePath: savePath,
fileName: "$fileName.downloading",
connectionCount: 10)
.listen((event) async {
dPrint(
"id == ${event.id} p ==${event.progress} t==${event.total} s==${event.speed} st==${event.status}");
downloadTaskId = event.id;
count = event.progress;
if (event.total != 0) {
total = event.total;
}
speed = event.speed;
if (total != null && total != 0 && event.progress != 0) {
progress = (event.progress / total!) * 100;
}
notifyListeners();
if (progress != null &&
progress != 0 &&
event.status == const MyDownloaderStatus.noStart()) {
Navigator.pop(context!, "cancel");
return;
}
if (event.status == const MyDownloaderStatus.finished()) {
count = total;
isInMerging = true;
notifyListeners();
await File(downloaderSavePath)
.rename(downloaderSavePath.replaceAll(".downloading", ""));
final bsonFile = File("$downloaderSavePath.bson");
if (await bsonFile.exists()) {
bsonFile.delete();
}
Navigator.pop(context!, "$savePath\\$fileName");
}
});
} catch (e) {
Navigator.pop(context!, e);
}
2023-10-09 09:32:07 +08:00
}
2024-02-06 20:19:53 +08:00
doCancel() async {
try {
if (downloadTaskId != null) {
2024-02-06 20:19:53 +08:00
await rust_downloader.cancelDownload(id: downloadTaskId!);
2023-11-05 16:36:47 +08:00
downloadTaskId = null;
} else {
Navigator.pop(context!, "cancel");
}
} catch (_) {}
2023-10-09 09:32:07 +08:00
}
}