Line data Source code
1 : class FileDownloadProgress { 2 : int chunksDownloaded = 0; 3 : int chunksTotal = 1; 4 : bool complete = false; 5 : bool gotManifest = false; 6 : bool _interrupted = false; 7 : 8 : // we keep track of both an explicit interrupt flag (for when a request fails or is explicitly cancelled) 9 0 : set interrupted(isInterrupted) { 10 0 : this._interrupted = isInterrupted; 11 : } 12 : 13 : // but we have a fuzzy get which depends on lastUpdate, if the file isn't complete, but the last update was more 14 : // that 30 seconds ago, we consider this download as failed. 15 0 : get interrupted => _interrupted || (DateTime.now().difference(lastUpdate).abs().inSeconds > 30 && !complete); 16 : 17 : String? downloadedTo; 18 : DateTime? timeStart; 19 : DateTime? timeEnd; 20 : DateTime? requested; 21 : DateTime lastUpdate = DateTime.fromMillisecondsSinceEpoch(0); 22 : 23 0 : FileDownloadProgress(this.chunksTotal, this.timeStart); 24 : 25 0 : double progress() { 26 0 : return 1.0 * chunksDownloaded / chunksTotal; 27 : } 28 : 29 0 : void markUpdate() { 30 0 : lastUpdate = DateTime.now(); 31 : } 32 : } 33 : 34 0 : String prettyBytes(int bytes) { 35 0 : if (bytes > 1000000000) { 36 0 : return (1.0 * bytes / 1000000000).toStringAsFixed(1) + " GB"; 37 0 : } else if (bytes > 1000000) { 38 0 : return (1.0 * bytes / 1000000).toStringAsFixed(1) + " MB"; 39 0 : } else if (bytes > 1000) { 40 0 : return (1.0 * bytes / 1000).toStringAsFixed(1) + " kB"; 41 : } else { 42 0 : return bytes.toString() + " B"; 43 : } 44 : }