Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 64 additions & 31 deletions lib/network/eh_network/eh_download_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,22 @@ class EhDownloadingItem extends DownloadingItem{
_downloadLink = res.data;
}
_downloader = _IsolateDownloader(
_downloadLink!,
path,
(current, total, speed){
_currentBytes = current;
_totalBytes = total;
_currentSpeed = speed;
updateInfo?.call();
if(current == total){
if (DownloadManager().downloading.firstOrNull != this) return;
finish();
}
},
onError!
);
_downloadLink!,
path,
(current, total, speed){
_currentBytes = current;
_totalBytes = total;
_currentSpeed = speed;
if(current == total){
if (DownloadManager().downloading.firstOrNull != this) return;
finish(); // _onFinish 中已含 _saveInfo → 通知
} else {
updateInfo?.call(); // 只在未完成时通知进度
}
},
onError!
);

_downloader!.start();
}
catch(e, s){
Expand All @@ -216,9 +218,15 @@ class EhDownloadingItem extends DownloadingItem{
}
}

void finish() async{
onFinish?.call();
}
void finish() async{
try {
onFinish?.call();
} catch (e, s) {
LogManager.addLog(LogLevel.error, "Download", "Finish error: $e\n$s");
onError?.call();
}
}


@override
pause() async{
Expand Down Expand Up @@ -342,22 +350,47 @@ class _IsolateDownloader{
sendPort.send(status);
}
ZipFile.openAndExtract("$savePath/temp.zip", savePath);
var files = Directory(savePath).listSync();
files.sort((a, b) => a.path.compareTo(b.path));
int index = 0;
for(var entry in Directory(savePath).listSync()){
if(entry is File){
var name = entry.path.split(pathSep).last;
if(name.endsWith(".zip")){
entry.deleteSync();
} else if(!name.contains("cover")){
var baseName = index.toString();
index++;
var ext = name.split(".").last;
entry.renameSync("$savePath/$baseName.$ext");
var files = Directory(savePath)
.listSync()
.whereType<File>()
.where((f) => !f.path.endsWith(".zip") && !f.path.contains("cover"))
.toList();

// 自然排序:按文件名中交替出现的文本段和数字段依次比较
int naturalCompare(String a, String b) {
final regex = RegExp(r'(\D+)|(\d+)');
final aParts = regex.allMatches(a).map((m) => m.group(0)!).toList();
final bParts = regex.allMatches(b).map((m) => m.group(0)!).toList();

for (int i = 0; i < aParts.length && i < bParts.length; i++) {
final numA = int.tryParse(aParts[i]);
final numB = int.tryParse(bParts[i]);

if (numA != null && numB != null) {
final cmp = numA.compareTo(numB);
if (cmp != 0) return cmp;
} else {
final cmp = aParts[i].compareTo(bParts[i]);
if (cmp != 0) return cmp;
}
}
return aParts.length.compareTo(bParts.length);
}

files.sort((a, b) {
final nameA = a.path.split(pathSep).last;
final nameB = b.path.split(pathSep).last;
return naturalCompare(nameA, nameB);
});

for (int index = 0; index < files.length; index++) {
final entry = files[index];
final ext = entry.path.split(".").last;
entry.renameSync("$savePath/$index.$ext");
}

// 删除 zip 文件
File("$savePath/temp.zip").deleteSync();
sendPort.send("finish");
}
catch(e, s){
Expand All @@ -381,4 +414,4 @@ class _DownloadException{
final String message;

const _DownloadException(this.message);
}
}
10 changes: 6 additions & 4 deletions lib/pages/downloading_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ class _DownloadingTileState extends State<_DownloadingTile> {
}
}

void updateStatistic() {
if(comic != DownloadManager().downloading.first) {
void updateStatistic() {
final firstItem = DownloadManager().downloading.firstOrNull;
if (firstItem == null || comic != firstItem) {
return;
}
comic = DownloadManager().downloading.first;
comic = firstItem;
speed = comic.currentSpeed;
downloadPages = comic.downloadedPages;
pagesCount = comic.totalPages;
Expand All @@ -209,7 +210,8 @@ class _DownloadingTileState extends State<_DownloadingTile> {
if (pagesCount != null && pagesCount! > 0) {
value = downloadPages / pagesCount!;
}
}
}


void updateUi() {
setState(() {
Expand Down