2023-10-09 09:32:07 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:file_picker/file_picker.dart';
|
2024-01-29 22:36:04 +08:00
|
|
|
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-13 21:58:25 +08:00
|
|
|
|
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;
|
2024-01-29 22:36:04 +08:00
|
|
|
final bool isP4kDownload;
|
2023-10-09 09:32:07 +08:00
|
|
|
|
|
|
|
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
|
2024-01-29 22:36:04 +08:00
|
|
|
{this.showChangeSavePathDialog = false,
|
|
|
|
this.threadCount = 1,
|
|
|
|
this.isP4kDownload = false});
|
2023-10-09 09:32:07 +08:00
|
|
|
|
|
|
|
bool isInMerging = false;
|
|
|
|
|
2023-11-05 15:56:48 +08:00
|
|
|
String? downloadTaskId;
|
|
|
|
|
2023-10-09 09:32:07 +08:00
|
|
|
double? progress;
|
2023-10-13 21:58:25 +08:00
|
|
|
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();
|
|
|
|
}
|
2023-11-05 15:56:48 +08:00
|
|
|
|
|
|
|
if (savePath.endsWith("\\$fileName")) {
|
|
|
|
savePath = savePath.substring(0, savePath.length - fileName.length - 1);
|
|
|
|
}
|
|
|
|
|
2024-01-29 22:36:04 +08:00
|
|
|
if (isP4kDownload) {
|
|
|
|
final box = await Hive.openBox("p4k_cache");
|
|
|
|
await box.put(
|
|
|
|
"last_save_dir",
|
|
|
|
{"save_path": savePath, "file_name": fileName},
|
|
|
|
);
|
|
|
|
}
|
2023-11-05 15:56:48 +08:00
|
|
|
|
2024-01-29 22:36:04 +08:00
|
|
|
final downloaderSavePath = "$savePath//$fileName.downloading";
|
2023-10-13 21:58:25 +08:00
|
|
|
try {
|
2024-02-06 20:19:53 +08:00
|
|
|
rust_downloader
|
2023-11-05 15:56:48 +08:00
|
|
|
.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;
|
2023-10-13 21:58:25 +08:00
|
|
|
}
|
2023-11-05 15:56:48 +08:00
|
|
|
speed = event.speed;
|
|
|
|
if (total != null && total != 0 && event.progress != 0) {
|
|
|
|
progress = (event.progress / total!) * 100;
|
2023-10-13 21:58:25 +08:00
|
|
|
}
|
|
|
|
notifyListeners();
|
2023-11-05 15:56:48 +08:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
2023-10-13 21:58:25 +08:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2023-11-05 15:56:48 +08:00
|
|
|
Navigator.pop(context!, e);
|
2023-10-13 21:58:25 +08:00
|
|
|
}
|
2023-10-09 09:32:07 +08:00
|
|
|
}
|
|
|
|
|
2024-02-06 20:19:53 +08:00
|
|
|
doCancel() async {
|
2023-10-13 21:58:25 +08:00
|
|
|
try {
|
2023-11-05 15:56:48 +08:00
|
|
|
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");
|
2023-11-05 15:56:48 +08:00
|
|
|
}
|
2023-10-13 21:58:25 +08:00
|
|
|
} catch (_) {}
|
2023-10-09 09:32:07 +08:00
|
|
|
}
|
|
|
|
}
|