2022-08-26 19:28:01 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class DiskSize {
|
2022-09-06 10:27:18 +00:00
|
|
|
const DiskSize({final this.byte = 0});
|
2022-08-24 05:35:49 +00:00
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
DiskSize.fromKibibyte(final double kibibyte)
|
|
|
|
: this(byte: (kibibyte * 1024).round());
|
|
|
|
DiskSize.fromMebibyte(final double mebibyte)
|
|
|
|
: this(byte: (mebibyte * 1024 * 1024).round());
|
|
|
|
DiskSize.fromGibibyte(final double gibibyte)
|
|
|
|
: this(byte: (gibibyte * 1024 * 1024 * 1024).round());
|
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
final int byte;
|
2022-08-26 19:28:01 +00:00
|
|
|
|
2022-09-06 09:17:44 +00:00
|
|
|
double get kibibyte => byte / 1024.0;
|
|
|
|
double get mebibyte => byte / 1024.0 / 1024.0;
|
|
|
|
double get gibibyte => byte / 1024.0 / 1024.0 / 1024.0;
|
2022-08-24 05:35:49 +00:00
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
DiskSize operator +(final DiskSize other) =>
|
|
|
|
DiskSize(byte: byte + other.byte);
|
|
|
|
DiskSize operator -(final DiskSize other) =>
|
|
|
|
DiskSize(byte: byte - other.byte);
|
|
|
|
DiskSize operator *(final double other) =>
|
|
|
|
DiskSize(byte: (byte * other).round());
|
|
|
|
|
2022-08-26 19:28:01 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
if (byte < 1024) {
|
|
|
|
return '${byte.toStringAsFixed(0)} ${tr('bytes')}';
|
|
|
|
} else if (byte < 1024 * 1024) {
|
2022-09-06 09:17:44 +00:00
|
|
|
return 'providers.storage.kb'.tr(args: [kibibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
} else if (byte < 1024 * 1024 * 1024) {
|
2022-09-06 09:17:44 +00:00
|
|
|
return 'providers.storage.mb'.tr(args: [mebibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
} else {
|
2022-09-06 09:17:44 +00:00
|
|
|
return 'providers.storage.gb'.tr(args: [gibibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 05:35:49 +00:00
|
|
|
}
|