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

100 lines
2.5 KiB
Dart
Raw Normal View History

2023-10-09 09:32:07 +08:00
import 'dart:io';
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';
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});
CancelToken? downloadCancelToken;
2023-10-09 09:32:07 +08:00
int? downloadTaskId;
bool isInMerging = false;
double? progress;
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
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();
}
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) {
2023-10-29 17:10:39 +08:00
Navigator.pop(context!, e);
}
}
2023-10-09 09:32:07 +08:00
}
doCancel() {
try {
downloadCancelToken?.cancel();
downloadCancelToken = null;
} catch (_) {}
2023-10-09 09:32:07 +08:00
Navigator.pop(context!, "cancel");
}
}