Skip to content

Commit

Permalink
Add eta support
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed May 25, 2024
1 parent ddee58e commit afbda5a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/api/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class TaskDownloadSingleProgress {
final DateTime started;
final int downloaded;
final double speed;
@JsonKey(name: 'last_updated')
final int lastUpdated;
@JsonKey(name: 'last_updated', fromJson: _fromJson, toJson: _toJson)
final DateTime lastUpdated;
static DateTime _fromJson(int d) =>
DateTime.fromMillisecondsSinceEpoch(d, isUtc: true);
static int _toJson(DateTime d) => d.millisecondsSinceEpoch;
Expand Down
5 changes: 3 additions & 2 deletions lib/api/task.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/components/task.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class _TaskView extends State<TaskView> {
onTap: () {
context.push("/dialog/task/${widget.task.base.id}");
},
behavior: HitTestBehavior.opaque,
child: Column(children: [
_buildText(context),
LinearPercentIndicator(
Expand Down
16 changes: 14 additions & 2 deletions lib/dialog/task_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:go_router/go_router.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';
import '../api/task.dart';
import '../globals.dart';
import '../utils/duration.dart';
import '../utils/filesize.dart';

class _KeyValue extends StatelessWidget {
Expand Down Expand Up @@ -151,7 +152,8 @@ class _TaskPage extends State<TaskPage> {
_KeyValue(i18n.totalPages, p.totalPage.toString(), fontSize: 16),
_KeyValue(i18n.downloadedSize2, getFileSize(p.downloadedBytes),
fontSize: 16),
_KeyValue(i18n.speed, "${getFileSize((speed * 1000).toInt())}/s", fontSize: 16),
_KeyValue(i18n.speed, "${getFileSize((speed * 1000).toInt())}/s",
fontSize: 16),
]);
}
int now = 0;
Expand Down Expand Up @@ -199,12 +201,21 @@ class _TaskPage extends State<TaskPage> {
}
final p = task.progress as TaskDownloadProgess;
if (p.details.isEmpty) return SliverToBoxAdapter(child: Container());
final i18n = AppLocalizations.of(context)!;
return SliverList.builder(
itemCount: p.details.length,
itemBuilder: (context, index) {
final d = p.details[index];
final percent = d.total == 0 ? 0.0 : d.downloaded / d.total;
final percentText = "${(percent * 100).toStringAsFixed(2)}%";
final avgSpeed = d.started.millisecondsSinceEpoch == 0
? 0.0
: d.downloaded /
(d.lastUpdated.millisecondsSinceEpoch -
d.started.millisecondsSinceEpoch);
final eta = d.total == 0
? double.infinity
: (d.total - d.downloaded) / avgSpeed;
return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
SelectableText("${d.name}(${d.width}x${d.height})"),
LinearPercentIndicator(
Expand All @@ -223,7 +234,8 @@ class _TaskPage extends State<TaskPage> {
Expanded(
child: Text(
"${getFileSize(d.downloaded)}/${getFileSize(d.total)}")),
Text("${getFileSize((d.speed * 1000).toInt())}/s"),
Text(
"${getFileSize((d.speed * 1000).toInt())}/s${i18n.comma}${fmtDuration(context, eta)}"),
]),
]);
},
Expand Down
10 changes: 9 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,13 @@
},
"comma": ", ",
"downloadedSize2": "Downloaded size",
"speed": "Speed"
"speed": "Speed",
"days": "{num} days",
"@days": {
"placeholders": {
"num": {
"type": "int"
}
}
}
}
10 changes: 9 additions & 1 deletion lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,13 @@
},
"comma": ",",
"downloadedSize2": "已下载大小",
"speed": "速度"
"speed": "速度",
"days": "{num} 天",
"@days": {
"placeholders": {
"num": {
"type": "int"
}
}
}
}
21 changes: 21 additions & 0 deletions lib/utils/duration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

String fmtDuration(BuildContext context, double ms) {
if (ms.isInfinite) {
return "∞";
}
final dur = ms.toInt() ~/ 1000;
String re = "";
if (dur >= 86400) {
final i18n = AppLocalizations.of(context)!;
re += "${i18n.days(dur ~/ 86400)} ";
}
if (dur >= 3600) {
re += "${(dur ~/ 3600).toString().padLeft(2, '0')}:";
}
final min = (dur ~/ 60).toString().padLeft(2, '0');
final secs = (dur % 60).toString().padLeft(2, '0');
re += "$min:$secs";
return re;
}

0 comments on commit afbda5a

Please sign in to comment.