2022-01-18 21:26:52 +00:00
|
|
|
class FileDownloadProgress {
|
|
|
|
int chunksDownloaded = 0;
|
|
|
|
int chunksTotal = 1;
|
|
|
|
bool complete = false;
|
|
|
|
bool gotManifest = false;
|
2024-01-04 19:52:02 +00:00
|
|
|
bool _interrupted = false;
|
|
|
|
|
|
|
|
// we keep track of both an explicit interrupt flag (for when a request fails or is explicitly cancelled)
|
|
|
|
set interrupted(isInterrupted) {
|
|
|
|
this._interrupted = isInterrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
// but we have a fuzzy get which depends on lastUpdate, if the file isn't complete, but the last update was more
|
|
|
|
// that 30 seconds ago, we consider this download as failed.
|
|
|
|
get interrupted => _interrupted || (DateTime.now().difference(lastUpdate).abs().inSeconds > 30 && !complete);
|
|
|
|
|
2022-01-18 21:26:52 +00:00
|
|
|
String? downloadedTo;
|
|
|
|
DateTime? timeStart;
|
|
|
|
DateTime? timeEnd;
|
2022-07-06 19:14:40 +00:00
|
|
|
DateTime? requested;
|
2024-01-04 19:52:02 +00:00
|
|
|
DateTime lastUpdate = DateTime.fromMillisecondsSinceEpoch(0);
|
2022-01-18 21:26:52 +00:00
|
|
|
|
|
|
|
FileDownloadProgress(this.chunksTotal, this.timeStart);
|
2022-07-06 19:14:40 +00:00
|
|
|
|
2022-01-18 21:26:52 +00:00
|
|
|
double progress() {
|
|
|
|
return 1.0 * chunksDownloaded / chunksTotal;
|
|
|
|
}
|
2023-04-20 22:48:00 +00:00
|
|
|
|
|
|
|
void markUpdate() {
|
|
|
|
lastUpdate = DateTime.now();
|
|
|
|
}
|
2022-01-18 21:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
String prettyBytes(int bytes) {
|
|
|
|
if (bytes > 1000000000) {
|
|
|
|
return (1.0 * bytes / 1000000000).toStringAsFixed(1) + " GB";
|
|
|
|
} else if (bytes > 1000000) {
|
|
|
|
return (1.0 * bytes / 1000000).toStringAsFixed(1) + " MB";
|
|
|
|
} else if (bytes > 1000) {
|
|
|
|
return (1.0 * bytes / 1000).toStringAsFixed(1) + " kB";
|
|
|
|
} else {
|
|
|
|
return bytes.toString() + " B";
|
|
|
|
}
|
2022-01-19 21:58:52 +00:00
|
|
|
}
|