mirror of
https://ghfast.top/https://github.com/StarCitizenToolBox/app.git
synced 2025-07-29 17:21:19 +08:00
re init
This commit is contained in:
60
lib/ui/tools/downloader/downloader_dialog_ui.dart
Normal file
60
lib/ui/tools/downloader/downloader_dialog_ui.dart
Normal file
@ -0,0 +1,60 @@
|
||||
import 'package:file_sizes/file_sizes.dart';
|
||||
import 'package:starcitizen_doctor/base/ui.dart';
|
||||
|
||||
import 'downloader_dialog_ui_model.dart';
|
||||
|
||||
class DownloaderDialogUI extends BaseUI<DownloaderDialogUIModel> {
|
||||
@override
|
||||
Widget? buildBody(BuildContext context, DownloaderDialogUIModel model) {
|
||||
return ContentDialog(
|
||||
constraints:
|
||||
BoxConstraints(maxWidth: MediaQuery.of(context).size.width * .54),
|
||||
title: const Text("文件下载..."),
|
||||
content: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text("文件名:${model.fileName}"),
|
||||
const SizedBox(height: 6),
|
||||
Text("保存位置:${model.savePath}"),
|
||||
const SizedBox(height: 6),
|
||||
Text("线程数:${model.threadCount}"),
|
||||
const SizedBox(height: 6),
|
||||
Text(
|
||||
"文件大小: ${FileSize.getSize(model.count ?? 0)} / ${FileSize.getSize(model.total ?? 0)}"),
|
||||
const SizedBox(height: 6),
|
||||
Text("下载速度: ${FileSize.getSize(model.speed?.toInt() ?? 0)}/s"),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
children: [
|
||||
Text(getStatus(model)),
|
||||
const SizedBox(width: 24),
|
||||
Expanded(
|
||||
child: ProgressBar(
|
||||
value: model.progress == 100 ? null : model.progress,
|
||||
)),
|
||||
const SizedBox(width: 24),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
FilledButton(
|
||||
child: const Padding(
|
||||
padding: EdgeInsets.only(left: 8, right: 8, top: 2, bottom: 2),
|
||||
child: Text("取消下载"),
|
||||
),
|
||||
onPressed: () => model.doCancel()),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
String getUITitle(BuildContext context, DownloaderDialogUIModel model) => "";
|
||||
|
||||
String getStatus(DownloaderDialogUIModel model) {
|
||||
if (model.progress == null && !model.isInMerging) return "准备中...";
|
||||
if (model.isInMerging) return "正在合并文件...";
|
||||
return "${model.progress?.toStringAsFixed(2) ?? "0"}% ";
|
||||
}
|
||||
}
|
100
lib/ui/tools/downloader/downloader_dialog_ui_model.dart
Normal file
100
lib/ui/tools/downloader/downloader_dialog_ui_model.dart
Normal file
@ -0,0 +1,100 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:hyper_thread_downloader/hyper_thread_downloader.dart';
|
||||
import 'package:starcitizen_doctor/base/ui_model.dart';
|
||||
|
||||
class DownloaderDialogUIModel extends BaseUIModel {
|
||||
final String fileName;
|
||||
String savePath;
|
||||
final String downloadUrl;
|
||||
final bool showChangeSavePathDialog;
|
||||
final int threadCount;
|
||||
|
||||
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
|
||||
{this.showChangeSavePathDialog = false, this.threadCount = 1});
|
||||
|
||||
final downloader = HyperDownload();
|
||||
|
||||
int? downloadTaskId;
|
||||
|
||||
bool isInMerging = false;
|
||||
|
||||
double? progress;
|
||||
double? speed;
|
||||
double? remainTime;
|
||||
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();
|
||||
} else {
|
||||
savePath = "$savePath/$fileName";
|
||||
}
|
||||
// start download
|
||||
downloader.startDownload(
|
||||
url: downloadUrl,
|
||||
savePath: savePath,
|
||||
threadCount: threadCount,
|
||||
prepareWorking: (bool done) {},
|
||||
workingMerge: (bool done) {
|
||||
isInMerging = true;
|
||||
progress = null;
|
||||
notifyListeners();
|
||||
},
|
||||
downloadProgress: ({
|
||||
required double progress,
|
||||
required double speed,
|
||||
required double remainTime,
|
||||
required int count,
|
||||
required int total,
|
||||
}) {
|
||||
this.progress = ((progress) * 100);
|
||||
this.speed = speed;
|
||||
this.remainTime = remainTime;
|
||||
this.count = count;
|
||||
this.total = total;
|
||||
notifyListeners();
|
||||
},
|
||||
downloadComplete: () {
|
||||
notifyListeners();
|
||||
Navigator.pop(context!, savePath);
|
||||
},
|
||||
downloadFailed: (String reason) {
|
||||
notifyListeners();
|
||||
showToast(context!, "下载失败! $reason");
|
||||
},
|
||||
downloadTaskId: (int id) {
|
||||
downloadTaskId = id;
|
||||
},
|
||||
downloadingLog: (String log) {});
|
||||
}
|
||||
|
||||
doCancel() {
|
||||
if (downloadTaskId != null) {
|
||||
downloader.stopDownload(id: downloadTaskId!);
|
||||
}
|
||||
Navigator.pop(context!, "cancel");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user