import 'package:toolbox/core/extension/numx.dart'; get initNetSpeedPart => NetSpeedPart( '', 0, 0, 0, ); get initNetSpeed => NetSpeed( [initNetSpeedPart], [initNetSpeedPart], ); class NetSpeedPart { String device; int bytesIn; int bytesOut; int time; NetSpeedPart(this.device, this.bytesIn, this.bytesOut, this.time); } class NetSpeed { List _old; List _now; NetSpeed(this._old, this._now); List get devices { final devices = []; for (var item in _now) { devices.add(item.device); } return devices; } void update(List newOne) { _old = _now; _now = newOne; } int get timeDiff => _now[0].time - _old[0].time; String speedIn({String? device}) { if (_old[0].device == '' || _now[0].device == '') return '0kb/s'; final idx = deviceIdx(device); final speedInBytesPerSecond = (_now[idx].bytesIn - _old[idx].bytesIn) / timeDiff; return buildStandardOutput(speedInBytesPerSecond); } String speedOut({String? device}) { if (_old[0].device == '' || _now[0].device == '') return '0kb/s'; final idx = deviceIdx(device); final speedOutBytesPerSecond = (_now[idx].bytesOut - _old[idx].bytesOut) / timeDiff; return buildStandardOutput(speedOutBytesPerSecond); } int deviceIdx(String? device) { if (device != null) { for (var item in _now) { if (item.device == device) { return _now.indexOf(item); } } } return 0; } String buildStandardOutput(double speed) => '${speed.convertBytes.toLowerCase()}/s'; }