2023-10-09 09:32:07 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-10-13 21:58:25 +08:00
|
|
|
import 'package:dio/dio.dart';
|
2023-10-09 09:32:07 +08:00
|
|
|
import 'package:file_picker/file_picker.dart';
|
|
|
|
import 'package:starcitizen_doctor/base/ui_model.dart';
|
|
|
|
|
2023-10-13 21:58:25 +08:00
|
|
|
import 'dio_range_download.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;
|
|
|
|
|
|
|
|
DownloaderDialogUIModel(this.fileName, this.savePath, this.downloadUrl,
|
|
|
|
{this.showChangeSavePathDialog = false, this.threadCount = 1});
|
|
|
|
|
2023-10-13 21:58:25 +08:00
|
|
|
CancelToken? downloadCancelToken;
|
2023-10-09 09:32:07 +08:00
|
|
|
|
|
|
|
int? downloadTaskId;
|
|
|
|
|
|
|
|
bool isInMerging = false;
|
|
|
|
|
|
|
|
double? progress;
|
2023-10-13 21:58:25 +08:00
|
|
|
int? speed;
|
|
|
|
DateTime? lastUpdateTime;
|
|
|
|
int? lastUpdateCount;
|
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();
|
|
|
|
} else {
|
|
|
|
savePath = "$savePath/$fileName";
|
|
|
|
}
|
|
|
|
// start download
|
2023-10-13 21:58:25 +08:00
|
|
|
try {
|
|
|
|
downloadCancelToken = CancelToken();
|
|
|
|
final r = await RangeDownload.downloadWithChunks(downloadUrl, savePath,
|
|
|
|
maxChunk: 10,
|
|
|
|
cancelToken: downloadCancelToken,
|
|
|
|
isRangeDownload: true, onReceiveProgress: (int count, int total) {
|
|
|
|
lastUpdateTime ??= DateTime.now();
|
|
|
|
if ((DateTime.now().difference(lastUpdateTime ?? DateTime.now()))
|
|
|
|
.inSeconds >=
|
|
|
|
1) {
|
|
|
|
lastUpdateTime = DateTime.now();
|
|
|
|
speed = (count - (lastUpdateCount ?? 0));
|
|
|
|
lastUpdateCount = count;
|
2023-10-09 09:32:07 +08:00
|
|
|
notifyListeners();
|
2023-10-13 21:58:25 +08:00
|
|
|
}
|
|
|
|
this.count = count;
|
|
|
|
this.total = total;
|
|
|
|
progress = count / total * 100;
|
|
|
|
if (count == total) {
|
|
|
|
isInMerging = true;
|
|
|
|
}
|
|
|
|
notifyListeners();
|
|
|
|
});
|
|
|
|
if (r.statusCode == 200) {
|
|
|
|
Navigator.pop(context!, savePath);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e is DioException && e.type != DioExceptionType.cancel) {
|
|
|
|
if (mounted) showToast(context!, "下载失败:$e");
|
|
|
|
}
|
|
|
|
}
|
2023-10-09 09:32:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
doCancel() {
|
2023-10-13 21:58:25 +08:00
|
|
|
try {
|
|
|
|
downloadCancelToken?.cancel();
|
|
|
|
downloadCancelToken = null;
|
|
|
|
} catch (_) {}
|
2023-10-09 09:32:07 +08:00
|
|
|
Navigator.pop(context!, "cancel");
|
|
|
|
}
|
|
|
|
}
|